|
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; |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
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:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.