Completed
Push — master ( 1c5373...ea381d )
by Ross
9s
created

IntegrationTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createKernel() 0 6 1
A setUp() 0 10 1
A tearDown() 0 6 1
A givenConfig() 0 4 1
A registerService() 0 4 1
A handleCommand() 0 8 1
1
<?php
2
declare(strict_types=1);
3
4
namespace League\Tactician\Bundle\Tests\Integration;
5
6
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
7
use Symfony\Component\Filesystem\Filesystem;
8
use Symfony\Component\HttpKernel\Kernel;
9
use Symfony\Component\Yaml\Yaml;
10
11
abstract class IntegrationTest extends KernelTestCase
12
{
13
    /**
14
     * @var Kernel
15
     */
16
    protected static $kernel;
17
18
    /**
19
     * @var Filesystem
20
     */
21
    private $filesystem;
22
23
    protected static function createKernel(array $options = array())
24
    {
25
        require_once __DIR__.'/../testapp/AppKernel.php';
26
27
        return new \AppKernel('test', true);
28
    }
29
30
    protected function setUp()
31
    {
32
        static::$kernel = static::createKernel();
33
        $this->filesystem = new Filesystem();
34
35
        $cacheDir = sys_get_temp_dir().DIRECTORY_SEPARATOR.'tactician-bundle'.DIRECTORY_SEPARATOR.uniqid("tactician-bundle", true);
36
37
        $this->filesystem->mkdir($cacheDir);
38
        static::$kernel->defineCacheDir($cacheDir);
39
    }
40
41
    protected function tearDown()
42
    {
43
        $this->filesystem->remove(
44
            static::$kernel->getCacheDir()
45
        );
46
    }
47
48
    protected function givenConfig($namespace, $config)
49
    {
50
        static::$kernel->loadConfig($namespace, Yaml::parse((string) $config));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\HttpKernel\Kernel as the method loadConfig() does only exist in the following sub-classes of Symfony\Component\HttpKernel\Kernel: AppKernel. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
51
    }
52
53
    protected function registerService($serviceId, $className, array $tags)
54
    {
55
        static::$kernel->addServiceToRegister($serviceId, $className, $tags);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\HttpKernel\Kernel as the method addServiceToRegister() does only exist in the following sub-classes of Symfony\Component\HttpKernel\Kernel: AppKernel. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
56
    }
57
58
    protected function handleCommand($busId, $commandClass, array $args)
59
    {
60
        $class = new \ReflectionClass($commandClass);
61
        $command = $class->newInstanceArgs($args);
62
63
        static::$kernel->boot();
64
        static::$kernel->getContainer()->get('tactician.commandbus.'.$busId)->handle($command);
65
    }
66
}