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
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\Common\Collections;
use Doctrine\Common\Collections\AbstractLazyCollection;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use LogicException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use function is_array;
use function is_numeric;
use function is_string;
/**
* Tests for {@see AbstractLazyCollection}.
*/
#[CoversClass(AbstractLazyCollection::class)]
class AbstractLazyCollectionTest extends CollectionTestCase
{
use VerifyDeprecations;
protected function setUp(): void
{
$this->collection = new LazyArrayCollection(new ArrayCollection());
}
/** @phpstan-param mixed[] $elements */
private function buildCollection(array $elements): LazyArrayCollection
{
return new LazyArrayCollection(new ArrayCollection($elements));
}
public function testClearInitializes(): void
{
$collection = $this->buildCollection(['a', 'b', 'c']);
$collection->clear();
self::assertTrue($collection->isInitialized());
self::assertCount(0, $collection);
}
public function testFilterInitializes(): void
{
$collection = $this->buildCollection([1, 'foo', 3]);
$res = $collection->filter(static fn ($value) => is_numeric($value));
self::assertEquals([0 => 1, 2 => 3], $res->toArray());
}
public function testForAllInitializes(): void
{
$collection = $this->buildCollection(['foo', 'bar']);
self::assertEquals($collection->forAll(static fn ($k, $e) => is_string($e)), true);
self::assertEquals($collection->forAll(static fn ($k, $e) => is_array($e)), false);
}
public function testMapInitializes(): void
{
$collection = $this->buildCollection([1, 2]);
$res = $collection->map(static fn ($e) => $e * 2);
self::assertEquals([2, 4], $res->toArray());
}
public function testPartitionInitializes(): void
{
$collection = $this->buildCollection([true, false]);
$partition = $collection->partition(static fn ($k, $e) => $e === true);
self::assertEquals($partition[0][0], true);
self::assertEquals($partition[1][0], false);
}
public function testSliceInitializes(): void
{
$collection = $this->buildCollection(['one', 'two', 'three']);
$slice = $collection->slice(0, 1);
self::assertIsArray($slice);
self::assertEquals(['one'], $slice);
$slice = $collection->slice(1);
self::assertEquals([1 => 'two', 2 => 'three'], $slice);
$slice = $collection->slice(1, 1);
self::assertEquals([1 => 'two'], $slice);
}
public function testGetInitializes(): void
{
$value = 'foo';
$collection = $this->buildCollection([$value]);
$this->assertSame($value, $collection[0]);
}
public function testUnsetInitializes(): void
{
$collection = $this->buildCollection(['foo', 'bar']);
$collection->offsetUnset(0);
self::assertCount(1, $collection);
self::assertFalse(isset($collection[0]));
}
public function testMatchingInitializes(): void
{
$collection = $this->buildCollection(['foo', 'bar', 'baz']);
self::assertFalse($collection->isInitialized());
$result = $collection->matching(Criteria::create(true));
self::assertTrue($collection->isInitialized());
self::assertCount(3, $result);
}
public function testMatchingWithCriteria(): void
{
$obj1 = new TestObjectPrivatePropertyOnly('bar');
$obj2 = new TestObjectPrivatePropertyOnly('baz');
$obj3 = new TestObjectPrivatePropertyOnly('bar');
$collection = $this->buildCollection([$obj1, $obj2, $obj3]);
$criteria = Criteria::create(true)
->where(Criteria::expr()->eq('fooBar', 'bar'));
$result = $collection->matching($criteria);
self::assertCount(2, $result);
self::assertSame($obj1, $result->first());
}
#[IgnoreDeprecations]
public function testMatchingThrowsExceptionWhenBackedCollectionNotSelectable(): void
{
$lazyCollection = new LazyArrayCollection($this->createStub(Collection::class));
$this->expectException(LogicException::class);
$this->expectExceptionMessage('The backed collection must implement Selectable to use matching().');
$lazyCollection->matching(Criteria::create(true));
}
#[IgnoreDeprecations]
public function testMatchingTriggersDeprecationWhenBackedCollectionNotSelectable(): void
{
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/collections/pull/518');
$lazyCollection = new LazyArrayCollection($this->createStub(Collection::class));
// Trigger initialization with any method - deprecation happens during initialize()
$lazyCollection->isEmpty();
}
}
|