mirror of https://github.com/flightphp/core
				
				
				
			
			You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							103 lines
						
					
					
						
							2.4 KiB
						
					
					
				
			
		
		
	
	
							103 lines
						
					
					
						
							2.4 KiB
						
					
					
				<?php
 | 
						|
 | 
						|
declare(strict_types=1);
 | 
						|
 | 
						|
namespace tests;
 | 
						|
 | 
						|
use flight\util\Collection;
 | 
						|
use PHPUnit\Framework\TestCase;
 | 
						|
 | 
						|
class CollectionTest extends TestCase
 | 
						|
{
 | 
						|
    private Collection $collection;
 | 
						|
 | 
						|
    protected function setUp(): void
 | 
						|
    {
 | 
						|
        $this->collection = new Collection(['a' => 1, 'b' => 2]);
 | 
						|
    }
 | 
						|
 | 
						|
    // Get an item
 | 
						|
    public function testGet()
 | 
						|
    {
 | 
						|
        $this->assertEquals(1, $this->collection->a);
 | 
						|
    }
 | 
						|
 | 
						|
    // Set an item
 | 
						|
    public function testSet()
 | 
						|
    {
 | 
						|
        $this->collection->c = 3;
 | 
						|
        $this->assertEquals(3, $this->collection->c);
 | 
						|
    }
 | 
						|
 | 
						|
    // Check if an item exists
 | 
						|
    public function testExists()
 | 
						|
    {
 | 
						|
        $this->assertTrue(isset($this->collection->a));
 | 
						|
    }
 | 
						|
 | 
						|
    // Unset an item
 | 
						|
    public function testUnset()
 | 
						|
    {
 | 
						|
        unset($this->collection->a);
 | 
						|
        $this->assertFalse(isset($this->collection->a));
 | 
						|
    }
 | 
						|
 | 
						|
    // Count items
 | 
						|
    public function testCount()
 | 
						|
    {
 | 
						|
        $this->assertEquals(2, count($this->collection));
 | 
						|
    }
 | 
						|
 | 
						|
    // Iterate through items
 | 
						|
    public function testIterate()
 | 
						|
    {
 | 
						|
        $items = [];
 | 
						|
        foreach ($this->collection as $key => $value) {
 | 
						|
            $items[$key] = $value;
 | 
						|
        }
 | 
						|
 | 
						|
        $this->assertEquals(['a' => 1, 'b' => 2], $items);
 | 
						|
    }
 | 
						|
 | 
						|
    public function testJsonSerialize()
 | 
						|
    {
 | 
						|
        $this->assertEquals(['a' => 1, 'b' => 2], $this->collection->jsonSerialize());
 | 
						|
    }
 | 
						|
 | 
						|
    public function testOffsetSetWithNullOffset()
 | 
						|
    {
 | 
						|
        $this->collection->offsetSet(null, 3);
 | 
						|
        $this->assertEquals(3, $this->collection->offsetGet(0));
 | 
						|
    }
 | 
						|
 | 
						|
    public function testOffsetExists()
 | 
						|
    {
 | 
						|
        $this->collection->a = 1;
 | 
						|
        $this->assertTrue($this->collection->offsetExists('a'));
 | 
						|
    }
 | 
						|
 | 
						|
    public function testOffsetUnset()
 | 
						|
    {
 | 
						|
        $this->collection->a = 1;
 | 
						|
        $this->assertTrue($this->collection->offsetExists('a'));
 | 
						|
        $this->collection->offsetUnset('a');
 | 
						|
        $this->assertFalse($this->collection->offsetExists('a'));
 | 
						|
    }
 | 
						|
 | 
						|
    public function testKeys()
 | 
						|
    {
 | 
						|
        $this->collection->a = 1;
 | 
						|
        $this->collection->b = 2;
 | 
						|
        $this->assertEquals(['a', 'b'], $this->collection->keys());
 | 
						|
    }
 | 
						|
 | 
						|
    public function testClear()
 | 
						|
    {
 | 
						|
        $this->collection->a = 1;
 | 
						|
        $this->collection->b = 2;
 | 
						|
        $this->assertEquals(['a', 'b'], $this->collection->keys());
 | 
						|
        $this->collection->clear();
 | 
						|
        $this->assertEquals(0, $this->collection->count());
 | 
						|
    }
 | 
						|
}
 |