Completed
Push — master ( 2a6740...c8e4a8 )
by Gaetano
11:00
created

CommandTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
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\BufferedOutput;
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
    protected function setUp()
25
    {
26
        $this->container = $this->getContainer();
27
28
        $this->app = new Application(static::$kernel);
29
        $this->app->setAutoExit(false);
30
        $this->output = new BufferedOutput();
31
        $this->leftovers = array();
32
    }
33
34
    protected function tearDown()
35
    {
36
        foreach($this->leftovers as $file) {
37
            unlink($file);
38
        }
39
40
        // clean buffer, just in case...
41
        $this->output->fetch();
42
    }
43
44
    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...
45
    {
46
        if (null !== static::$kernel) {
47
            static::$kernel->shutdown();
48
        }
49
        // run in our own test environment. Sf by default uses the 'test' one. We let phpunit.xml set it...
50
        $options = array(
51
            'environment' => $_SERVER['SYMFONY_ENV']
52
        );
53
        static::$kernel = static::createKernel($options);
54
        static::$kernel->boot();
55
        return static::$kernel->getContainer();
56
    }
57
}
58