1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
*/
namespace Slim\Tests;
use PHPUnit;
use ReflectionProperty;
use Slim\Collection;
class CollectionTest extends PHPUnit\Framework\TestCase
{
/**
* @var Collection
*/
protected $bag;
/**
* @var ReflectionProperty
*/
protected $property;
protected function setUp(): void
{
$this->bag = new Collection();
$this->property = new ReflectionProperty($this->bag, 'data');
$this->property->setAccessible(true);
}
public function testInitializeWithData()
{
$bag = new Collection(['foo' => 'bar']);
$bagProperty = new ReflectionProperty($bag, 'data');
$bagProperty->setAccessible(true);
$this->assertEquals(['foo' => 'bar'], $bagProperty->getValue($bag));
}
public function testSet()
{
$this->bag->set('foo', 'bar');
$this->assertArrayHasKey('foo', $this->property->getValue($this->bag));
$bag = $this->property->getValue($this->bag);
$this->assertEquals('bar', $bag['foo']);
}
public function testOffsetSet()
{
$this->bag['foo'] = 'bar';
$this->assertArrayHasKey('foo', $this->property->getValue($this->bag));
$bag = $this->property->getValue($this->bag);
$this->assertEquals('bar', $bag['foo']);
}
public function testGet()
{
$this->property->setValue($this->bag, ['foo' => 'bar']);
$this->assertEquals('bar', $this->bag->get('foo'));
}
public function testGetWithDefault()
{
$this->property->setValue($this->bag, ['foo' => 'bar']);
$this->assertEquals('default', $this->bag->get('abc', 'default'));
}
public function testReplace()
{
$this->bag->replace([
'abc' => '123',
'foo' => 'bar',
]);
$this->assertArrayHasKey('abc', $this->property->getValue($this->bag));
$this->assertArrayHasKey('foo', $this->property->getValue($this->bag));
$bag = $this->property->getValue($this->bag);
$this->assertEquals('123', $bag['abc']);
$this->assertEquals('bar', $bag['foo']);
}
public function testAll()
{
$data = [
'abc' => '123',
'foo' => 'bar',
];
$this->property->setValue($this->bag, $data);
$this->assertEquals($data, $this->bag->all());
}
public function testKeys()
{
$data = [
'abc' => '123',
'foo' => 'bar',
];
$this->property->setValue($this->bag, $data);
$this->assertEquals(['abc', 'foo'], $this->bag->keys());
}
public function testHas()
{
$this->property->setValue($this->bag, ['foo' => 'bar']);
$this->assertTrue($this->bag->has('foo'));
$this->assertFalse($this->bag->has('abc'));
}
public function testOffsetExists()
{
$this->property->setValue($this->bag, ['foo' => 'bar']);
$this->assertTrue(isset($this->bag['foo']));
}
public function testRemove()
{
$data = [
'abc' => '123',
'foo' => 'bar',
];
$this->property->setValue($this->bag, $data);
$this->bag->remove('foo');
$this->assertEquals(['abc' => '123'], $this->property->getValue($this->bag));
}
public function testOffsetUnset()
{
$data = [
'abc' => '123',
'foo' => 'bar',
];
$this->property->setValue($this->bag, $data);
unset($this->bag['foo']);
$this->assertEquals(['abc' => '123'], $this->property->getValue($this->bag));
}
public function testClear()
{
$data = [
'abc' => '123',
'foo' => 'bar',
];
$this->property->setValue($this->bag, $data);
$this->bag->clear();
$this->assertEquals([], $this->property->getValue($this->bag));
}
public function testCount()
{
$this->property->setValue($this->bag, ['foo' => 'bar', 'abc' => '123']);
$this->assertEquals(2, $this->bag->count());
}
}
|