Skip to content

Commit d38cab9

Browse files
committed
Added real Nette support via symfony/browser-kit connector
1 parent 8711fa5 commit d38cab9

File tree

3 files changed

+113
-12
lines changed

3 files changed

+113
-12
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"autoload": {
2424
"psr-0": {
25-
"Codeception\\Module": "src/"
25+
"Codeception": "src/"
2626
}
2727
}
2828
}

src/Codeception/Module/Nette.php

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
namespace Codeception\Module;
44

5+
use Codeception\TestCase;
6+
use Codeception\Util\Connector\Nette as NetteConnector;
57
use Codeception\Util\Framework;
68
use Nette\Configurator;
7-
use Nette\InvalidStateException;
89
use Nette\DI\Container;
910
use Nette\DI\MissingServiceException;
11+
use Nette\Diagnostics\Debugger;
12+
use Nette\Environment;
13+
use Nette\InvalidStateException;
1014
use Nette\Loaders\RobotLoader;
1115
use Nette\Utils\Validators;
1216
use RecursiveDirectoryIterator;
@@ -18,12 +22,18 @@
1822
class Nette extends Framework
1923
{
2024

25+
/** @var Configurator */
26+
protected $configurator;
27+
2128
/** @var Container */
2229
protected $container;
2330

2431
/** @var RobotLoader */
2532
private $robotLoader;
2633

34+
/** @var string */
35+
private $suite;
36+
2737
/**
2838
* @var array $config
2939
*/
@@ -49,35 +59,63 @@ public function _beforeSuite($settings = array())
4959
{
5060
parent::_beforeSuite($settings);
5161

52-
$suite = $this->detectSuiteName($settings);
53-
$tempDir = $this->config['tempDir'] . DIRECTORY_SEPARATOR . $suite;
62+
$this->detectSuiteName($settings);
63+
$path = pathinfo($settings['path'], PATHINFO_DIRNAME);
64+
$tempDir = $path . DIRECTORY_SEPARATOR . '_temp' . DIRECTORY_SEPARATOR . $this->suite;
65+
Debugger::$logDirectory = $path . DIRECTORY_SEPARATOR . '_log';
5466

5567
self::purge($tempDir);
56-
$configurator = new Configurator();
57-
$configurator->setTempDirectory($tempDir);
58-
$configurator->addParameters(array(
68+
$this->configurator = new Configurator();
69+
$this->configurator->setDebugMode(FALSE);
70+
$this->configurator->setTempDirectory($tempDir);
71+
$this->configurator->addParameters(array(
5972
'container' => array(
60-
'class' => ucfirst($suite) . 'SuiteContainer',
73+
'class' => $this->getContainerClass(),
6174
),
6275
));
76+
6377
$files = $this->config['configFiles'];
6478
$files[] = __DIR__ . '/config.neon';
6579
foreach ($files as $file) {
66-
$configurator->addConfig($file);
80+
$this->configurator->addConfig($file);
6781
}
68-
$this->robotLoader = $configurator->createRobotLoader();
82+
83+
$this->robotLoader = $this->configurator->createRobotLoader();
6984
foreach ($this->config['robotLoader'] as $dir) {
7085
$this->robotLoader->addDirectory($dir);
7186
}
7287
$this->robotLoader->register();
73-
$this->container = $configurator->createContainer();
7488
}
7589

7690
public function _afterSuite()
7791
{
7892
$this->robotLoader->unregister();
7993
}
8094

95+
public function _before(TestCase $test)
96+
{
97+
$class = $this->getContainerClass();
98+
if (!class_exists($class, FALSE)) {
99+
$this->container = $this->configurator->createContainer();
100+
} else {
101+
$this->container = new $class;
102+
$this->container->initialize();
103+
Environment::setContext($this->container);
104+
}
105+
$this->client = new NetteConnector();
106+
$this->client->setContainer($this->container);
107+
parent::_before($test);
108+
}
109+
110+
public function _after(TestCase $test)
111+
{
112+
parent::_after($test);
113+
$_SESSION = array();
114+
$_GET = array();
115+
$_POST = array();
116+
$_COOKIE = array();
117+
}
118+
81119
/**
82120
* @param string $service
83121
* @return object
@@ -101,7 +139,12 @@ private function detectSuiteName($settings)
101139
if ($position === FALSE) {
102140
throw new InvalidStateException('Could not detect suite name, path is invalid.');
103141
}
104-
return substr($directory, $position + 1);
142+
$this->suite = substr($directory, $position + 1);
143+
}
144+
145+
private function getContainerClass()
146+
{
147+
return ucfirst($this->suite) . 'SuiteContainer';
105148
}
106149

107150
/**
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Codeception\Util\Connector;
4+
5+
use Nette\DI\Container;
6+
use Nette\Diagnostics\Debugger;
7+
use Symfony\Component\BrowserKit\Client;
8+
use Symfony\Component\BrowserKit\Request;
9+
use Symfony\Component\BrowserKit\Response;
10+
11+
/**
12+
* @author Jáchym Toušek
13+
*/
14+
class Nette extends Client
15+
{
16+
17+
/** @var Container */
18+
protected $container;
19+
20+
public function setContainer(Container $container)
21+
{
22+
$this->container = $container;
23+
}
24+
25+
/**
26+
* @param Request $request
27+
* @return Response
28+
*/
29+
public function doRequest($request)
30+
{
31+
$_COOKIE = $request->getCookies();
32+
$_SERVER = $request->getServer();
33+
$_FILES = $request->getFiles();
34+
35+
$uri = str_replace('http://localhost', '', $request->getUri());
36+
37+
$_SERVER['REQUEST_METHOD'] = strtoupper($request->getMethod());
38+
$_SERVER['REQUEST_URI'] = $uri;
39+
40+
ob_start();
41+
try {
42+
$this->container->getByType('Nette\Application\Application')->run();
43+
} catch (Exception $e) {
44+
ob_end_clean();
45+
Debugger::log($e);
46+
throw $e;
47+
}
48+
$content = ob_get_clean();
49+
50+
$httpResponse = $this->container->getByType('Nette\Http\IResponse');
51+
$code = $httpResponse->getCode();
52+
$headers = $httpResponse->getHeaders();
53+
54+
$repsonse = new Response($content, $code, $headers);
55+
return $repsonse;
56+
}
57+
58+
}

0 commit comments

Comments
 (0)