Passed
Push — master ( 36d135...97e9b8 )
by Sébastien
08:13
created

RepositoryAssertion::assertEntityValues()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.9666
c 0
b 0
f 0
cc 4
nc 3
nop 5
crap 4
1
<?php
2
3
namespace Bdf\Prime\Test;
4
5
use Bdf\Prime\Entity\Hydrator\Exception\UninitializedPropertyException;
6
use Bdf\Prime\Prime;
7
use PHPUnit\Framework\Constraint\Constraint;
8
9
/**
10
 * RepositoryAssertion
11
 */
12
trait RepositoryAssertion
13
{
14
    /**
15
     * @var TestPack
16
     */
17
    protected $testPack;
18
    
19
    /**
20
     * Get the test pack manager
21
     *
22
     * @return TestPack
23
     */
24 93
    public function getTestPack()
25
    {
26 93
        if ($this->testPack === null) {
27 93
            $this->testPack = TestPack::pack();
28
        }
29
        
30 93
        return $this->testPack;
31
    }
32
    
33
    /**
34
     * Assert that two array of entities are the same
35
     * 
36
     * @param array $expectedEntities
37
     * @param array $actualEntities
38
     * @param string $message
39
     *
40
     * @throws \Exception
41
     */
42 4
    public function assertSameEntities($expectedEntities, $actualEntities, $message = '')
0 ignored issues
show
introduced by
Type hint "array" missing for $actualEntities
Loading history...
introduced by
Type hint "array" missing for $expectedEntities
Loading history...
43
    {
44 4
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        $this->/** @scrutinizer ignore-call */ 
45
               assertEquals(
Loading history...
45 4
            count($expectedEntities),
46 4
            count($actualEntities),
47 4
            'Failed asserting that two collection entities are the same.'.($message ? PHP_EOL.$message : '')
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
48
        );
49
50 3
        foreach ($expectedEntities as $index => $expectedEntity) {
51 3
            $this->assertSameEntity($expectedEntity, $actualEntities[$index], $message);
52
        }
53 2
    }
54
    
55
    /**
56
     * Assert that two entities are the same
57
     * Compare only fields defined in associated mapper
58
     * 
59
     * @param object        $expected
60
     * @param object        $entity
61
     * @param string        $message
62
     *
63
     * @throws \Exception
64
     */
65 16
    public function assertSameEntity($expected, $entity, $message = '')
66
    {
67 16
        $this->assertEntity($expected, $entity, 0, $message);
68 13
    }
69
    
70
     /**
71
     * Assert that two array of entities are equal
0 ignored issues
show
Coding Style introduced by
Expected 6 space(s) before asterisk; 5 found
Loading history...
72
     * 
0 ignored issues
show
Coding Style introduced by
Expected 6 space(s) before asterisk; 5 found
Loading history...
73
     * @param array $expectedEntities
0 ignored issues
show
Coding Style introduced by
Expected 6 space(s) before asterisk; 5 found
Loading history...
74
     * @param array $actualEntities
0 ignored issues
show
Coding Style introduced by
Expected 6 space(s) before asterisk; 5 found
Loading history...
75
     * @param int   $dateTimeDelta
0 ignored issues
show
Coding Style introduced by
Expected 6 space(s) before asterisk; 5 found
Loading history...
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
76
     * @param string $message
0 ignored issues
show
Coding Style introduced by
Expected 6 space(s) before asterisk; 5 found
Loading history...
77
      *
78
      * @throws \Exception
79
     */
0 ignored issues
show
Coding Style introduced by
Expected 6 space(s) before asterisk; 5 found
Loading history...
80 12
    public function assertEntities($expectedEntities, $actualEntities, $dateTimeDelta = 5, $message = '')
0 ignored issues
show
introduced by
Type hint "array" missing for $actualEntities
Loading history...
introduced by
Type hint "array" missing for $expectedEntities
Loading history...
81
    {
82 12
        if (is_string($dateTimeDelta)) {
0 ignored issues
show
introduced by
The condition is_string($dateTimeDelta) is always false.
Loading history...
83
            $message = $dateTimeDelta;
84
            $dateTimeDelta = 5;
85
86
            @trigger_error('The assertEntities interface change. Use message as 4th parameter.', E_USER_DEPRECATED);
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged; found: @trigger_error('The assertEntities interface change. Use message as 4th parameter.'...
Loading history...
introduced by
The trigger_error message 'The assertEntities interface change. Use message as 4th parameter.' does not match the relaxed standard format: thing is deprecated in deprecation-version any free text removal-version. extra-info. See cr-link
Loading history...
87
        }
88
89 12
        $this->assertEquals(
90 12
            count($expectedEntities),
91 12
            count($actualEntities),
92 12
            'Failed asserting that two collection entities are the same.'.($message ? PHP_EOL.$message : '')
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
93
        );
94
95 11
        foreach ($expectedEntities as $index => $expectedEntity) {
96 11
            $this->assertEntity($expectedEntity, $actualEntities[$index], $dateTimeDelta, $message);
97
        }
98 11
    }
99
    
100
    /**
101
     * Assert that two entities are equal
102
     * Compare only fields defined in associated mapper
103
     * Useful for dates : add a time delta
104
     *
105
     * @param object        $expected
106
     * @param object        $entity
107
     * @param int           $dateTimeDelta
0 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
108
     * @param string        $message
109
     *
110
     * @throws \Exception
111
     */
112 46
    public function assertEntity($expected, $entity, $dateTimeDelta = 5, $message = '')
113
    {
114 46
        if (is_string($dateTimeDelta)) {
0 ignored issues
show
introduced by
The condition is_string($dateTimeDelta) is always false.
Loading history...
115
            $message = $dateTimeDelta;
116
            $dateTimeDelta = 5;
117
118
            @trigger_error('The assertEntity interface change. Use message as 4th parameter.', E_USER_DEPRECATED);
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged; found: @trigger_error('The assertEntity interface change. Use message as 4th parameter.'...
Loading history...
introduced by
The trigger_error message 'The assertEntity interface change. Use message as 4th parameter.' does not match the relaxed standard format: thing is deprecated in deprecation-version any free text removal-version. extra-info. See cr-link
Loading history...
119
        }
120
121 46
        $this->compareEntity(get_class($expected), $expected, $entity, $dateTimeDelta, $message);
0 ignored issues
show
Bug introduced by
$expected of type object is incompatible with the type array expected by parameter $expected of Bdf\Prime\Test\Repositor...ertion::compareEntity(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
        $this->compareEntity(get_class($expected), /** @scrutinizer ignore-type */ $expected, $entity, $dateTimeDelta, $message);
Loading history...
122 43
    }
123
124
    /**
125
     * Compare entity values with a map of values.
126
     * The map can also contain constraints
127
     *
128
     * @param string        $expectedClass
129
     * @param array         $expected
130
     * @param object|object[] $entities
131
     * @param int           $dateTimeDelta
0 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
132
     * @param string        $message
133
     *
134
     * @throws \Exception
135
     */
136 2
    public function assertEntityValues($expectedClass, $expected, $entities, $dateTimeDelta = 5, $message = '')
0 ignored issues
show
introduced by
Type hint "array" missing for $expected
Loading history...
137
    {
138 2
        if (!is_array($entities)) {
139 1
            $this->compareEntity($expectedClass, $expected, $entities, $dateTimeDelta, $message);
140 1
            return;
141
        }
142
143 1
        $this->assertEquals(
144 1
            count($expected),
145 1
            count($entities),
146 1
            'Failed asserting that two collection entities are the same.'.($message ? PHP_EOL.$message : '')
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
147
        );
148
149 1
        foreach ($entities as $index => $entity) {
150 1
            $this->compareEntity($expectedClass, $expected[$index], $entity, $dateTimeDelta, $message);
151
        }
152 1
    }
153
154
    /**
155
     * Compare 2 entities
156
     * If strict is false, add delta on date comparison
157
     *
158
     * @param string $expectedClass
159
     * @param array $expected
160
     * @param object $entity
161
     * @param int $dateTimeDelta
0 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
162
     * @param string $message
163
     *
164
     * @throws \Exception
165
     */
166 48
    private function compareEntity($expectedClass, $expected, $entity, $dateTimeDelta = 0, $message = '')
0 ignored issues
show
Coding Style introduced by
Private method name "RepositoryAssertion::compareEntity" must be prefixed with an underscore
Loading history...
introduced by
Type hint "array" missing for $expected
Loading history...
167
    {
168 48
        if (is_string($dateTimeDelta)) {
0 ignored issues
show
introduced by
The condition is_string($dateTimeDelta) is always false.
Loading history...
169
            $message = $dateTimeDelta;
170
            $dateTimeDelta = 5;
171
172
            @trigger_error('The compareEntity interface change. Use message as 4th parameter.', E_USER_DEPRECATED);
0 ignored issues
show
introduced by
The trigger_error message 'The compareEntity interface change. Use message as 4th parameter.' does not match the relaxed standard format: thing is deprecated in deprecation-version any free text removal-version. extra-info. See cr-link
Loading history...
Coding Style introduced by
Silencing errors is discouraged; found: @trigger_error('The compareEntity interface change. Use message as 4th parameter.'...
Loading history...
173
        }
174
175 48
        if ($message == '') {
176 47
            $message = $expectedClass;
177
        }
178
179 48
        $this->assertEquals($expectedClass, get_class($entity), 'Failed asserting that two entities are the same.');
180
181 47
        $repository = Prime::repository($entity);
182
183 47
        foreach ($repository->metadata()->attributes as $attribute => $metadata) {
184 47
            $path = $repository->metadata()->entityClass.'::'.$attribute;
185 47
            $isUninitialized = false;
186
187
            try {
188 47
                $expectedValue = is_object($expected) ? $repository->extractOne($expected, $attribute) : ($expected[$attribute] ?? null);
0 ignored issues
show
Bug introduced by
The method extractOne() does not exist on Bdf\Prime\Repository\RepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Repository\RepositoryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

188
                $expectedValue = is_object($expected) ? $repository->/** @scrutinizer ignore-call */ extractOne($expected, $attribute) : ($expected[$attribute] ?? null);
Loading history...
189
            } catch (UninitializedPropertyException $e) {
190
                $isUninitialized = true;
191
            }
192
193
            try {
194 47
                $value = $repository->extractOne($entity, $attribute);
195
196 47
                if ($isUninitialized) {
197 47
                    $this->fail($message . ': Expected attribute "'.$path.'" to be not initialised');
0 ignored issues
show
Bug introduced by
It seems like fail() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

197
                    $this->/** @scrutinizer ignore-call */ 
198
                           fail($message . ': Expected attribute "'.$path.'" to be not initialised');
Loading history...
198
                }
199
            } catch (UninitializedPropertyException $e) {
200
                if (!$isUninitialized) {
201
                    $this->fail($message . ': The attribute "'.$path.'" is not initialised');
202
                }
203
            }
204
205 47
            if (!is_object($expectedValue)) {
206 47
                $this->assertSame($expectedValue, $value, $message . ': Expected attribute "'.$path.'" is not the same');
0 ignored issues
show
Bug introduced by
The method assertSame() does not exist on Bdf\Prime\Test\RepositoryAssertion. Did you maybe mean assertSameEntities()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

206
                $this->/** @scrutinizer ignore-call */ 
207
                       assertSame($expectedValue, $value, $message . ': Expected attribute "'.$path.'" is not the same');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
207 47
                continue;
208
            }
209
210 17
            if ($expectedValue instanceof Constraint) {
211 2
                $this->assertThat($value, $expectedValue, $message . ': Expected attribute "'.$path.'" is not the same');
0 ignored issues
show
Bug introduced by
It seems like assertThat() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

211
                $this->/** @scrutinizer ignore-call */ 
212
                       assertThat($value, $expectedValue, $message . ': Expected attribute "'.$path.'" is not the same');
Loading history...
212 17
            } elseif ($expectedValue instanceof \DateTimeInterface) {
0 ignored issues
show
Coding Style introduced by
Usage of ELSEIF not allowed; use ELSE IF instead
Loading history...
213 17
                $this->assertEqualsWithDelta($expectedValue, $value, $dateTimeDelta, $message . ': Expected attribute "'.$path.'" is not the same');
0 ignored issues
show
Bug introduced by
It seems like assertEqualsWithDelta() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

213
                $this->/** @scrutinizer ignore-call */ 
214
                       assertEqualsWithDelta($expectedValue, $value, $dateTimeDelta, $message . ': Expected attribute "'.$path.'" is not the same');
Loading history...
214
            } else {
215 17
                $this->assertEquals($expectedValue, $value, $message . ': Expected attribute "'.$path.'" is not the same');
216
            }
217
        }
0 ignored issues
show
Coding Style introduced by
End comment for long condition not found; expected "//end foreach"
Loading history...
218 45
    }
219
}
220