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