1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
4
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
5
|
|
|
use Symfony\Component\Console\Output\StreamOutput; |
6
|
|
|
|
7
|
|
|
abstract class CommandTest extends WebTestCase |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
protected $dslDir; |
10
|
|
|
protected $targetBundle = 'EzPublishCoreBundle'; // it is always present :-) |
11
|
|
|
protected $leftovers = array(); |
12
|
|
|
|
13
|
|
|
protected $container; |
14
|
|
|
protected $app; |
15
|
|
|
protected $output; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
public function __construct($name = null, array $data = array(), $dataName = '') |
19
|
|
|
{ |
20
|
|
|
parent::__construct($name, $data, $dataName); |
21
|
|
|
// seems like this can not be used outside of the constructor... |
22
|
|
|
$this->dslDir = __DIR__ . '/../dsl'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function setUp() |
26
|
|
|
{ |
27
|
|
|
$this->container = $this->getContainer(); |
28
|
|
|
|
29
|
|
|
$this->app = new Application(static::$kernel); |
30
|
|
|
$this->app->setAutoExit(false); |
31
|
|
|
$fp = fopen('php://temp', 'r+'); |
32
|
|
|
$this->output = new StreamOutput($fp); |
33
|
|
|
$this->leftovers = array(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Fetches the data from the output buffer, resetting it. |
38
|
|
|
* It would be nice to use BufferedOutput, but that is not available in Sf 2.3... |
39
|
|
|
* @return null|string |
40
|
|
|
*/ |
41
|
|
|
protected function fetchOutput() |
42
|
|
|
{ |
43
|
|
|
if (!$this->output) { |
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$fp = $this->output->getStream(); |
48
|
|
|
rewind($fp); |
49
|
|
|
$out = stream_get_contents($fp); |
50
|
|
|
|
51
|
|
|
fclose($fp); |
52
|
|
|
$fp = fopen('php://temp', 'r+'); |
53
|
|
|
$this->output = new StreamOutput($fp); |
54
|
|
|
|
55
|
|
|
return $out; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function tearDown() |
59
|
|
|
{ |
60
|
|
|
foreach($this->leftovers as $file) { |
61
|
|
|
unlink($file); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// clean buffer, just in case... |
65
|
|
|
$fp = $this->output->getStream(); |
66
|
|
|
fclose($fp); |
67
|
|
|
$this->output = null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function getContainer() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
if (null !== static::$kernel) { |
73
|
|
|
static::$kernel->shutdown(); |
74
|
|
|
} |
75
|
|
|
// run in our own test environment. Sf by default uses the 'test' one. We let phpunit.xml set it... |
76
|
|
|
$options = array( |
77
|
|
|
'environment' => $_SERVER['SYMFONY_ENV'] |
78
|
|
|
); |
79
|
|
|
static::$kernel = static::createKernel($options); |
80
|
|
|
static::$kernel->boot(); |
81
|
|
|
return static::$kernel->getContainer(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.