|
4 | 4 |
|
5 | 5 | namespace Yokai\Batch\Tests\Bridge\Doctrine\ORM; |
6 | 6 |
|
7 | | -use Doctrine\ORM\AbstractQuery; |
8 | 7 | use Doctrine\ORM\EntityManager; |
9 | | -use Doctrine\ORM\QueryBuilder; |
| 8 | +use Doctrine\ORM\ORMSetup; |
| 9 | +use Doctrine\ORM\Tools\SchemaTool; |
10 | 10 | use Doctrine\Persistence\ManagerRegistry; |
| 11 | +use Generator; |
11 | 12 | use PHPUnit\Framework\TestCase; |
12 | | -use Prophecy\PhpUnit\ProphecyTrait; |
13 | | -use Prophecy\Prophecy\ObjectProphecy; |
14 | 13 | use Yokai\Batch\Bridge\Doctrine\ORM\EntityReader; |
15 | 14 | use Yokai\Batch\Exception\UnexpectedValueException; |
| 15 | +use Yokai\Batch\Tests\Bridge\Doctrine\ORM\Dummy\SingleManagerRegistry; |
| 16 | +use Yokai\Batch\Tests\Bridge\Doctrine\ORM\Entity\Unknown; |
| 17 | +use Yokai\Batch\Tests\Bridge\Doctrine\ORM\Entity\User; |
16 | 18 |
|
17 | 19 | class EntityReaderTest extends TestCase |
18 | 20 | { |
19 | | - use ProphecyTrait; |
| 21 | + private EntityManager $manager; |
| 22 | + private ManagerRegistry $doctrine; |
20 | 23 |
|
21 | | - private const ENTITY = 'App\Entity\User'; |
22 | | - |
23 | | - public function testRead() |
| 24 | + protected function setUp(): void |
24 | 25 | { |
25 | | - /** @var ObjectProphecy|AbstractQuery $query */ |
26 | | - $query = $this->prophesize(AbstractQuery::class); |
27 | | - $query->toIterable() |
28 | | - ->shouldBeCalledTimes(1) |
29 | | - ->willReturn( |
30 | | - new \ArrayIterator([[$user1 = new User('1')], [$user2 = new User('2')], [$user3 = new User('3')]]) |
31 | | - ); |
32 | | - |
33 | | - $queryBuilder = $this->getMockBuilder(QueryBuilder::class) |
34 | | - ->disableOriginalConstructor() |
35 | | - ->getMock(); |
36 | | - $queryBuilder->expects($this->once()) |
37 | | - ->method('select') |
38 | | - ->with('e') |
39 | | - ->will($this->returnSelf()); |
40 | | - $queryBuilder->expects($this->once()) |
41 | | - ->method('from') |
42 | | - ->with(self::ENTITY) |
43 | | - ->will($this->returnSelf()); |
44 | | - $queryBuilder->expects($this->once()) |
45 | | - ->method('getQuery') |
46 | | - ->will($this->returnValue($query->reveal())); |
| 26 | + $config = ORMSetup::createAttributeMetadataConfiguration([__DIR__ . '/Entity'], true); |
| 27 | + $this->manager = EntityManager::create(['url' => \getenv('DATABASE_URL')], $config); |
| 28 | + $this->doctrine = new SingleManagerRegistry($this->manager); |
47 | 29 |
|
48 | | - /** @var ObjectProphecy|EntityManager $manager */ |
49 | | - $manager = $this->prophesize(EntityManager::class); |
50 | | - $manager->createQueryBuilder() |
51 | | - ->shouldBeCalledTimes(1) |
52 | | - ->willReturn($queryBuilder); |
| 30 | + (new SchemaTool($this->manager)) |
| 31 | + ->createSchema($this->manager->getMetadataFactory()->getAllMetadata()); |
| 32 | + } |
53 | 33 |
|
54 | | - /** @var ObjectProphecy|ManagerRegistry $doctrine */ |
55 | | - $doctrine = $this->prophesize(ManagerRegistry::class); |
56 | | - $doctrine->getManagerForClass(self::ENTITY) |
57 | | - ->shouldBeCalledTimes(1) |
58 | | - ->willReturn($manager->reveal()); |
| 34 | + public function testRead(): void |
| 35 | + { |
| 36 | + $this->manager->persist($forest = new User('Forest')); |
| 37 | + $this->manager->persist($jenny = new User('Jenny')); |
| 38 | + $this->manager->persist($bubba = new User('Bubba')); |
| 39 | + $this->manager->flush(); |
59 | 40 |
|
60 | | - $reader = new EntityReader($doctrine->reveal(), self::ENTITY); |
| 41 | + $reader = new EntityReader($this->doctrine, User::class); |
61 | 42 | $entities = $reader->read(); |
62 | 43 |
|
63 | | - self::assertInstanceOf(\Generator::class, $entities); |
64 | | - self::assertSame([$user1, $user2, $user3], iterator_to_array($entities)); |
| 44 | + self::assertInstanceOf(Generator::class, $entities); |
| 45 | + self::assertSame([$forest, $jenny, $bubba], \iterator_to_array($entities)); |
65 | 46 | } |
66 | 47 |
|
67 | | - public function testReadExceptionWithUnknownEntityClass() |
| 48 | + public function testReadExceptionWithUnknownEntityClass(): void |
68 | 49 | { |
69 | 50 | $this->expectException(UnexpectedValueException::class); |
70 | 51 |
|
71 | | - /** @var ObjectProphecy|ManagerRegistry $doctrine */ |
72 | | - $doctrine = $this->prophesize(ManagerRegistry::class); |
73 | | - $doctrine->getManagerForClass(self::ENTITY) |
74 | | - ->shouldBeCalledTimes(1) |
75 | | - ->willReturn(null); |
| 52 | + $reader = new EntityReader($this->doctrine, Unknown::class); |
| 53 | + $entities = $reader->read(); |
76 | 54 |
|
77 | | - $reader = new EntityReader($doctrine->reveal(), self::ENTITY); |
78 | | - iterator_to_array($reader->read()); // the method is using a yield expression |
| 55 | + self::assertInstanceOf(Generator::class, $entities); |
| 56 | + \iterator_to_array($entities); |
79 | 57 | } |
80 | 58 | } |
0 commit comments