|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
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 Symfony\Bundle\FrameworkBundle\Test; |
|
|
13 |
|
|
|
14 |
use Symfony\Bundle\FrameworkBundle\Client; |
|
|
15 |
use Symfony\Component\Finder\Finder; |
|
|
16 |
use Symfony\Component\HttpFoundation\Response; |
|
|
17 |
use Symfony\Component\HttpKernel\HttpKernelInterface; |
|
|
18 |
|
|
|
19 |
/** |
|
|
20 |
* WebTestCase is the base class for functional tests. |
|
|
21 |
* |
|
|
22 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
23 |
*/ |
|
|
24 |
abstract class WebTestCase extends \PHPUnit_Framework_TestCase |
|
|
25 |
{ |
|
|
26 |
static protected $class; |
|
|
27 |
static protected $kernel; |
|
|
28 |
|
|
|
29 |
/** |
|
|
30 |
* Creates a Client. |
|
|
31 |
* |
|
|
32 |
* @param array $options An array of options to pass to the createKernel class |
|
|
33 |
* @param array $server An array of server parameters |
|
|
34 |
* |
|
|
35 |
* @return Client A Client instance |
|
|
36 |
*/ |
|
|
37 |
static protected function createClient(array $options = array(), array $server = array()) |
|
|
38 |
{ |
|
|
39 |
static::$kernel = static::createKernel($options); |
|
|
40 |
static::$kernel->boot(); |
|
|
41 |
|
|
|
42 |
$client = static::$kernel->getContainer()->get('test.client'); |
|
|
43 |
$client->setServerParameters($server); |
|
|
44 |
|
|
|
45 |
return $client; |
|
|
46 |
} |
|
|
47 |
|
|
|
48 |
/** |
|
|
49 |
* Finds the directory where the phpunit.xml(.dist) is stored. |
|
|
50 |
* |
|
|
51 |
* If you run tests with the PHPUnit CLI tool, everything will work as expected. |
|
|
52 |
* If not, override this method in your test classes. |
|
|
53 |
* |
|
|
54 |
* @return string The directory where phpunit.xml(.dist) is stored |
|
|
55 |
*/ |
|
|
56 |
static protected function getPhpUnitXmlDir() |
|
|
57 |
{ |
|
|
58 |
if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) { |
|
|
59 |
throw new \RuntimeException('You must override the WebTestCase::createKernel() method.'); |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
$dir = static::getPhpUnitCliConfigArgument(); |
|
|
63 |
if ($dir === null && |
|
|
64 |
(file_exists(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml') || |
|
|
65 |
file_exists(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml.dist'))) { |
|
|
66 |
$dir = getcwd(); |
|
|
67 |
} |
|
|
68 |
|
|
|
69 |
// Can't continue |
|
|
70 |
if ($dir === null) { |
|
|
71 |
throw new \RuntimeException('Unable to guess the Kernel directory.'); |
|
|
72 |
} |
|
|
73 |
|
|
|
74 |
if (!is_dir($dir)) { |
|
|
75 |
$dir = dirname($dir); |
|
|
76 |
} |
|
|
77 |
|
|
|
78 |
return $dir; |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
/** |
|
|
82 |
* Finds the value of configuration flag from cli |
|
|
83 |
* |
|
|
84 |
* PHPUnit will use the last configuration argument on the command line, so this only returns |
|
|
85 |
* the last configuration argument |
|
|
86 |
* |
|
|
87 |
* @return string The value of the phpunit cli configuration option |
|
|
88 |
*/ |
|
|
89 |
static private function getPhpUnitCliConfigArgument() |
|
|
90 |
{ |
|
|
91 |
$dir = null; |
|
|
92 |
$reversedArgs = array_reverse($_SERVER['argv']); |
|
|
93 |
foreach ($reversedArgs as $argIndex=>$testArg) { |
|
|
94 |
if ($testArg === '-c' || $testArg === '--configuration') { |
|
|
95 |
$dir = realpath($reversedArgs[$argIndex - 1]); |
|
|
96 |
break; |
|
|
97 |
} elseif (strpos($testArg, '--configuration=') === 0) { |
|
|
98 |
$argPath = substr($testArg, strlen('--configuration=')); |
|
|
99 |
$dir = realpath($argPath); |
|
|
100 |
break; |
|
|
101 |
} |
|
|
102 |
} |
|
|
103 |
|
|
|
104 |
return $dir; |
|
|
105 |
} |
|
|
106 |
|
|
|
107 |
/** |
|
|
108 |
* Attempts to guess the kernel location. |
|
|
109 |
* |
|
|
110 |
* When the Kernel is located, the file is required. |
|
|
111 |
* |
|
|
112 |
* @return string The Kernel class name |
|
|
113 |
*/ |
|
|
114 |
static protected function getKernelClass() |
|
|
115 |
{ |
|
|
116 |
$dir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : static::getPhpUnitXmlDir(); |
|
|
117 |
|
|
|
118 |
$finder = new Finder(); |
|
|
119 |
$finder->name('*Kernel.php')->depth(0)->in($dir); |
|
|
120 |
if (!count($finder)) { |
|
|
121 |
throw new \RuntimeException('You must override the WebTestCase::createKernel() method.'); |
|
|
122 |
} |
|
|
123 |
|
|
|
124 |
$file = current(iterator_to_array($finder)); |
|
|
125 |
$class = $file->getBasename('.php'); |
|
|
126 |
|
|
|
127 |
require_once $file; |
|
|
128 |
|
|
|
129 |
return $class; |
|
|
130 |
} |
|
|
131 |
|
|
|
132 |
/** |
|
|
133 |
* Creates a Kernel. |
|
|
134 |
* |
|
|
135 |
* Available options: |
|
|
136 |
* |
|
|
137 |
* * environment |
|
|
138 |
* * debug |
|
|
139 |
* |
|
|
140 |
* @param array $options An array of options |
|
|
141 |
* |
|
|
142 |
* @return HttpKernelInterface A HttpKernelInterface instance |
|
|
143 |
*/ |
|
|
144 |
static protected function createKernel(array $options = array()) |
|
|
145 |
{ |
|
|
146 |
if (null === static::$class) { |
|
|
147 |
static::$class = static::getKernelClass(); |
|
|
148 |
} |
|
|
149 |
|
|
|
150 |
return new static::$class( |
|
|
151 |
isset($options['environment']) ? $options['environment'] : 'test', |
|
|
152 |
isset($options['debug']) ? $options['debug'] : true |
|
|
153 |
); |
|
|
154 |
} |
|
|
155 |
|
|
|
156 |
/** |
|
|
157 |
* Shuts the kernel down if it was used in the test. |
|
|
158 |
*/ |
|
|
159 |
protected function tearDown() |
|
|
160 |
{ |
|
|
161 |
if (null !== static::$kernel) { |
|
|
162 |
static::$kernel->shutdown(); |
|
|
163 |
} |
|
|
164 |
} |
|
|
165 |
} |