Completed
Push — master ( c8e4a8...b92c5e )
by Gaetano
10:34
created

CommandTest::fetchOutput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 16
rs 9.4285
cc 2
eloc 10
nc 2
nop 0
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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()
0 ignored issues
show
Coding Style introduced by
getContainer uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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