1 | <?php |
||
13 | trait EntityCollectionTester |
||
14 | { |
||
15 | /** |
||
16 | * @return string |
||
17 | */ |
||
18 | abstract protected function entityCollectionClass(): string; |
||
19 | |||
20 | /** |
||
21 | * @test |
||
22 | * @dataProvider invalidEntities |
||
23 | * @param array $elements |
||
24 | */ |
||
25 | public function invalidElementByConstructor(array $elements) |
||
26 | { |
||
27 | /** @var EntityCollection $entityCollectionClass */ |
||
28 | $entityCollectionClass = $this->entityCollectionClass(); |
||
29 | $exception = $entityCollectionClass::customInvalidEntityException(); |
||
30 | |||
31 | $this->expectException(get_class($exception)); |
||
|
|||
32 | $this->expectExceptionMessage($exception->getMessage()); |
||
33 | |||
34 | new $entityCollectionClass($elements); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @return array |
||
39 | */ |
||
40 | public function invalidEntities() |
||
41 | { |
||
42 | return [ |
||
43 | [[1]], |
||
44 | [['element']], |
||
45 | [[new \stdClass()]], |
||
46 | [[new \stdClass(), new \stdClass()]], |
||
47 | [[null]], |
||
48 | [[false]] |
||
49 | ]; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @test |
||
54 | */ |
||
55 | public function emptyConstructor() |
||
56 | { |
||
57 | /** @var EntityCollection $entityCollectionClass */ |
||
58 | $entityCollectionClass = $this->entityCollectionClass(); |
||
59 | |||
60 | /** @var EntityCollection $collection */ |
||
61 | $collection = new $entityCollectionClass(); |
||
62 | |||
63 | $this->assertSame(0, $collection->count()); |
||
64 | $this->assertTrue($collection->isEmpty()); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @test |
||
69 | */ |
||
70 | public function invalidAddElement() |
||
83 | |||
84 | /** |
||
85 | * @test |
||
86 | */ |
||
87 | public function validChildrenElementByConstructor() |
||
98 | |||
99 | /** |
||
100 | * @test |
||
101 | */ |
||
102 | public function validElementByConstructor() |
||
113 | |||
114 | /** |
||
115 | * @test |
||
116 | */ |
||
117 | public function addTwoValidElement() |
||
136 | } |
||
137 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.