Passed
Pull Request — master (#8)
by Alex
03:52
created

CollectionEventTest::getSetAndGetCollectionData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 16
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\DoctrineEntityRepository\Persistence\Event;
6
7
use Arp\DoctrineEntityRepository\Constant\EntityEventName;
8
use Arp\DoctrineEntityRepository\Persistence\Event\CollectionEvent;
9
use Arp\DoctrineEntityRepository\Persistence\PersistServiceInterface;
10
use Arp\Entity\EntityInterface;
11
use Arp\EventDispatcher\Resolver\EventNameAwareInterface;
12
use Doctrine\ORM\EntityManagerInterface;
13
use PHPUnit\Framework\MockObject\MockObject;
14
use PHPUnit\Framework\TestCase;
15
use Psr\Log\LoggerInterface;
16
17
/**
18
 * @covers \Arp\DoctrineEntityRepository\Persistence\Event\CollectionEvent
19
 * @covers \Arp\DoctrineEntityRepository\Persistence\Event\AbstractEntityEvent
20
 *
21
 * @author  Alex Patterson <[email protected]>
22
 * @package ArpTest\DoctrineEntityRepository\Persistence\Event
23
 */
24
final class CollectionEventTest extends TestCase
25
{
26
    /**
27
     * @var string
28
     */
29
    private string $eventName;
30
31
    /**
32
     * @var PersistServiceInterface&MockObject
33
     */
34
    private $persistService;
35
36
    /**
37
     * @var EntityManagerInterface&MockObject
38
     */
39
    private $entityManager;
40
41
    /**
42
     * @var LoggerInterface&MockObject
43
     */
44
    private $logger;
45
46
    /**
47
     * @var array<mixed>
48
     */
49
    private array $params = [];
50
51
    /**
52
     * Prepare the test case dependencies
53
     */
54
    public function setUp(): void
55
    {
56
        $this->eventName = EntityEventName::UPDATE;
57
58
        $this->persistService = $this->createMock(PersistServiceInterface::class);
59
60
        $this->entityManager = $this->createMock(EntityManagerInterface::class);
61
62
        $this->logger = $this->createMock(LoggerInterface::class);
63
    }
64
65
    /**
66
     * Assert that the class implements EventNameAwareInterface
67
     */
68
    public function testImplementsEventNameAwareInterface(): void
69
    {
70
        $event = new CollectionEvent(
71
            $this->eventName,
72
            $this->persistService,
73
            $this->entityManager,
74
            $this->logger,
75
            $this->params
76
        );
77
78
        $this->assertInstanceOf(EventNameAwareInterface::class, $event);
79
    }
80
81
    /**
82
     * Assert that a iterable collection can be get/set on the collection via getCollection() and setCollection()
83
     *
84
     * @param iterable<EntityInterface&MockObject> $collection
85
     *
86
     * @dataProvider getSetAndGetCollectionData
87
     */
88
    public function testSetAndGetCollection(iterable $collection): void
89
    {
90
        $event = new CollectionEvent(
91
            $this->eventName,
92
            $this->persistService,
93
            $this->entityManager,
94
            $this->logger,
95
            $this->params
96
        );
97
98
        $event->setCollection($collection);
99
100
        $this->assertSame($collection, $event->getCollection());
101
    }
102
103
    /**
104
     * @return array<mixed>
105
     */
106
    public function getSetAndGetCollectionData(): array
107
    {
108
        $arrayCollection = [
109
            $this->createMock(EntityInterface::class),
110
            $this->createMock(EntityInterface::class),
111
            $this->createMock(EntityInterface::class),
112
        ];
113
114
        $objectCollection = new \ArrayIterator($arrayCollection);
115
116
        return [
117
            [
118
                $arrayCollection,
119
            ],
120
            [
121
                $objectCollection
122
            ]
123
        ];
124
    }
125
126
    /**
127
     * Assert that calls to count() will return the collection size
128
     *
129
     * @param iterable<EntityInterface&MockObject> $collection
130
     *
131
     * @dataProvider getCountWillReturnCollectionSizeData
132
     */
133
    public function testCountWillReturnCollectionSize(iterable $collection): void
134
    {
135
        $event = new CollectionEvent(
136
            $this->eventName,
137
            $this->persistService,
138
            $this->entityManager,
139
            $this->logger,
140
            $this->params
141
        );
142
143
        $countExpected = ($collection instanceof \Traversable)
144
            ? count(iterator_to_array($collection))
145
            : count($collection);
146
147
        $event->setCollection($collection);
148
149
        $this->assertSame($countExpected, $event->getCount());
150
    }
151
152
    /**
153
     * @return array<mixed>
154
     */
155
    public function getCountWillReturnCollectionSizeData(): array
156
    {
157
        $arrayCollection = [
158
            $this->createMock(EntityInterface::class),
159
            $this->createMock(EntityInterface::class),
160
            $this->createMock(EntityInterface::class),
161
        ];
162
163
        $objectCollection = new \ArrayIterator($arrayCollection);
164
165
        return [
166
            [
167
                $arrayCollection,
168
            ],
169
            [
170
                $objectCollection
171
            ]
172
        ];
173
    }
174
}
175