GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( a9bc98...b864c9 )
by butschster
10:50
created

NamedFormElementTest::test_resolving_path()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 0
rs 8.8571
1
<?php
2
3
use Mockery as m;
4
use SleepingOwl\Admin\Form\Element\NamedFormElement;
5
6
class NamedFormElementTest extends TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    /**
9
     * @param string $path
10
     * @param string|null $label
11
     *
12
     * @return NamedFormElement
13
     */
14
    protected function getElement($path = 'path', $label = null)
15
    {
16
        return $this->getMockForAbstractClass(NamedFormElement::class, [
17
            $path,
18
            $label,
19
        ]);
20
    }
21
22
    /**
23
     * @covers NamedFormElement::__constructor
24
     * @covers NamedFormElement::getLabel
25
     * @covers NamedFormElement::setLabel
26
     * @covers NamedFormElement::getPath
27
     * @covers NamedFormElement::setPath
28
     * @covers NamedFormElement::getName
29
     * @covers NamedFormElement::setName
30
     */
31
    public function test_constructor()
32
    {
33
        $element = $this->getElement($path = 'path.test.test1.tets2', $label = 'Label');
34
35
        $this->assertEquals($label, $element->getLabel());
36
        $this->assertEquals($path, $element->getPath());
37
        $this->assertEquals('path[test][test1][tets2]', $element->getName());
38
    }
39
40
    /**
41
     * @covers NamedFormElement::__constructor
42
     * @expectedException  \SleepingOwl\Admin\Exceptions\Form\FormElementException
43
     */
44
    public function test_constructor_exception()
45
    {
46
        $this->getElement(null);
47
    }
48
49
    /**
50
     * @covers NamedFormElement::setAttribute
51
     * @covers NamedFormElement::getAttribute
52
     */
53
    public function test_gets_and_sets_attribute()
54
    {
55
        $element = $this->getElement();
56
57
        $element->setAttribute('test');
58
        $this->assertEquals('test', $element->getAttribute());
59
    }
60
61
    /**
62
     * @covers NamedFormElement::setDefaultValue
63
     * @covers NamedFormElement::getDefaultValue
64
     */
65
    public function test_gets_and_sets_default_value()
66
    {
67
        $element = $this->getElement();
68
69
        $element->setDefaultValue('test');
70
        $this->assertEquals('test', $element->getDefaultValue());
71
    }
72
73
    /**
74
     * @covers NamedFormElement::setHelpText
75
     * @covers NamedFormElement::getHelpText
76
     */
77
    public function test_gets_and_sets_help_text()
78
    {
79
        $element = $this->getElement();
80
81
        $element->setHelpText('test');
82
        $this->assertEquals('test', $element->getHelpText());
83
84
        $helpText = m::mock(\Illuminate\Contracts\Support\Htmlable::class);
85
        $helpText->shouldReceive('toHtml')->once()->andReturn('html');
86
87
        $element->setHelpText($helpText);
88
        $this->assertEquals('html', $element->getHelpText());
89
    }
90
91
    /**
92
     * @covers NamedFormElement::required
93
     * @covers NamedFormElement::getValidationRules
94
     */
95
    public function test_add_required_rule()
96
    {
97
        $element = $this->getElement('key');
98
99
        $element->required();
100
        $this->assertEquals(['key' => ['required']], $element->getValidationRules());
101
    }
102
103
    /**
104
     * @covers NamedFormElement::required
105
     * @covers NamedFormElement::getValidationMessages
106
     */
107
    public function test_add_required_rule_with_message()
108
    {
109
        $element = $this->getElement('key');
110
111
        $element->required('required field');
112
113
        $this->assertEquals(['key.required' => 'required field'], $element->getValidationMessages());
114
    }
115
116
    /**
117
     * @covers NamedFormElement::required
118
     * @covers NamedFormElement::getValidationRules
119
     */
120
    public function test_add_unique_rule()
121
    {
122
        $element = $this->getElement('key');
123
124
        $model = m::mock(\Illuminate\Database\Eloquent\Model::class);
125
        $element->setModel($model);
126
127
        $model->shouldReceive('getTable')->once()->andReturn('test_table');
128
129
        $element->unique();
130
        $this->assertEquals(['key' => ['unique:test_table,key']], $element->getValidationRules());
131
    }
132
133
    /**
134
     * @covers NamedFormElement::unique
135
     * @covers NamedFormElement::getValidationMessages
136
     */
137
    public function test_add_unique_rule_with_message()
138
    {
139
        $element = $this->getElement('key');
140
141
        $element->unique('must be unique');
142
        $this->assertEquals(['key.unique' => 'must be unique'], $element->getValidationMessages());
143
    }
144
145
    /**
146
     * @covers NamedFormElement::addValidationMessage
147
     * @covers NamedFormElement::getValidationMessages
148
     */
149
    public function test_gets_validation_messages()
150
    {
151
        $element = $this->getElement('key');
152
153
        $element->addValidationMessage('test', 'test message');
154
        $element->addValidationMessage('hello', 'hello message');
155
        $this->assertEquals(['key.test' => 'test message', 'key.hello' => 'hello message'], $element->getValidationMessages());
156
    }
157
158
    /**
159
     * @covers NamedFormElement::getValidationLabels
160
     */
161
    public function test_gets_validation_labels()
162
    {
163
        $element = $this->getElement('key.subkey', 'Label');
164
165
        $this->assertEquals(['key.subkey' => 'Label'], $element->getValidationLabels());
166
    }
167
168
    /**
169
     * @covers NamedFormElement::getValueFromRequest
170
     */
171
    public function test_gets_session_value_from_request()
172
    {
173
        $request = $this->app['request'];
174
        $session = $request->getSession();
175
176
        $element = $this->getElement('key.subkey', 'Label');
177
        $session->shouldReceive('getOldInput')->andReturn('test');
178
179
        $this->assertEquals('test', $element->getValueFromRequest());
180
    }
181
182
    /**
183
     * @covers NamedFormElement::getValueFromRequest
184
     */
185 View Code Duplication
    public function test_gets_value_from_request()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
    {
187
        /** @var \Illuminate\Http\Request $request */
188
        $request = $this->app['request'];
189
        $request->offsetSet('key', [
190
            'subkey1' => 'hello world',
191
        ]);
192
193
        $session = $request->getSession();
194
195
        $element = $this->getElement('key.subkey1', 'Label');
196
        $session->shouldReceive('getOldInput')->andReturn(null);
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

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...
197
        $this->assertEquals('hello world', $element->getValueFromRequest());
198
    }
199
200
    /**
201
     * @covers NamedFormElement:;getValue
202
     */
203 View Code Duplication
    public function test_gets_value_with_request()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
    {
205
        $request = $this->app['request'];
206
207
        $session = $request->getSession();
208
        $session->shouldReceive('getOldInput')->andReturn(null);
209
210
        $element = $this->getElement('key.subkey', 'Label');
211
        $request->offsetSet('key', [
212
            'subkey' => 'hello world',
213
        ]);
214
215
        $this->assertEquals('hello world', $element->getValue());
216
    }
217
218
    /**
219
     * @covers NamedFormElement:;getValue
220
     */
221
    public function test_gets_value()
222
    {
223
        $request = $this->app['request'];
224
        $session = $request->getSession();
225
        $session->shouldReceive('getOldInput')->andReturn(null);
226
227
        $element = $this->getElement('key', 'Label');
228
229
        $this->assertNull($element->getValue());
230
231
        $element->setModel($model = m::mock(\Illuminate\Database\Eloquent\Model::class));
232
        $model->shouldReceive('getAttribute')->with('key')->andReturn('value');
233
234
        $this->assertNull($element->getValue());
235
236
        $model->exists = true;
0 ignored issues
show
Bug introduced by
Accessing exists on the interface Mockery\MockInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
237
        $this->assertEquals('value', $element->getValue());
238
    }
239
240
    /**
241
     * @covers NamedFormElement:;resolvePath
242
     */
243
    public function test_resolving_path()
244
    {
245
        $element = $this->getElement('key', 'Label');
246
        $element->setModel($model = m::mock(\Illuminate\Database\Eloquent\Model::class));
247
248
        $this->assertEquals($model, $element->resolvePath());
249
250
        // -------------
251
252
        $element = $this->getElement('key.subkey', 'Label');
253
        $element->setModel($model = m::mock(\Illuminate\Database\Eloquent\Model::class));
254
        $model->exists = true;
0 ignored issues
show
Bug introduced by
Accessing exists on the interface Mockery\MockInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
255
256
        $model->shouldReceive('getAttribute')->andReturn($subModel = m::mock(\Illuminate\Database\Eloquent\Model::class));
257
258
        $this->assertEquals($subModel, $element->resolvePath());
259
260
        // -------------
261
262
        $element = $this->getElement('key.subkey', 'Label');
263
        $element->setModel($model = new NamedFormElementTestModuleForTestingResolvePath());
264
265
        $this->assertInstanceOf(NamedFormElementTestModuleForTestingResolvePathBelongsTo::class, $element->resolvePath());
266
267
        // -------------
268
269
        $element = $this->getElement('key1.subkey', 'Label');
270
        $element->setModel($model = new NamedFormElementTestModuleForTestingResolvePath());
271
272
        $this->assertInstanceOf(NamedFormElementTestModuleForTestingResolvePathHasOne::class, $element->resolvePath());
273
274
        // -------------
275
276
        $element = $this->getElement('key2.subkey', 'Label');
277
        $element->setModel($model = new NamedFormElementTestModuleForTestingResolvePath());
278
279
        $this->assertInstanceOf(NamedFormElementTestModuleForTestingResolvePathHasMany::class, $element->resolvePath());
280
    }
281
282
    /**
283
     * @covers NamedFormElement::toArray
284
     */
285
    public function test_gets_array()
286
    {
287
        $element = $this->getElement('key2.subkey', 'Label');
288
289
        $request = $this->app['request'];
290
        $session = $request->getSession();
291
        $session->shouldReceive('getOldInput')->andReturn(null);
292
293
        $this->assertEquals([
294
            'value' => null,
295
            'readonly' => false,
296
            'model' => null,
297
            'id' => 'key2[subkey]',
298
            'name' => 'key2[subkey]',
299
            'path' => 'key2.subkey',
300
            'label' => 'Label',
301
            'helpText' => null,
302
            'required' => false,
303
        ], $element->toArray());
304
    }
305
306
    public function test_saving()
307
    {
308
        $this->markTestSkipped('NamedFormElement::save() not tested');
309
    }
310
}
311
312
class NamedFormElementTestModuleForTestingResolvePath extends \Illuminate\Database\Eloquent\Model
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
313
{
314
    public function key()
315
    {
316
        return $this->hasOne(NamedFormElementTestModuleForTestingResolvePathBelongsTo::class);
317
    }
318
319
    public function key1()
320
    {
321
        return $this->hasOne(NamedFormElementTestModuleForTestingResolvePathHasOne::class);
322
    }
323
324
    public function key2()
325
    {
326
        return $this->hasMany(NamedFormElementTestModuleForTestingResolvePathHasMany::class);
327
    }
328
}
329
330
class NamedFormElementTestModuleForTestingResolvePathBelongsTo extends \Illuminate\Database\Eloquent\Model
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
331
{
332
}
333
class NamedFormElementTestModuleForTestingResolvePathHasOne extends \Illuminate\Database\Eloquent\Model
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
334
{
335
}
336
class NamedFormElementTestModuleForTestingResolvePathHasMany extends \Illuminate\Database\Eloquent\Model
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
337
{
338
}
339