Failed Conditions
Pull Request — master (#26)
by Bob Olde
07:33
created

tests/services/UsersTest.php (3 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft\Craft;
6
use Craft\BaseTest;
7
use Craft\FieldLayoutModel;
8
use Craft\LocalizationService;
9
use Craft\FieldsService;
10
use NerdsAndCompany\Schematic\Models\Result;
11
use PHPUnit_Framework_MockObject_MockObject as Mock;
12
13
/**
14
 * Class UsersTest.
15
 *
16
 * @author    Nerds & Company
17
 * @copyright Copyright (c) 2015, Nerds & Company
18
 * @license   MIT
19
 *
20
 * @link      http://www.nerds.company
21
 *
22
 * @coversDefaultClass NerdsAndCompany\Schematic\Services\Users
23
 * @covers ::__construct
24
 * @covers ::<!public>
25
 */
26
class UsersTest extends BaseTest
27
{
28
    /**
29
     * @var Users
30
     */
31
    private $schematicUsersService;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function setUp()
37
    {
38
        $this->schematicUsersService = new Users();
39
    }
40
41
    /**
42
     * Get mock for localization service.
43
     *
44
     * @return Mock|PathService
45
     */
46
    public function getMockLocalizationService()
47
    {
48
        $mock = $this->getMockBuilder(LocalizationService::class)->getMock();
49
50
        $mock->expects($this->any())->method('getPrimarySiteLocaleId')->willReturn('nl_nl');
51
52
        return $mock;
53
    }
54
55
    /**
56
     * @return Mock|FieldLayoutModel
57
     */
58
    public function getMockFieldLayout()
59
    {
60
        $mock = $this->getMockBuilder(FieldLayoutModel::class)
61
            ->disableOriginalConstructor()
62
            ->getMock();
63
64
        return $mock;
65
    }
66
67
    /**
68
     * @return Mock|Fields
69
     */
70
    public function getMockSchematicFieldsService()
71
    {
72
        $mock = $this->getMockBuilder(Fields::class)
73
            ->disableOriginalConstructor()
74
            ->getMock();
75
76
        return $mock;
77
    }
78
79
    /**
80
     * @return FieldsService|Mock
81
     */
82
    public function getMockFieldsService()
83
    {
84
        $mock = $this->getMockBuilder(FieldsService::class)->getMock();
85
86
        return $mock;
87
    }
88
89
    /**
90
     * Test users service export.
91
     *
92
     * @covers ::export
93
     */
94
    public function testUsersServiceExport()
95
    {
96
        $mockI18n = $this->getMockLocalizationService();
97
98
        $mockFieldLayout = $this->getMockFieldLayout();
99
100
        $mockFieldsService = $this->getMockFieldsService();
101
        $mockFieldsService->expects($this->exactly(1))->method('getLayoutByType')->willReturn($mockFieldLayout);
102
103
        $mockSchematicFieldsService = $this->getMockSchematicFieldsService();
104
        $mockSchematicFieldsService->expects($this->exactly(1))->method('getFieldLayoutDefinition')->willReturn([]);
105
106
        $this->setComponent(Craft::app(), 'i18n', $mockI18n);
107
        $this->setComponent(Craft::app(), 'fields', $mockFieldsService);
108
        $this->setComponent(Craft::app(), 'schematic_fields', $mockSchematicFieldsService);
109
110
        $export = $this->schematicUsersService->export();
111
112
        $this->assertArrayHasKey('fieldLayout', $export);
113
    }
114
115
    /**
116
     * @param bool $safeLayout
117
     * @param bool $deleteLayoutsByType
118
     *
119
     * @return FieldsService|Mock
120
     */
121
    private function getMockFieldServiceForImport($safeLayout = true, $deleteLayoutsByType = true)
122
    {
123
        $mockFieldsService = $this->getMockFieldsService();
124
        $mockFieldsService->expects($this->exactly(1))->method('saveLayout')->willReturn($safeLayout);
125
        $mockFieldsService->expects($this->exactly(1))->method('deleteLayoutsByType')->willReturn($deleteLayoutsByType);
126
127
        return  $mockFieldsService;
128
    }
129
130
    /**
131
     * @param bool $errors
132
     *
133
     * @return Fields|Mock
134
     */
135
    private function getMockSchematicFieldServiceForImport($errors = false)
136
    {
137
        $mockFieldLayout = $this->getMockFieldLayout();
138
        if ($errors) {
139
            $mockFieldLayout->expects($this->exactly(1))->method('getAllErrors')->willReturn([
140
                'errors' => ['error 1', 'error 2', 'error 3'],
141
            ]);
142
        }
143
144
        $mockSchematicFieldsService = $this->getMockSchematicFieldsService();
145
        $mockSchematicFieldsService->expects($this->exactly(1))->method('getFieldLayout')->willReturn($mockFieldLayout);
146
147
        return $mockSchematicFieldsService;
148
    }
149
150
    /**
151
     * Test users service import.
152
     *
153
     * @covers ::import
154
     */
155
    public function testUsersServiceImportWithForce()
156
    {
157
        $mockFieldsService = $this->getMockFieldServiceForImport();
158
        $mockSchematicFieldsService = $this->getMockSchematicFieldServiceForImport();
159
160
        $this->setComponent(Craft::app(), 'fields', $mockFieldsService);
161
        $this->setComponent(Craft::app(), 'schematic_fields', $mockSchematicFieldsService);
162
163
        $import = $this->schematicUsersService->import(['fieldLayout' => []]);
164
165
        $this->assertTrue($import instanceof Result);
0 ignored issues
show
The class NerdsAndCompany\Schematic\Models\Result does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
166
    }
167
168
    /**
169
     * Test users service import.
170
     *
171
     * @covers ::import
172
     */
173
    public function testUsersServiceImportWithoutFieldLayout()
174
    {
175
        $mockFieldsService = $this->getMockFieldServiceForImport();
176
        $mockSchematicFieldsService = $this->getMockSchematicFieldServiceForImport();
177
178
        $this->setComponent(Craft::app(), 'fields', $mockFieldsService);
179
        $this->setComponent(Craft::app(), 'schematic_fields', $mockSchematicFieldsService);
180
181
        $import = $this->schematicUsersService->import([]);
182
183
        $this->assertTrue($import instanceof Result);
0 ignored issues
show
The class NerdsAndCompany\Schematic\Models\Result does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
184
    }
185
186
    /**
187
     * Test users service import.
188
     *
189
     * @covers ::import
190
     */
191
    public function testUsersServiceImportWithImportError()
192
    {
193
        $mockFieldsService = $this->getMockFieldServiceForImport(false);
194
        $mockSchematicFieldsService = $this->getMockSchematicFieldServiceForImport(true);
195
196
        $this->setComponent(Craft::app(), 'fields', $mockFieldsService);
197
        $this->setComponent(Craft::app(), 'schematic_fields', $mockSchematicFieldsService);
198
199
        $import = $this->schematicUsersService->import([]);
200
201
        $this->assertTrue($import instanceof Result);
0 ignored issues
show
The class NerdsAndCompany\Schematic\Models\Result does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
202
        $this->assertTrue($import->hasErrors('errors'));
203
    }
204
}
205