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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\Common\Collections;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Selectable;
use PHPUnit\Framework\TestCase;
use stdClass;
use function count;
use function is_array;
use function is_numeric;
use function is_string;
use function sprintf;
abstract class CollectionTestCase extends TestCase
{
/** @var Collection<mixed> */
protected Collection $collection;
public function testIssetAndUnset(): void
{
self::assertFalse(isset($this->collection[0]));
$this->collection->add('testing');
self::assertTrue(isset($this->collection[0]));
unset($this->collection[0]);
self::assertFalse(isset($this->collection[0]));
}
public function testRemovingNonExistentEntryReturnsNull(): void
{
self::assertEquals(null, $this->collection->remove('testing_does_not_exist'));
}
public function testExists(): void
{
$this->collection->add('one');
$this->collection->add('two');
$exists = $this->collection->exists(static fn ($k, $e) => $e === 'one');
self::assertTrue($exists);
$exists = $this->collection->exists(static fn ($k, $e) => $e === 'other');
self::assertFalse($exists);
}
public function testFindFirst(): void
{
$this->collection->add('one');
$this->collection->add('two');
$one = $this->collection->findFirst(static fn ($k, $e) => $e === 'one');
self::assertSame('one', $one);
}
public function testFindFirstNotFound(): void
{
$this->collection->add('one');
$this->collection->add('two');
$other = $this->collection->findFirst(static fn ($k, $e) => $e === 'other');
self::assertNull($other);
}
public function testMap(): void
{
$this->collection->add(1);
$this->collection->add(2);
$res = $this->collection->map(static fn ($e) => $e * 2);
self::assertEquals([2, 4], $res->toArray());
}
public function testReduce(): void
{
$this->collection->add(1);
$this->collection->add(2);
$this->collection->add(3);
$this->collection->add(4);
$res = $this->collection->reduce(static fn ($sum, $e) => $sum + $e);
self::assertSame(10, $res);
}
public function testFilter(): void
{
$this->collection->add(1);
$this->collection->add('foo');
$this->collection->add(3);
$res = $this->collection->filter(static fn ($e) => is_numeric($e));
self::assertEquals([0 => 1, 2 => 3], $res->toArray());
}
public function testFilterByValueAndKey(): void
{
$this->collection->add(1);
$this->collection->add('foo');
$this->collection->add(3);
$this->collection->add(4);
$this->collection->add(5);
$res = $this->collection->filter(static fn ($v, $k) => is_numeric($v) && $k % 2 === 0);
self::assertSame([0 => 1, 2 => 3, 4 => 5], $res->toArray());
}
public function testFirstAndLast(): void
{
$this->collection->add('one');
$this->collection->add('two');
self::assertEquals($this->collection->first(), 'one');
self::assertEquals($this->collection->last(), 'two');
}
public function testArrayAccess(): void
{
$this->collection[] = 'one';
$this->collection[] = 'two';
self::assertEquals($this->collection[0], 'one');
self::assertEquals($this->collection[1], 'two');
unset($this->collection[0]);
self::assertEquals($this->collection->count(), 1);
}
public function testContainsKey(): void
{
$this->collection[5] = 'five';
self::assertTrue($this->collection->containsKey(5));
}
public function testContains(): void
{
$this->collection[0] = 'test';
self::assertTrue($this->collection->contains('test'));
}
public function testSearch(): void
{
$this->collection[0] = 'test';
self::assertEquals(0, $this->collection->indexOf('test'));
}
public function testGet(): void
{
$this->collection[0] = 'test';
self::assertEquals('test', $this->collection->get(0));
}
public function testGetKeys(): void
{
$this->collection[] = 'one';
$this->collection[] = 'two';
self::assertEquals([0, 1], $this->collection->getKeys());
}
public function testGetValues(): void
{
$this->collection[] = 'one';
$this->collection[] = 'two';
self::assertEquals(['one', 'two'], $this->collection->getValues());
}
public function testCount(): void
{
$this->collection[] = 'one';
$this->collection[] = 'two';
self::assertEquals($this->collection->count(), 2);
self::assertEquals(count($this->collection), 2);
}
public function testForAll(): void
{
$this->collection[] = 'one';
$this->collection[] = 'two';
self::assertEquals($this->collection->forAll(static fn ($k, $e) => is_string($e)), true);
self::assertEquals($this->collection->forAll(static fn ($k, $e) => is_array($e)), false);
}
public function testPartition(): void
{
$this->collection[] = true;
$this->collection[] = false;
$partition = $this->collection->partition(static fn ($k, $e) => $e === true);
self::assertEquals($partition[0][0], true);
self::assertEquals($partition[1][0], false);
}
public function testClear(): void
{
$this->collection[] = 'one';
$this->collection[] = 'two';
$this->collection->clear();
self::assertEquals($this->collection->isEmpty(), true);
}
public function testRemove(): void
{
$this->collection[] = 'one';
$this->collection[] = 'two';
$el = $this->collection->remove(0);
self::assertEquals('one', $el);
self::assertEquals($this->collection->contains('one'), false);
self::assertNull($this->collection->remove(0));
}
public function testRemoveElement(): void
{
$this->collection[] = 'one';
$this->collection[] = 'two';
self::assertTrue($this->collection->removeElement('two'));
self::assertFalse($this->collection->contains('two'));
self::assertFalse($this->collection->removeElement('two'));
}
public function testSlice(): void
{
$this->collection[] = 'one';
$this->collection[] = 'two';
$this->collection[] = 'three';
$slice = $this->collection->slice(0, 1);
self::assertIsArray($slice);
self::assertEquals(['one'], $slice);
$slice = $this->collection->slice(1);
self::assertEquals([1 => 'two', 2 => 'three'], $slice);
$slice = $this->collection->slice(1, 1);
self::assertEquals([1 => 'two'], $slice);
}
protected function fillMatchingFixture(): void
{
$std1 = new stdClass();
$std1->foo = 'bar';
$this->collection[] = $std1;
$std2 = new stdClass();
$std2->foo = 'baz';
$this->collection[] = $std2;
}
public function testCanRemoveNullValuesByKey(): void
{
$this->collection->add(null);
$this->collection->remove(0);
self::assertTrue($this->collection->isEmpty());
}
public function testCanVerifyExistingKeysWithNullValues(): void
{
$this->collection->set('key', null);
self::assertTrue($this->collection->containsKey('key'));
}
public function testMatchingAlwaysReturnsCollection(): void
{
if (! $this->collection instanceof Selectable) {
self::markTestSkipped(sprintf('Collection does not implement %s', Selectable::class));
}
$criteria = Criteria::create();
self::assertInstanceOf(Collection::class, $this->collection->matching($criteria));
self::assertInstanceOf(Selectable::class, $this->collection->matching($criteria));
}
}
|