Skip to content

Commit e64321c

Browse files
authored
Encapsulate the Client (#140)
1 parent 3e43a76 commit e64321c

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

src/Codeception/Module/Symfony.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
use Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector;
3232
use Symfony\Component\DependencyInjection\ContainerInterface;
3333
use Symfony\Component\Finder\Finder;
34-
use Symfony\Component\HttpFoundation\Response;
3534
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
3635
use Symfony\Component\HttpKernel\DataCollector\TimeDataCollector;
3736
use Symfony\Component\HttpKernel\Kernel;
@@ -149,6 +148,11 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
149148
*/
150149
public $kernel;
151150

151+
/**
152+
* @var SymfonyConnector
153+
*/
154+
public $client;
155+
152156
public $config = [
153157
'app_path' => 'app',
154158
'kernel_class' => 'App\Kernel',
@@ -279,6 +283,11 @@ public function _getContainer(): ContainerInterface
279283
return $container;
280284
}
281285

286+
protected function getClient(): SymfonyConnector
287+
{
288+
return $this->client ?: $this->fail('Client is not initialized');
289+
}
290+
282291
/**
283292
* Attempts to guess the kernel location.
284293
* When the Kernel is located, the file is required.
@@ -340,8 +349,7 @@ protected function getProfile(): ?Profile
340349
return null;
341350
}
342351
try {
343-
/** @var Response $response */
344-
$response = $this->client->getResponse();
352+
$response = $this->getClient()->getResponse();
345353
return $profiler->loadProfileFromResponse($response);
346354
} catch (BadMethodCallException $e) {
347355
$this->fail('You must perform a request before using this method.');

src/Codeception/Module/Symfony/BrowserAssertionsTrait.php

+6-10
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
namespace Codeception\Module\Symfony;
66

7-
use Codeception\Lib\Connector\Symfony as SymfonyConnector;
8-
use Symfony\Component\HttpFoundation\Response;
7+
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsSuccessful;
98
use function sprintf;
109

1110
trait BrowserAssertionsTrait
@@ -28,9 +27,7 @@ trait BrowserAssertionsTrait
2827
*/
2928
public function rebootClientKernel(): void
3029
{
31-
if ($this->client instanceof SymfonyConnector) {
32-
$this->client->rebootKernel();
33-
}
30+
$this->getClient()->rebootKernel();
3431
}
3532

3633
/**
@@ -53,7 +50,7 @@ public function seePageIsAvailable(string $url = null): void
5350
$this->amOnPage($url);
5451
$this->seeInCurrentUrl($url);
5552
}
56-
$this->seeResponseCodeIsSuccessful();
53+
$this->assertThat($this->getClient()->getResponse(), new ResponseIsSuccessful());
5754
}
5855

5956
/**
@@ -69,14 +66,13 @@ public function seePageIsAvailable(string $url = null): void
6966
*/
7067
public function seePageRedirectsTo(string $page, string $redirectsTo): void
7168
{
72-
$this->client->followRedirects(false);
69+
$this->getClient()->followRedirects(false);
7370
$this->amOnPage($page);
74-
/** @var Response $response */
75-
$response = $this->client->getResponse();
71+
$response = $this->getClient()->getResponse();
7672
$this->assertTrue(
7773
$response->isRedirection()
7874
);
79-
$this->client->followRedirect();
75+
$this->getClient()->followRedirect();
8076
$this->seeInCurrentUrl($redirectsTo);
8177
}
8278

src/Codeception/Module/Symfony/RouterAssertionsTrait.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Codeception\Module\Symfony;
66

7-
use Symfony\Component\HttpFoundation\Request;
87
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
98
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
109
use Symfony\Component\Routing\RouterInterface;
@@ -101,8 +100,7 @@ public function seeCurrentActionIs(string $action): void
101100
foreach ($routes as $route) {
102101
$controller = $route->getDefault('_controller');
103102
if (substr_compare($controller, $action, -strlen($action)) === 0) {
104-
/** @var Request $request */
105-
$request = $this->client->getRequest();
103+
$request = $this->getClient()->getRequest();
106104
$currentActionFqcn = $request->attributes->get('_controller');
107105

108106
$this->assertStringEndsWith($action, $currentActionFqcn, "Current action is '{$currentActionFqcn}'.");

0 commit comments

Comments
 (0)