1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
declare(strict_types=1);
|
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasDoctrineFixtures\Service\Repository;
|
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Persistence\ObjectManager;
|
8
|
|
|
use Doctrine\ORM\UnitOfWork;
|
9
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
10
|
|
|
use PHPUnit\Framework\TestCase;
|
11
|
|
|
|
12
|
|
|
/**
|
13
|
|
|
* @author Alex Patterson <[email protected]>
|
14
|
|
|
* @package Arp\LaminasDoctrineFixtures\Service\Repository
|
15
|
|
|
*/
|
16
|
|
|
final class ReferenceRepositoryTest extends TestCase
|
17
|
|
|
{
|
18
|
|
|
/**
|
19
|
|
|
* @var ObjectManager|MockObject
|
20
|
|
|
*/
|
21
|
|
|
private $objectManager;
|
22
|
|
|
|
23
|
|
|
public function setUp(): void
|
24
|
|
|
{
|
25
|
|
|
$this->objectManager = $this->getMockForAbstractClass(ObjectManager::class);
|
26
|
|
|
}
|
27
|
|
|
|
28
|
|
|
/**
|
29
|
|
|
* Assert that the ReferenceRepository is of type \Doctrine\Common\DataFixtures\ReferenceRepository.
|
30
|
|
|
*
|
31
|
|
|
* @covers \Arp\LaminasDoctrineFixtures\Service\Repository\ReferenceRepository
|
32
|
|
|
*/
|
33
|
|
|
public function testExtendsReferenceRepository(): void
|
34
|
|
|
{
|
35
|
|
|
$referenceRepository = new ReferenceRepository($this->objectManager);
|
36
|
|
|
|
37
|
|
|
$this->assertInstanceOf(\Doctrine\Common\DataFixtures\ReferenceRepository::class, $referenceRepository);
|
38
|
|
|
}
|
39
|
|
|
|
40
|
|
|
/**
|
41
|
|
|
* Assert a collection reference can be set and get on a ReferenceRepository via
|
42
|
|
|
* setCollectionReference() and getCollectionReference().
|
43
|
|
|
*
|
44
|
|
|
* @covers \Arp\LaminasDoctrineFixtures\Service\Repository\ReferenceRepository::setCollectionReference
|
45
|
|
|
* @covers \Arp\LaminasDoctrineFixtures\Service\Repository\ReferenceRepository::getCollectionReference
|
46
|
|
|
*/
|
47
|
|
|
public function testSetAndGetCollectionReference(): void
|
48
|
|
|
{
|
49
|
|
|
/** @var ReferenceRepository|MockObject $collectionReference */
|
50
|
|
|
$collectionReference = $this->getMockBuilder(ReferenceRepository::class)
|
51
|
|
|
->setConstructorArgs([$this->objectManager])
|
52
|
|
|
->onlyMethods(['setReference', 'getReference'])
|
53
|
|
|
->getMock();
|
54
|
|
|
|
55
|
|
|
$collectionName = 'FooCollection';
|
56
|
|
|
|
57
|
|
|
/** @var \stdClass[] $data */
|
58
|
|
|
$data = [
|
59
|
|
|
'A' => new \stdClass(),
|
60
|
|
|
'B' => new \stdClass(),
|
61
|
|
|
'C' => new \stdClass(),
|
62
|
|
|
];
|
63
|
|
|
|
64
|
|
|
$setReferenceArgs = $getReferenceArgs = [];
|
65
|
|
|
foreach ($data as $index => $item) {
|
66
|
|
|
$itemName = $collectionName . '.' . $index;
|
67
|
|
|
$setReferenceArgs[] = [$itemName, $item];
|
68
|
|
|
$getReferenceArgs[] = [$itemName];
|
69
|
|
|
}
|
70
|
|
|
|
71
|
|
|
$collectionReference->expects($this->exactly(count($setReferenceArgs)))
|
72
|
|
|
->method('setReference')
|
73
|
|
|
->withConsecutive(...$setReferenceArgs);
|
74
|
|
|
|
75
|
|
|
$collectionReference->expects($this->exactly(count($setReferenceArgs)))
|
76
|
|
|
->method('getReference')
|
77
|
|
|
->withConsecutive(...$getReferenceArgs)
|
78
|
|
|
->willReturn(...array_values($data));
|
79
|
|
|
|
80
|
|
|
$collectionReference->setCollectionReference($collectionName, $data);
|
81
|
|
|
|
82
|
|
|
$this->assertSame($data, $collectionReference->getCollectionReference($collectionName));
|
83
|
|
|
}
|
84
|
|
|
}
|
85
|
|
|
|