Complex classes like WebTestCase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WebTestCase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | abstract class WebTestCase extends BaseWebTestCase |
||
| 43 | { |
||
| 44 | protected $environment = 'test'; |
||
| 45 | |||
| 46 | protected $containers; |
||
| 47 | |||
| 48 | // 5 * 1024 * 1024 KB |
||
| 49 | protected $maxMemory = 5242880; |
||
| 50 | |||
| 51 | // RUN COMMAND |
||
| 52 | protected $verbosityLevel; |
||
| 53 | |||
| 54 | protected $decorated; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array|null |
||
| 58 | */ |
||
| 59 | private $inputs = null; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private $firewallLogins = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Creates a mock object of a service identified by its id. |
||
| 68 | * |
||
| 69 | * @param string $id |
||
| 70 | * |
||
| 71 | * @return MockBuilder |
||
| 72 | */ |
||
| 73 | protected function getServiceMockBuilder(string $id): MockBuilder |
||
| 74 | { |
||
| 75 | $service = $this->getContainer()->get($id); |
||
| 76 | $class = get_class($service); |
||
| 77 | |||
| 78 | return $this->getMockBuilder($class)->disableOriginalConstructor(); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Builds up the environment to run the given command. |
||
| 83 | * |
||
| 84 | * @param string $name |
||
| 85 | * @param array $params |
||
| 86 | * @param bool $reuseKernel |
||
| 87 | * |
||
| 88 | * @return CommandTester |
||
| 89 | */ |
||
| 90 | 13 | protected function runCommand(string $name, array $params = [], bool $reuseKernel = false): CommandTester |
|
| 91 | { |
||
| 92 | 13 | if (!$reuseKernel) { |
|
| 93 | 13 | if (null !== static::$kernel) { |
|
| 94 | 1 | static::$kernel->shutdown(); |
|
| 95 | } |
||
| 96 | |||
| 97 | 13 | $kernel = static::$kernel = static::createKernel(['environment' => $this->environment]); |
|
| 98 | 13 | $kernel->boot(); |
|
| 99 | } else { |
||
| 100 | 2 | $kernel = $this->getContainer()->get('kernel'); |
|
| 101 | } |
||
| 102 | |||
| 103 | 13 | $application = new Application($kernel); |
|
|
|
|||
| 104 | |||
| 105 | $options = [ |
||
| 106 | 13 | 'interactive' => false, |
|
| 107 | 13 | 'decorated' => $this->getDecorated(), |
|
| 108 | 13 | 'verbosity' => $this->getVerbosityLevel(), |
|
| 109 | ]; |
||
| 110 | |||
| 111 | 12 | $command = $application->find($name); |
|
| 112 | 12 | $commandTester = new CommandTester($command); |
|
| 113 | |||
| 114 | 12 | if (null !== $inputs = $this->getInputs()) { |
|
| 115 | 1 | $commandTester->setInputs($inputs); |
|
| 116 | 1 | $options['interactive'] = true; |
|
| 117 | 1 | $this->inputs = null; |
|
| 118 | } |
||
| 119 | |||
| 120 | 12 | $commandTester->execute( |
|
| 121 | 12 | array_merge(['command' => $command->getName()], $params), |
|
| 122 | 12 | $options |
|
| 123 | ); |
||
| 124 | |||
| 125 | 12 | return $commandTester; |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Retrieves the output verbosity level. |
||
| 130 | * |
||
| 131 | * @see \Symfony\Component\Console\Output\OutputInterface for available levels |
||
| 132 | * |
||
| 133 | * @throws \OutOfBoundsException If the set value isn't accepted |
||
| 134 | * |
||
| 135 | * @return int |
||
| 136 | */ |
||
| 137 | 13 | protected function getVerbosityLevel(): int |
|
| 162 | |||
| 163 | 6 | public function setVerbosityLevel($level): void |
|
| 167 | |||
| 168 | 1 | protected function setInputs(array $inputs): void |
|
| 172 | |||
| 173 | 12 | protected function getInputs(): ?array |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Set verbosity for Symfony 3.4+. |
||
| 180 | * |
||
| 181 | * @see https://github.com/symfony/symfony/pull/24425 |
||
| 182 | * |
||
| 183 | * @param $level |
||
| 184 | */ |
||
| 185 | private function setVerbosityLevelEnv($level): void |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Retrieves the flag indicating if the output should be decorated or not. |
||
| 192 | * |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | 13 | protected function getDecorated(): bool |
|
| 209 | |||
| 210 | 6 | public function isDecorated(bool $decorated): void |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Get an instance of the dependency injection container. |
||
| 217 | * (this creates a kernel *without* parameters). |
||
| 218 | * |
||
| 219 | * @return ContainerInterface |
||
| 220 | */ |
||
| 221 | 15 | protected function getContainer(): ContainerInterface |
|
| 222 | { |
||
| 223 | 15 | $cacheKey = $this->environment; |
|
| 224 | 15 | if (empty($this->containers[$cacheKey])) { |
|
| 225 | $options = [ |
||
| 226 | 15 | 'environment' => $this->environment, |
|
| 227 | ]; |
||
| 228 | 15 | $kernel = $this->createKernel($options); |
|
| 229 | 15 | $kernel->boot(); |
|
| 230 | |||
| 231 | 15 | $container = $kernel->getContainer(); |
|
| 232 | 15 | if ($container->has('test.service_container')) { |
|
| 233 | 15 | $this->containers[$cacheKey] = $container->get('test.service_container'); |
|
| 234 | } else { |
||
| 235 | $this->containers[$cacheKey] = $container; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | 15 | return $this->containers[$cacheKey]; |
|
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Creates an instance of a lightweight Http client. |
||
| 244 | * |
||
| 245 | * $params can be used to pass headers to the client, note that they have |
||
| 246 | * to follow the naming format used in $_SERVER. |
||
| 247 | * Example: 'HTTP_X_REQUESTED_WITH' instead of 'X-Requested-With' |
||
| 248 | * |
||
| 249 | * @param array $params |
||
| 250 | * |
||
| 251 | * @return Client |
||
| 252 | */ |
||
| 253 | 30 | protected function makeClient(array $params = []): Client |
|
| 254 | { |
||
| 255 | 30 | return $this->createClientWithParams($params); |
|
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Creates an instance of a lightweight Http client. |
||
| 260 | * |
||
| 261 | * $params can be used to pass headers to the client, note that they have |
||
| 262 | * to follow the naming format used in $_SERVER. |
||
| 263 | * Example: 'HTTP_X_REQUESTED_WITH' instead of 'X-Requested-With' |
||
| 264 | * |
||
| 265 | * @param array $params |
||
| 266 | * |
||
| 267 | * @return Client |
||
| 268 | */ |
||
| 269 | 1 | protected function makeAuthenticatedClient(array $params = []): Client |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Creates an instance of a lightweight Http client and log in user with |
||
| 281 | * username and password params. |
||
| 282 | * |
||
| 283 | * $params can be used to pass headers to the client, note that they have |
||
| 284 | * to follow the naming format used in $_SERVER. |
||
| 285 | * Example: 'HTTP_X_REQUESTED_WITH' instead of 'X-Requested-With' |
||
| 286 | * |
||
| 287 | * @param string $username |
||
| 288 | * @param string $password |
||
| 289 | * @param array $params |
||
| 290 | * |
||
| 291 | * @return Client |
||
| 292 | */ |
||
| 293 | 1 | protected function makeClientWithCredentials(string $username, string $password, array $params = []): Client |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Create User Token. |
||
| 300 | * |
||
| 301 | * Factory method for creating a User Token object for the firewall based on |
||
| 302 | * the user object provided. By default it will be a Username/Password |
||
| 303 | * Token based on the user's credentials, but may be overridden for custom |
||
| 304 | * tokens in your applications. |
||
| 305 | * |
||
| 306 | * @param UserInterface $user The user object to base the token off of |
||
| 307 | * @param string $firewallName name of the firewall provider to use |
||
| 308 | * |
||
| 309 | * @return TokenInterface The token to be used in the security context |
||
| 310 | */ |
||
| 311 | 3 | protected function createUserToken(UserInterface $user, string $firewallName): TokenInterface |
|
| 312 | { |
||
| 313 | 3 | return new UsernamePasswordToken( |
|
| 314 | 3 | $user, |
|
| 315 | 3 | null, |
|
| 316 | 3 | $firewallName, |
|
| 317 | 3 | $user->getRoles() |
|
| 318 | ); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Extracts the location from the given route. |
||
| 323 | * |
||
| 324 | * @param string $route The name of the route |
||
| 325 | * @param array $params Set of parameters |
||
| 326 | * @param int $absolute |
||
| 327 | * |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | 1 | protected function getUrl(string $route, array $params = [], int $absolute = UrlGeneratorInterface::ABSOLUTE_PATH): string |
|
| 331 | { |
||
| 332 | 1 | return $this->getContainer()->get('router')->generate($route, $params, $absolute); |
|
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Checks the success state of a response. |
||
| 337 | * |
||
| 338 | * @param Response $response Response object |
||
| 339 | * @param bool $success to define whether the response is expected to be successful |
||
| 340 | * @param string $type |
||
| 341 | */ |
||
| 342 | 6 | public function isSuccessful(Response $response, $success = true, $type = 'text/html'): void |
|
| 343 | { |
||
| 344 | 6 | HttpAssertions::isSuccessful($response, $success, $type); |
|
| 345 | 5 | } |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Executes a request on the given url and returns the response contents. |
||
| 349 | * |
||
| 350 | * This method also asserts the request was successful. |
||
| 351 | * |
||
| 352 | * @param string $path path of the requested page |
||
| 353 | * @param string $method The HTTP method to use, defaults to GET |
||
| 354 | * @param bool $authentication Whether to use authentication, defaults to false |
||
| 355 | * @param bool $success to define whether the response is expected to be successful |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | 1 | public function fetchContent(string $path, string $method = 'GET', bool $authentication = false, bool $success = true): string |
|
| 360 | { |
||
| 361 | 1 | $client = ($authentication) ? $this->makeAuthenticatedClient() : $this->makeClient(); |
|
| 362 | |||
| 363 | 1 | $client->request($method, $path); |
|
| 364 | |||
| 365 | 1 | $content = $client->getResponse()->getContent(); |
|
| 366 | 1 | $this->isSuccessful($client->getResponse(), $success); |
|
| 367 | |||
| 368 | 1 | return $content; |
|
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Executes a request on the given url and returns a Crawler object. |
||
| 373 | * |
||
| 374 | * This method also asserts the request was successful. |
||
| 375 | * |
||
| 376 | * @param string $path path of the requested page |
||
| 377 | * @param string $method The HTTP method to use, defaults to GET |
||
| 378 | * @param bool $authentication Whether to use authentication, defaults to false |
||
| 379 | * @param bool $success Whether the response is expected to be successful |
||
| 380 | * |
||
| 381 | * @return Crawler |
||
| 382 | */ |
||
| 383 | 1 | public function fetchCrawler(string $path, string $method = 'GET', bool $authentication = false, bool $success = true): Crawler |
|
| 384 | { |
||
| 385 | 1 | $client = ($authentication) ? $this->makeAuthenticatedClient() : $this->makeClient(); |
|
| 386 | |||
| 387 | 1 | $crawler = $client->request($method, $path); |
|
| 388 | |||
| 389 | 1 | $this->isSuccessful($client->getResponse(), $success); |
|
| 390 | |||
| 391 | 1 | return $crawler; |
|
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param UserInterface $user |
||
| 396 | * @param string $firewallName |
||
| 397 | * |
||
| 398 | * @return WebTestCase |
||
| 399 | */ |
||
| 400 | 2 | public function loginAs(UserInterface $user, string $firewallName): self |
|
| 401 | { |
||
| 402 | 2 | @trigger_error(sprintf('"%s()" is deprecated, use loginClient() after creating a client.', __METHOD__), E_USER_DEPRECATED); |
|
| 403 | |||
| 404 | 2 | $this->firewallLogins[$firewallName] = $user; |
|
| 405 | |||
| 406 | 2 | return $this; |
|
| 407 | } |
||
| 408 | |||
| 409 | 1 | public function loginClient(KernelBrowser $client, UserInterface $user, string $firewallName): void |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Asserts that the HTTP response code of the last request performed by |
||
| 435 | * $client matches the expected code. If not, raises an error with more |
||
| 436 | * information. |
||
| 437 | * |
||
| 438 | * @param int $expectedStatusCode |
||
| 439 | * @param Client $client |
||
| 440 | */ |
||
| 441 | 13 | public static function assertStatusCode(int $expectedStatusCode, Client $client): void |
|
| 442 | { |
||
| 443 | 13 | HttpAssertions::assertStatusCode($expectedStatusCode, $client); |
|
| 444 | 10 | } |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Assert that the last validation errors within $container match the |
||
| 448 | * expected keys. |
||
| 449 | * |
||
| 450 | * @param array $expected A flat array of field names |
||
| 451 | * @param ContainerInterface $container |
||
| 452 | */ |
||
| 453 | 4 | public static function assertValidationErrors(array $expected, ContainerInterface $container): void |
|
| 454 | { |
||
| 455 | 4 | HttpAssertions::assertValidationErrors($expected, $container); |
|
| 456 | 2 | } |
|
| 457 | |||
| 458 | 48 | protected function tearDown(): void |
|
| 472 | |||
| 473 | 32 | protected function createClientWithParams(array $params, ?string $username = null, ?string $password = null): Client |
|
| 512 | } |
||
| 513 |
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: