]>
Commit | Line | Data |
---|---|---|
3b815d2d J |
1 | <?php |
2 | ||
23634d5d | 3 | namespace Tests\Wallabag\CoreBundle; |
3b815d2d | 4 | |
7ab5eb95 | 5 | use Symfony\Bundle\FrameworkBundle\Client; |
6 | use Symfony\Bundle\FrameworkBundle\Console\Application; | |
3b815d2d | 7 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
fdc90ceb | 8 | use Symfony\Component\BrowserKit\Cookie; |
7ab5eb95 | 9 | use Symfony\Component\Console\Input\ArrayInput; |
2e1c1653 | 10 | use Symfony\Component\Console\Output\BufferedOutput; |
7ab5eb95 | 11 | use Wallabag\CoreBundle\Entity\Config; |
12 | use Wallabag\UserBundle\Entity\User; | |
3b815d2d | 13 | |
769e19dc | 14 | abstract class WallabagCoreTestCase extends WebTestCase |
3b815d2d | 15 | { |
7ab5eb95 | 16 | /** |
17 | * @var Client|null | |
18 | */ | |
3b815d2d J |
19 | private $client = null; |
20 | ||
3b815d2d J |
21 | public function setUp() |
22 | { | |
d5027625 JB |
23 | parent::setUp(); |
24 | ||
3b815d2d J |
25 | $this->client = static::createClient(); |
26 | } | |
27 | ||
300f293c KD |
28 | public function getNewClient() |
29 | { | |
30 | return $this->client = static::createClient(); | |
31 | } | |
32 | ||
f808b016 JB |
33 | public function getClient() |
34 | { | |
35 | return $this->client; | |
36 | } | |
37 | ||
7ab5eb95 | 38 | public function resetDatabase(Client $client) |
39 | { | |
40 | $application = new Application($client->getKernel()); | |
41 | $application->setAutoExit(false); | |
42 | ||
2e1c1653 JB |
43 | $output = new BufferedOutput(); |
44 | $exitCode = $application->run(new ArrayInput([ | |
7ab5eb95 | 45 | 'command' => 'doctrine:schema:drop', |
46 | '--no-interaction' => true, | |
47 | '--force' => true, | |
48 | '--env' => 'test', | |
2e1c1653 | 49 | ]), $output); |
7ab5eb95 | 50 | |
2e1c1653 JB |
51 | if (0 !== $exitCode) { |
52 | var_dump('doctrine:schema:drop'); | |
48c802da JB |
53 | var_export($output->fetch()); |
54 | die(); | |
2e1c1653 JB |
55 | } |
56 | ||
57 | $output = new BufferedOutput(); | |
58 | $exitCode = $application->run(new ArrayInput([ | |
7ab5eb95 | 59 | 'command' => 'doctrine:schema:create', |
60 | '--no-interaction' => true, | |
61 | '--env' => 'test', | |
2e1c1653 JB |
62 | ]), $output); |
63 | ||
64 | if (0 !== $exitCode) { | |
65 | var_dump('doctrine:schema:create'); | |
48c802da JB |
66 | var_export($output->fetch()); |
67 | die(); | |
2e1c1653 | 68 | } |
7ab5eb95 | 69 | |
2e1c1653 JB |
70 | $output = new BufferedOutput(); |
71 | $exitCode = $application->run(new ArrayInput([ | |
7ab5eb95 | 72 | 'command' => 'doctrine:fixtures:load', |
73 | '--no-interaction' => true, | |
74 | '--env' => 'test', | |
2e1c1653 JB |
75 | ]), $output); |
76 | ||
77 | if (0 !== $exitCode) { | |
78 | var_dump('doctrine:fixtures:load'); | |
48c802da JB |
79 | var_export($output->fetch()); |
80 | die(); | |
2e1c1653 | 81 | } |
7ab5eb95 | 82 | |
83 | /* | |
84 | * Recreate client to avoid error: | |
85 | * | |
86 | * [Doctrine\DBAL\ConnectionException] | |
87 | * Transaction commit failed because the transaction has been marked for rollback only. | |
88 | */ | |
89 | $this->client = static::createClient(); | |
90 | } | |
91 | ||
92 | public function getEntityManager() | |
93 | { | |
94 | return $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
95 | } | |
96 | ||
fdc90ceb JB |
97 | /** |
98 | * Login a user without making a HTTP request. | |
99 | * If we make a HTTP request we lose ability to mock service in the container. | |
100 | * | |
101 | * @param string $username User to log in | |
102 | */ | |
eb3bd7ef | 103 | public function logInAs($username) |
fdc90ceb JB |
104 | { |
105 | $container = $this->client->getContainer(); | |
106 | $session = $container->get('session'); | |
107 | ||
115de64e JB |
108 | $userManager = $container->get('fos_user.user_manager.test'); |
109 | $loginManager = $container->get('fos_user.security.login_manager.test'); | |
fdc90ceb JB |
110 | $firewallName = $container->getParameter('fos_user.firewall_name'); |
111 | ||
f808b016 | 112 | $user = $userManager->findUserBy(['username' => $username]); |
7ab5eb95 | 113 | $loginManager->logInUser($firewallName, $user); |
fdc90ceb | 114 | |
f808b016 | 115 | $session->set('_security_' . $firewallName, serialize($container->get('security.token_storage')->getToken())); |
fdc90ceb JB |
116 | $session->save(); |
117 | ||
118 | $cookie = new Cookie($session->getName(), $session->getId()); | |
119 | $this->client->getCookieJar()->set($cookie); | |
120 | } | |
121 | ||
122 | /** | |
123 | * Instead of `logInAs` this method use a HTTP request to log in the user. | |
124 | * Could be better for some tests. | |
125 | * | |
126 | * @param string $username User to log in | |
127 | */ | |
128 | public function logInAsUsingHttp($username) | |
3b815d2d J |
129 | { |
130 | $crawler = $this->client->request('GET', '/login'); | |
131 | $form = $crawler->filter('button[type=submit]')->form(); | |
4094ea47 | 132 | $data = [ |
eb3bd7ef | 133 | '_username' => $username, |
d9085c63 | 134 | '_password' => 'mypassword', |
4094ea47 | 135 | ]; |
3b815d2d J |
136 | |
137 | $this->client->submit($form, $data); | |
138 | } | |
78833672 JB |
139 | |
140 | /** | |
7ab5eb95 | 141 | * Return the user of the logged in user. |
78833672 JB |
142 | * You should be sure that you called `logInAs` before. |
143 | * | |
7ab5eb95 | 144 | * @return User |
78833672 | 145 | */ |
7ab5eb95 | 146 | public function getLoggedInUser() |
78833672 JB |
147 | { |
148 | $token = static::$kernel->getContainer()->get('security.token_storage')->getToken(); | |
149 | ||
150 | if (null !== $token) { | |
7ab5eb95 | 151 | return $token->getUser(); |
78833672 JB |
152 | } |
153 | ||
154 | throw new \RuntimeException('No logged in User.'); | |
155 | } | |
0e0102b6 | 156 | |
7ab5eb95 | 157 | /** |
158 | * Return the user id of the logged in user. | |
159 | * You should be sure that you called `logInAs` before. | |
160 | * | |
161 | * @return int | |
162 | */ | |
163 | public function getLoggedInUserId() | |
164 | { | |
165 | return $this->getLoggedInUser()->getId(); | |
166 | } | |
167 | ||
168 | public function useTheme($theme) | |
169 | { | |
170 | $config = $this->getEntityManager()->getRepository(Config::class)->findOneByUser($this->getLoggedInUser()); | |
171 | $config->setTheme($theme); | |
172 | $this->getEntityManager()->persist($config); | |
173 | $this->getEntityManager()->flush(); | |
174 | } | |
175 | ||
0e0102b6 JB |
176 | /** |
177 | * Check if Redis is installed. | |
084fb0d3 | 178 | * If not, mark test as skip. |
0e0102b6 JB |
179 | */ |
180 | protected function checkRedis() | |
181 | { | |
182 | try { | |
183 | $this->client->getContainer()->get('wallabag_core.redis.client')->connect(); | |
184 | } catch (\Exception $e) { | |
a0a4ce31 | 185 | $this->markTestSkipped('Redis is not installed/activated'); |
0e0102b6 JB |
186 | } |
187 | } | |
3b815d2d | 188 | } |