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