Completed
Push — master ( a49823...664dd1 )
by Philip
23:56
created

BaseAcceptanceTest::getFixtureLoader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace App\Tests\Acceptance;
4
5
use App\DataFixtures\ProxiedLoader;
6
use App\Entity\User;
7
use App\Tests\Integration\BaseIntegrationTest;
8
use Symfony\Bundle\FrameworkBundle\Client;
9
use Symfony\Component\BrowserKit\Cookie;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
12
13
abstract class BaseAcceptanceTest extends BaseIntegrationTest
14
{
15
    /**
16
     * @var Client
17
     */
18
    protected $client = null;
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected function setUp()
24
    {
25
        parent::setUp();
26
        $this->client = static::createClient();
27
    }
28
29
    /**
30
     * @param User $user
31
     */
32
    protected function logIn(User $user)
33
    {
34
        $session = $this->client->getContainer()->get('session');
35
36
        $firewall = 'main';
37
        $token = new UsernamePasswordToken($user, null, $firewall, $user->getRoles());
38
        $session->set('_security_' . $firewall, serialize($token));
39
        $session->save();
40
41
        $cookie = new Cookie($session->getName(), $session->getId());
42
        $this->client->getCookieJar()->set($cookie);
43
    }
44
45
    protected function logOut()
46
    {
47
        $session = $this->client->getContainer()->get('session');
48
        $session->clear();
49
        $session->save();
50
51
        $this->client->getCookieJar()->clear();
52
    }
53
54
    protected function getFixtureLoader(ContainerInterface $container, array $classNames)
55
    {
56
        $container = $this->getContainer();
57
        $loader = new ProxiedLoader($container->get('test.doctrine.fixtures.loader'));
58
        foreach ($classNames as $fixtureClass) {
59
            $loader->addByClass($fixtureClass);
60
        }
61
62
        return $loader;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $loader; (App\DataFixtures\ProxiedLoader) is incompatible with the return type of the parent method Liip\FunctionalTestBundl...tCase::getFixtureLoader of type Symfony\Bundle\DoctrineF...mon\DataFixtures\Loader.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
63
    }
64
}
65