Passed
Pull Request — 4 (#10028)
by Steve
08:25
created

expectExpectationFailedException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Dev\Tests;
4
5
use PHPUnit_Framework_ExpectationFailedException;
0 ignored issues
show
Bug introduced by
The type PHPUnit_Framework_ExpectationFailedException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use PHPUnit\Framework\ExpectationFailedException;
7
use SilverStripe\Dev\SapphireTest;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Dev\SapphireTest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use SilverStripe\ORM\ArrayList;
9
use SilverStripe\Security\Member;
10
use SilverStripe\Security\Permission;
11
12
class SapphireTestTest extends SapphireTest
13
{
14
15
    /**
16
     * @return array
17
     */
18
    public function provideResolveFixturePath()
19
    {
20
        return [
21
            'sameDirectory' => [
22
                __DIR__ . '/CsvBulkLoaderTest.yml',
23
                './CsvBulkLoaderTest.yml',
24
                'Could not resolve fixture path relative from same directory',
25
            ],
26
            'filenameOnly' => [
27
                __DIR__ . '/CsvBulkLoaderTest.yml',
28
                'CsvBulkLoaderTest.yml',
29
                'Could not resolve fixture path from filename only',
30
            ],
31
            'parentPath' => [
32
                dirname(__DIR__) . '/ORM/DataObjectTest.yml',
33
                '../ORM/DataObjectTest.yml',
34
                'Could not resolve fixture path from parent path',
35
            ],
36
            'absolutePath' => [
37
                dirname(__DIR__) . '/ORM/DataObjectTest.yml',
38
                dirname(__DIR__) . '/ORM/DataObjectTest.yml',
39
                'Could not relsolve fixture path from absolute path',
40
            ],
41
        ];
42
    }
43
44
    /**
45
     * @dataProvider provideResolveFixturePath
46
     */
47
    public function testResolveFixturePath($expected, $path, $message)
48
    {
49
        $this->assertEquals(
50
            $expected,
51
            $this->resolveFixturePath($path),
52
            $message
53
        );
54
    }
55
56
    /**
57
     * @useDatabase
58
     */
59
    public function testActWithPermission()
60
    {
61
        $this->logOut();
62
        $this->assertFalse(Permission::check('ADMIN'));
63
        $this->actWithPermission('ADMIN', function () {
64
            $this->assertTrue(Permission::check('ADMIN'), 'Member should now have ADMIN role');
65
            // check nested actAs calls work as expected
66
            Member::actAs(null, function () {
67
                $this->assertFalse(Permission::check('ADMIN'), 'Member should not act as ADMIN any more after reset');
68
            });
69
        });
70
    }
71
72
    /**
73
     * @useDatabase
74
     */
75
    public function testCreateMemberWithPermission()
76
    {
77
        $this->assertEmpty(
78
            Member::get()->filter(['Email' => '[email protected]']),
79
            'DB should not have the test member created when the test starts'
80
        );
81
        $this->createMemberWithPermission('TESTPERM');
82
        $this->assertCount(
83
            1,
84
            Member::get()->filter(['Email' => '[email protected]']),
85
            'Database should now contain the test member'
86
        );
87
    }
88
89
    /**
90
     * @dataProvider \SilverStripe\Dev\Tests\SapphireTestTest\DataProvider::provideAllMatchingList()
91
     *
92
     * @param $match
93
     * @param $itemsForList
94
     *
95
     * @testdox Has assertion assertListAllMatch
96
     */
97
    public function testAssertListAllMatch($match, $itemsForList, $message)
98
    {
99
        $list = $this->generateArrayListFromItems($itemsForList);
100
101
        $this->assertListAllMatch($match, $list, $message);
102
    }
103
104
    /**
105
     * generate SS_List as this is not possible in dataProvider
106
     *
107
     * @param array $itemsForList
108
     *
109
     * @return ArrayList
110
     */
111
    private function generateArrayListFromItems($itemsForList)
112
    {
113
        $list = ArrayList::create();
114
        foreach ($itemsForList as $data) {
115
            $list->push(Member::create($data));
116
        }
117
        return $list;
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    private function expectExpectationFailedException(): void
124
    {
125
        if (class_exists('PHPUnit_Framework_ExpectationFailedException')) {
126
            $class = PHPUnit_Framework_ExpectationFailedException::class;
127
        } else {
128
            $class = ExpectationFailedException::class;
129
        }
130
        $this->expectException($class);
131
    }
132
133
    /**
134
     * @dataProvider \SilverStripe\Dev\Tests\SapphireTestTest\DataProvider::provideNotMatchingList()
135
     *
136
     * @param $match
137
     * @param $itemsForList
138
     *
139
     * @testdox assertion assertListAllMatch fails when not all items are matching
140
     */
141
    public function testAssertListAllMatchFailsWhenNotMatchingAllItems($match, $itemsForList)
142
    {
143
        $this->expectExpectationFailedException();
144
        $list = $this->generateArrayListFromItems($itemsForList);
145
146
        $this->assertListAllMatch($match, $list);
147
    }
148
149
    /**
150
     * @dataProvider \SilverStripe\Dev\Tests\SapphireTestTest\DataProvider::provideEqualListsWithEmptyList()
151
     *
152
     * @param $matches
153
     * @param $itemsForList
154
     *
155
     * @testdox Has assertion assertListContains
156
     */
157
    public function testAssertListContains($matches, $itemsForList)
158
    {
159
        $list = $this->generateArrayListFromItems($itemsForList);
160
        $list->push(Member::create(['FirstName' => 'Foo', 'Surname' => 'Foo']));
161
        $list->push(Member::create(['FirstName' => 'Bar', 'Surname' => 'Bar']));
162
        $list->push(Member::create(['FirstName' => 'Baz', 'Surname' => 'Baz']));
163
164
        $this->assertListContains($matches, $list, 'The list does not contain the expected items');
165
    }
166
167
    /**
168
     * @dataProvider \SilverStripe\Dev\Tests\SapphireTestTest\DataProvider::provideNotContainingList
169
     * @testdox assertion assertListEquals fails on non equal Lists
170
     *
171
     * @param $matches
172
     * @param $itemsForList array
173
     */
174
    public function testAssertListContainsFailsIfListDoesNotContainMatch($matches, $itemsForList)
175
    {
176
        $this->expectExpectationFailedException();
177
        $list = $this->generateArrayListFromItems($itemsForList);
178
        $list->push(Member::create(['FirstName' => 'Foo', 'Surname' => 'Foo']));
179
        $list->push(Member::create(['FirstName' => 'Bar', 'Surname' => 'Bar']));
180
        $list->push(Member::create(['FirstName' => 'Baz', 'Surname' => 'Baz']));
181
182
        $this->assertListContains($matches, $list);
183
    }
184
185
    /**
186
     * @dataProvider \SilverStripe\Dev\Tests\SapphireTestTest\DataProvider::provideNotContainingList
187
     *
188
     * @testdox Has assertion assertListNotContains
189
     *
190
     * @param $matches
191
     * @param $itemsForList
192
     */
193
    public function testAssertListNotContains($matches, $itemsForList)
194
    {
195
        $list = $this->generateArrayListFromItems($itemsForList);
196
197
        $this->assertListNotContains($matches, $list, 'List contains forbidden items');
198
    }
199
200
    /**
201
     * @dataProvider \SilverStripe\Dev\Tests\SapphireTestTest\DataProvider::provideEqualLists
202
     *
203
     * @param $matches
204
     * @param $itemsForList
205
     *
206
     * @testdox assertion assertListNotContains throws a exception when a matching item is found in the list
207
     */
208
    public function testAssertListNotContainsFailsWhenListContainsAMatch($matches, $itemsForList)
209
    {
210
        $this->expectExpectationFailedException();
211
        $list = $this->generateArrayListFromItems($itemsForList);
212
        $list->push(Member::create(['FirstName' => 'Foo', 'Surname' => 'Foo']));
213
        $list->push(Member::create(['FirstName' => 'Bar', 'Surname' => 'Bar']));
214
        $list->push(Member::create(['FirstName' => 'Baz', 'Surname' => 'Baz']));
215
216
        $this->assertListNotContains($matches, $list);
217
    }
218
219
    /**
220
     * @dataProvider \SilverStripe\Dev\Tests\SapphireTestTest\DataProvider::provideEqualListsWithEmptyList()
221
     * @testdox Has assertion assertListEquals
222
     *
223
     * @param $matches
224
     * @param $itemsForList
225
     */
226
    public function testAssertListEquals($matches, $itemsForList)
227
    {
228
        $list = $this->generateArrayListFromItems($itemsForList);
229
230
        $this->assertListEquals($matches, $list, 'Lists do not equal');
231
    }
232
233
    /**
234
     * @dataProvider \SilverStripe\Dev\Tests\SapphireTestTest\DataProvider::provideNonEqualLists
235
     * @testdox assertion assertListEquals fails on non equal Lists
236
     *
237
     * @param $matches
238
     * @param $itemsForList
239
     */
240
    public function testAssertListEqualsFailsOnNonEqualLists($matches, $itemsForList)
241
    {
242
        $this->expectExpectationFailedException();
243
        $list = $this->generateArrayListFromItems($itemsForList);
244
245
        $this->assertListEquals($matches, $list);
246
    }
247
}
248