1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the core-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\CoreBundle\Tests; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
15
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
16
|
|
|
use Exception; |
17
|
|
|
use Symfony\Bundle\FrameworkBundle\Client; |
18
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
19
|
|
|
use Symfony\Component\BrowserKit\Cookie; |
20
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
21
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
22
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
23
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
24
|
|
|
use TestKernel; |
25
|
|
|
use WBW\Bundle\CoreBundle\Tests\Fixtures\TestFixtures; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Abstract web test case. |
29
|
|
|
* |
30
|
|
|
* @author webeweb <https://github.com/webeweb/> |
31
|
|
|
* @package WBW\Bundle\CoreBundle\Tests |
32
|
|
|
* @abstract |
33
|
|
|
*/ |
34
|
|
|
abstract class AbstractWebTestCase extends WebTestCase { |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Client. |
38
|
|
|
* |
39
|
|
|
* @var Client |
40
|
|
|
*/ |
41
|
|
|
protected $client; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritDoc} |
45
|
|
|
*/ |
46
|
|
|
protected static function getKernelClass(): string { |
47
|
|
|
require_once getcwd() . "/Tests/Fixtures/app/TestKernel.php"; |
48
|
|
|
return TestKernel::class; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
*/ |
54
|
|
|
protected function setUp(): void { |
55
|
|
|
parent::setUp(); |
56
|
|
|
|
57
|
|
|
// Set a Client mock. |
58
|
|
|
$this->client = static::createClient(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
|
|
public static function setUpBeforeClass(): void { |
65
|
|
|
parent::setUpBeforeClass(); |
66
|
|
|
|
67
|
|
|
static::$kernel = static::createKernel(); |
68
|
|
|
|
69
|
|
|
// Clean the cache to avoid issues due to cache files. |
70
|
|
|
$filesystem = new Filesystem(); |
71
|
|
|
$filesystem->remove(static::$kernel->getCacheDir()); |
72
|
|
|
|
73
|
|
|
static::$kernel->boot(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set up a cookie. |
78
|
|
|
* |
79
|
|
|
* @param UserInterface|string $user The user. |
80
|
|
|
* @param array $roles The user roles. |
81
|
|
|
* @param string $firewallName The firewall name. |
82
|
|
|
* @param string $firewallContext The firewall context. |
83
|
|
|
* @return Cookie Returns the cookie. |
84
|
|
|
*/ |
85
|
|
|
protected function setUpCookie($user = "webeweb", array $roles = ["ROLE_SUPER_ADMIN"], string $firewallName = "main", string $firewallContext = "main"): Cookie { |
86
|
|
|
|
87
|
|
|
$token = new UsernamePasswordToken($user, null, $firewallName, $roles); |
88
|
|
|
|
89
|
|
|
/** @var SessionInterface $session */ |
90
|
|
|
$session = static::$kernel->getContainer()->get("session"); |
91
|
|
|
$session->set("_security_" . $firewallContext, serialize($token)); |
92
|
|
|
$session->save(); |
93
|
|
|
|
94
|
|
|
$cookie = new Cookie($session->getName(), $session->getId()); |
95
|
|
|
|
96
|
|
|
return $cookie; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set up the schema tool. |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
* @throws Exception Throws an exception if an error occurs. |
104
|
|
|
*/ |
105
|
|
|
protected static function setUpSchemaTool(): void { |
106
|
|
|
|
107
|
|
|
/** @var EntityManagerInterface $em */ |
108
|
|
|
$em = static::$kernel->getContainer()->get("doctrine.orm.entity_manager"); |
109
|
|
|
|
110
|
|
|
$entities = $em->getMetadataFactory()->getAllMetadata(); |
111
|
|
|
|
112
|
|
|
$schemaTool = new SchemaTool($em); |
113
|
|
|
$schemaTool->dropDatabase(); |
114
|
|
|
$schemaTool->createSchema($entities); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set up the user fixtures. |
119
|
|
|
* |
120
|
|
|
* @return void |
121
|
|
|
* @throws Exception Throws an exception if an error occurs. |
122
|
|
|
*/ |
123
|
|
|
protected static function setUpUserFixtures(): void { |
124
|
|
|
|
125
|
|
|
/** @var EntityManagerInterface $em */ |
126
|
|
|
$em = static::$kernel->getContainer()->get("doctrine.orm.entity_manager"); |
127
|
|
|
|
128
|
|
|
foreach (TestFixtures::getUsers() as $current) { |
129
|
|
|
$em->persist($current); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$em->flush(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritDoc} |
137
|
|
|
*/ |
138
|
|
|
protected function tearDown(): void { |
139
|
|
|
static::$kernel->shutdown(); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|