Base   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 10
dl 0
loc 57
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A resetDatabase() 0 8 1
A runCommand() 0 15 2
A assertInArray() 0 4 1
A assertNotInArray() 0 4 1
1
<?php
2
3
/**
4
 * @author    Markus Tacker <[email protected]>
5
 * @copyright 2013-2016 Verein zur Förderung der Netzkultur im Rhein-Main-Gebiet e.V. | http://netzkultur-rheinmain.de/
6
 */
7
8
namespace BCRM\WebBundle\Tests\Functional;
9
10
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Symfony\Bundle\FrameworkBundle\Console\Application;
13
use Symfony\Component\Console\Tester\CommandTester;
14
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\CreateSchemaDoctrineCommand;
15
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\DropSchemaDoctrineCommand;
16
use Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand;
17
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
18
use PhpOption\Option;
19
use Symfony\Component\Console\Command\Command;
20
21
abstract class Base extends WebTestCase
22
{
23
    protected static function resetDatabase()
24
    {
25
        $client    = static::createClient();
26
        $container = $client->getContainer();
27
        static::runCommand($container, new DropSchemaDoctrineCommand(), 'doctrine:schema:drop', array('--force' => true));
0 ignored issues
show
Bug introduced by
It seems like $container defined by $client->getContainer() on line 26 can be null; however, BCRM\WebBundle\Tests\Functional\Base::runCommand() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
28
        static::runCommand($container, new CreateSchemaDoctrineCommand(), 'doctrine:schema:create');
0 ignored issues
show
Bug introduced by
It seems like $container defined by $client->getContainer() on line 26 can be null; however, BCRM\WebBundle\Tests\Functional\Base::runCommand() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
29
        static::runCommand($container, new LoadDataFixturesDoctrineCommand(), 'doctrine:fixtures:load', array('--append' => true, '--fixtures' => './src/BCRM/BackendBundle/Tests/data/fixtures/'));
0 ignored issues
show
Bug introduced by
It seems like $container defined by $client->getContainer() on line 26 can be null; however, BCRM\WebBundle\Tests\Functional\Base::runCommand() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
30
    }
31
32
    /**
33
     * Executes a console command.
34
     *
35
     * @param ContainerInterface $container
36
     * @param Command            $command
37
     * @param string             $alias
38
     * @param array              $args
39
     */
40
    protected static function runCommand(ContainerInterface $container, Command $command, $alias, array $args = null)
41
    {
42
        $application = new Application(static::$kernel);
43
        $application->setAutoExit(false);
44
        $application->add($command);
45
46
        $command = $application->find($alias);
47
        if ($command instanceof ContainerAwareCommand) {
48
            $command->setContainer($container);
49
        }
50
51
        $commandTester = new CommandTester($command);
52
        $call          = array_merge(Option::fromValue($args)->getOrElse(array()), array('command' => $command->getName()));
53
        $commandTester->execute($call);
54
    }
55
56
    /**
57
     * Asserts that $needle is in $haystack.
58
     *
59
     * @param $needle
60
     * @param $haystack
61
     */
62
    protected function assertInArray($needle, $haystack)
63
    {
64
        $this->assertTrue(in_array($needle, $haystack), sprintf('Failed asserting that %s is not in array.', $needle));
65
    }
66
67
    /**
68
     * Asserts that $needle is not in $haystack.
69
     *
70
     * @param $needle
71
     * @param $haystack
72
     */
73
    protected function assertNotInArray($needle, $haystack)
74
    {
75
        $this->assertFalse(in_array($needle, $haystack), sprintf('Failed asserting that %s is not in array.', $needle));
76
    }
77
}
78