This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AbstractProviderTestCase.php
185 lines (151 loc) · 5.42 KB
/
AbstractProviderTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
declare(strict_types=1);
/**
* Copyright (c) 2017-2020 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/test-util
*/
namespace Ergebnis\Test\Util\Test\Unit\DataProvider;
use Ergebnis\Test\Util;
use PHPUnit\Framework;
/**
* @internal
*/
abstract class AbstractProviderTestCase extends Framework\TestCase
{
use Util\Helper;
/**
* @param array<string, mixed> $values
* @param \Generator<string, array<mixed>> $provider
*/
final protected static function assertProvidesDataForValues(array $values, \Generator $provider): void
{
self::assertExpectedValuesAreNotEmpty($values);
$expected = \iterator_to_array(self::provideDataForValues($values));
$provided = \iterator_to_array($provider);
self::assertProvidedDataIsNotEmpty($provided);
self::assertEquals(
$expected,
$provided,
'Failed asserting that a generator yields data for expected values.'
);
}
/**
* @param array<string, \Closure|mixed> $tests
* @param \Generator<string, array<mixed>> $provider
*/
final protected static function assertProvidesDataForValuesPassingTests(array $tests, \Generator $provider): void
{
$provided = \iterator_to_array($provider);
self::assertEquals(
\array_keys($tests),
\array_keys($provided),
'Failed asserting that the provided data has the same keys as the tests.'
);
$normalizedTests = \array_map(static function ($test): \Closure {
if (!$test instanceof \Closure) {
return static function ($value) use ($test): bool {
return $value === $test;
};
}
return $test;
}, $tests);
$keysWhereValueDoesNotPassTest = \array_filter(\array_keys($provided), static function (string $key) use ($provided, $normalizedTests): bool {
$set = $provided[$key];
self::assertIsArray($set);
self::assertCount(1, $set);
$value = \array_shift($set);
$test = $normalizedTests[$key];
return true !== $test($value);
});
self::assertEquals(
[],
$keysWhereValueDoesNotPassTest,
'Failed asserting that all values pass the corresponding tests.'
);
}
/**
* @param array $actual
*/
final protected static function assertProvidedDataIsNotEmpty(array $actual): void
{
self::assertNotEmpty($actual, 'Failed asserting that provided values are not empty.');
}
/**
* @param array $values
*/
private static function assertExpectedValuesAreNotEmpty(array $values): void
{
self::assertNotEmpty($values, 'Failed asserting that expected values are not empty.');
}
/**
* @param \Closure $test
* @param array<string, mixed> $provided
*/
private static function assertProvidedDataContainsArraysWhereFirstElementPassesTest(\Closure $test, array $provided): void
{
self::assertProvidedDataContainsArraysWithOneElement($provided);
$value = \array_map(static function (array $set) {
return \array_shift($set);
}, $provided);
$tested = \array_filter($value, static function ($value) use ($test): bool {
return true === $test($value);
});
self::assertEquals(
$value,
$tested,
'Failed asserting that the first value in each array passed the test.'
);
}
/**
* @param \Closure $test
* @param array<string, mixed> $provided
*/
private static function assertProvidedDataContainsArraysWhereFirstElementDoesNotPassTest(\Closure $test, array $provided): void
{
self::assertProvidedDataContainsArraysWithOneElement($provided);
$value = \array_map(static function (array $set) {
return \array_shift($set);
}, $provided);
$tested = \array_filter($value, static function ($value) use ($test): bool {
return false === $test($value);
});
self::assertEquals(
$value,
$tested,
'Failed asserting that the first value in each array does not pass the test.'
);
}
/**
* @param array<string, mixed> $provided
*/
private static function assertProvidedDataContainsArraysWithOneElement(array $provided): void
{
self::assertProvidedDataContainsArraysOnly($provided);
$setsWhereNumberOfProvidedArgumentsIsNotOne = \array_filter($provided, static function (array $set): bool {
return 1 !== \count($set);
});
self::assertEquals(
[],
$setsWhereNumberOfProvidedArgumentsIsNotOne,
'Failed asserting that each set in the provided data contains only a single value.'
);
}
/**
* @param array<string, mixed> $provided
*/
private static function assertProvidedDataContainsArraysOnly(array $provided): void
{
$values = \array_filter($provided, static function ($set): bool {
return !\is_array($set);
});
self::assertEquals(
[],
$values,
'Failed asserting that each value is an array.'
);
}
}