]>
Commit | Line | Data |
---|---|---|
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 getClient() | |
22 | { | |
23 | return $this->client; | |
24 | } | |
25 | ||
26 | public function setUp() | |
27 | { | |
28 | parent::setUp(); | |
29 | ||
30 | $this->client = static::createClient(); | |
31 | } | |
32 | ||
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 | ||
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 | */ | |
77 | public function logInAs($username) | |
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)); | |
87 | $loginManager->logInUser($firewallName, $user); | |
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) | |
103 | { | |
104 | $crawler = $this->client->request('GET', '/login'); | |
105 | $form = $crawler->filter('button[type=submit]')->form(); | |
106 | $data = [ | |
107 | '_username' => $username, | |
108 | '_password' => 'mypassword', | |
109 | ]; | |
110 | ||
111 | $this->client->submit($form, $data); | |
112 | } | |
113 | ||
114 | /** | |
115 | * Return the user of the logged in user. | |
116 | * You should be sure that you called `logInAs` before. | |
117 | * | |
118 | * @return User | |
119 | */ | |
120 | public function getLoggedInUser() | |
121 | { | |
122 | $token = static::$kernel->getContainer()->get('security.token_storage')->getToken(); | |
123 | ||
124 | if (null !== $token) { | |
125 | return $token->getUser(); | |
126 | } | |
127 | ||
128 | throw new \RuntimeException('No logged in User.'); | |
129 | } | |
130 | ||
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 | ||
150 | /** | |
151 | * Check if Redis is installed. | |
152 | * If not, mark test as skip. | |
153 | */ | |
154 | protected function checkRedis() | |
155 | { | |
156 | try { | |
157 | $this->client->getContainer()->get('wallabag_core.redis.client')->connect(); | |
158 | } catch (\Exception $e) { | |
159 | $this->markTestSkipped('Redis is not installed/activated'); | |
160 | } | |
161 | } | |
162 | } |