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.
Passed
Push — master ( f5fadc...72d313 )
by Christian
02:28
created

AbstractFormHandlerTest::executePreProcess()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 0
loc 19
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\Form\Test;
13
14
use Core23\Form\Handler\FormHandlerInterface;
15
use PHPUnit\Framework\AssertionFailedError;
16
use PHPUnit\Framework\Constraint\Constraint;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Constraint\Constraint 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...
17
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase 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...
18
use Symfony\Component\Form\FormError;
19
use Symfony\Component\Form\FormInterface;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpFoundation\Session\Session;
23
24
abstract class AbstractFormHandlerTest extends TestCase
25
{
26
    protected $form;
27
28
    /**
29
     * @var Request
30
     */
31
    protected $request;
32
33
    protected $session;
34
35
    /**
36
     * @var array
37
     */
38
    private $errors;
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function setUp(): void
44
    {
45
        $this->form = $this->createMock(FormInterface::class);
46
47
        $this->session = $this->createMock(Session::class);
48
49
        $this->request = new Request();
50
        $this->request->setSession($this->session);
51
52
        $this->errors = [];
53
    }
54
55
    /**
56
     * @return FormHandlerInterface
57
     */
58
    abstract protected function createFormHandler(): FormHandlerInterface;
59
60
    /**
61
     * Executes the preProcess method.
62
     *
63
     * @param Request    $request
64
     * @param mixed|null $data
65
     *
66
     * @throws \ReflectionException
67
     *
68
     * @return Response|null
69
     */
70
    final protected function executePreProcess(Request $request, $data = null): ?Response
71
    {
72
        $handler = $this->createFormHandler();
73
74
        if (null !== $data) {
75
            $this->form->expects($this->once())->method('getData')
76
                ->will($this->returnValue($data));
77
        }
78
79
        $method = new \ReflectionMethod($handler, 'preProcess');
80
        $method->setAccessible(true);
81
82
        $this->checkCalledErrors();
83
84
        $result = $method->invoke($handler, $this->form, $request);
85
86
        $this->checkUncalledErrors();
87
88
        return $result;
89
    }
90
91
    /**
92
     * Executes the process method.
93
     *
94
     * @param Request    $request
95
     * @param mixed|null $data
96
     *
97
     * @throws \ReflectionException
98
     *
99
     * @return bool
100
     */
101
    final protected function executeProcess(Request $request, $data = null): bool
102
    {
103
        $handler = $this->createFormHandler();
104
105
        if (null !== $data) {
106
            $this->form->method('getData')
107
                ->will($this->returnValue($data));
108
        }
109
110
        $method = new \ReflectionMethod($handler, 'process');
111
        $method->setAccessible(true);
112
113
        $this->checkCalledErrors();
114
115
        $result = $method->invoke($handler, $this->form, $request);
116
117
        $this->checkUncalledErrors();
118
119
        return $result;
120
    }
121
122
    /**
123
     * Executes the postProcess method.
124
     *
125
     * @param Request    $request
126
     * @param Response   $response
127
     * @param mixed|null $data
128
     *
129
     * @throws \ReflectionException
130
     *
131
     * @return Response|null
132
     */
133
    final protected function executePostProcess(Request $request, Response $response, $data = null): ?Response
134
    {
135
        $handler = $this->createFormHandler();
136
137
        if (null !== $data) {
138
            $this->form->expects($this->once())->method('getData')
139
                ->will($this->returnValue($data));
140
        }
141
142
        $method = new \ReflectionMethod($handler, 'postProcess');
143
        $method->setAccessible(true);
144
145
        $this->checkCalledErrors();
146
147
        $result = $method->invoke($handler, $this->form, $request, $response);
148
149
        $this->checkUncalledErrors();
150
151
        return $result;
152
    }
153
154
    /**
155
     * Assets an error.
156
     *
157
     * @param string $message
158
     * @param array  $messageParameters
159
     */
160
    final protected function assertError(string $message, array $messageParameters = []): void
161
    {
162
        $this->errors[] = [
163
            'message'    => $message,
164
            'parameters' => $messageParameters,
165
            'count'      => 0,
166
        ];
167
    }
168
169
    /**
170
     * @return Constraint
171
     */
172
    private function equalToErrors(): Constraint
173
    {
174
        return $this->callback(function ($error) {
175
            if ($error instanceof FormError) {
176
                foreach ($this->errors as &$data) {
177
                    if ($error->getMessage() === $data['message'] && $error->getMessageParameters() === $data['parameters']) {
178
                        ++$data['count'];
179
180
                        return true;
181
                    }
182
                }
183
184
                throw new AssertionFailedError(
185
                    sprintf("Method 'addError' was not expected to be called with message '%s'", $error->getMessage())
186
                );
187
            }
188
189
            return false;
190
        });
191
    }
192
193
    private function checkCalledErrors(): void
194
    {
195
        $count = \count($this->errors);
196
        if (0 === $count) {
197
            $this->form->expects($this->never())->method('addError');
198
        } else {
199
            $this->form->expects($this->exactly($count))->method('addError')
200
                ->with($this->equalToErrors());
201
        }
202
    }
203
204
    /**
205
     * @throws AssertionFailedError
206
     */
207
    private function checkUncalledErrors(): void
208
    {
209
        foreach ($this->errors as $data) {
210
            if (0 === $data['count']) {
211
                throw new AssertionFailedError(
212
                    sprintf("Method 'addError' was expected to be called with message '%s' actually was not called", $data['message'])
213
                );
214
            }
215
        }
216
    }
217
}
218