Completed
Push — master ( 1ce8a3...77bf97 )
by Avtandil
02:04
created

RestrictsExtraAttributesTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 148
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
B it_should_throw_validation_error_on_extra_arguments() 0 28 6
B provideData() 0 95 1
A createValidator() 0 15 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tests\Unit\Http\Requests;
6
7
use Illuminate\Contracts\Translation\Translator;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Facades\Validator;
10
use Illuminate\Validation\ValidationException;
11
use Mockery;
12
use Tests\Unit\TestCase;
13
14
use function strpos;
15
16
class RestrictsExtraAttributesTest extends TestCase
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: artisan, be, call, seed
Loading history...
17
{
18
    /**
19
     * @test
20
     *
21
     * @dataProvider provideData
22
     */
23
    public function it_should_throw_validation_error_on_extra_arguments(
24
        array $rules,
25
        array $attributes,
26
        array $errorAttributes
27
    ): void {
28
        $formRequest = $this->createValidator($rules, $attributes);
29
30
        try {
31
            $formRequest->validateResolved();
32
        } catch (ValidationException $e) {
33
            if (! empty($errorAttributes)) {
34
                $errors = Arr::dot($e->errors());
0 ignored issues
show
Documentation introduced by
$e->errors() is of type array, but the function expects a object<Illuminate\Support\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
36
                foreach ($errorAttributes as $errorAttr) {
37
                    $found = false;
38
                    foreach ($errors as $error) {
39
                        if (strpos($error, $errorAttr) !== false) {
40
                            $found = true;
41
                            break;
42
                        }
43
                    }
44
                    $this->assertTrue($found, 'Failed asserting that validation errors contains "' . $errorAttr . '" string');
45
                }
46
            }
47
        }
48
49
        $this->assertInstanceOf(CustomRequest::class, $formRequest);
50
    }
51
52
    public function provideData(): array
53
    {
54
        return [
55
            [
56
                [
57
                    'field1' => 'required',
58
                    'field2' => 'required',
59
                ],
60
                [
61
                    'field1' => 'Some Data 1',
62
                    'field2' => 'Some Data 2',
63
                    'field3' => 'Some Data 3',
64
                ],
65
                [
66
                    'field3',
67
                ],
68
            ],
69
            [
70
                [
71
                    'field1'           => 'required',
72
                    'field2.subfield1' => 'required',
73
                    'field2.subfield2' => 'required',
74
                    'field2.subfield3' => 'required',
75
                ],
76
                [
77
                    'field1' => 'Some Data 1',
78
                    'field2' => [
79
                        'subfield1' => 'Some Data 2-1',
80
                        'subfield2' => 'Some Data 2-2',
81
                        'subfield3' => 'Some Data 2-3',
82
                        'subfield4' => 'Some Data 2-3',
83
                    ],
84
                    'field3' => 'Some Data 3',
85
                ],
86
                [
87
                    'field2.subfield4',
88
                    'field3',
89
                ],
90
            ],
91
            [
92
                [
93
                    'field1'             => 'required',
94
                    'field2.*.subfield1' => 'required',
95
                    'field2.*.subfield2' => 'required',
96
                    'field2.*.subfield3' => 'required',
97
                    'field3'             => 'required',
98
                    'field4'             => 'required',
99
                ],
100
                [
101
                    'field1' => 'Some Data 1',
102
                    'field2' => [
103
                        [
104
                            'subfield1' => 'Some Data 2-1',
105
                            'subfield2' => 'Some Data 2-2',
106
                        ],
107
                        [
108
                            'subfield3' => 'Some Data 2-3',
109
                            'subfield4' => 'Some Data 2-3',
110
                        ],
111
                    ],
112
                    'field3' => 'Some Data 3',
113
                    'field4' => 'Some Data 4',
114
                    'field5' => 'Some Data 3',
115
                ],
116
                [
117
                    'field2.*.subfield4',
118
                    'field5',
119
                ],
120
            ],
121
            [
122
                [
123
                    'field1'   => 'required',
124
                    'field2'   => 'required',
125
                    'field2.*' => 'required',
126
                ],
127
                [
128
                    'field1' => 'Some Data 1',
129
                    'field2' => [
130
                        [
131
                            'subfield1' => 'Some Data 2-1',
132
                            'subfield2' => 'Some Data 2-2',
133
                        ],
134
                        [
135
                            'subfield3' => 'Some Data 2-3',
136
                            'subfield4' => 'Some Data 2-3',
137
                        ],
138
                    ],
139
                    'field3' => 'Some Data 3',
140
                ],
141
                [
142
                    'field3',
143
                ],
144
            ],
145
        ];
146
    }
147
148
    private function createValidator(array $rules, array $attributes): CustomRequest
149
    {
150
        /** @var \Mockery\MockInterface|\Tests\Unit\Http\Requests\CustomRequest $formRequest */
151
        $formRequest = Mockery::mock(CustomRequest::class)->makePartial();
152
        $formRequest->shouldReceive('rules')->andReturn($rules);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Tests\Unit\Http\Requests\CustomRequest.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
153
        $formRequest->setContainer($this->app);
154
        $formRequest->initialize($attributes);
155
        $formRequest->setValidator(Validator::make($formRequest->all(), $rules));
156
157
        app(Translator::class)->addLines([
158
            'validation.restrict_extra_attributes' => 'The :attribute key is not allowed in the request body.',
159
        ], 'en');
160
161
        return $formRequest;
162
    }
163
}
164