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