diff options
author | Jérémy Benoist <j0k3r@users.noreply.github.com> | 2017-10-23 11:09:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-23 11:09:17 +0200 |
commit | 1953a872932a63792293b4aec087880265ba89f7 (patch) | |
tree | fd16599e737fcdaf193c933ef3ec4a4ee248b117 /tests/Wallabag/CoreBundle/WallabagCoreTestCase.php | |
parent | d83d25dadec2c38460a32d96f5d2903426fec9d3 (diff) | |
parent | 702f2d67d60ca963492b90dad74cb5f8dcc84e51 (diff) | |
download | wallabag-1953a872932a63792293b4aec087880265ba89f7.tar.gz wallabag-1953a872932a63792293b4aec087880265ba89f7.tar.zst wallabag-1953a872932a63792293b4aec087880265ba89f7.zip |
Merge pull request #3011 from wallabag/2.3
wallabag 2.3.0
Diffstat (limited to 'tests/Wallabag/CoreBundle/WallabagCoreTestCase.php')
-rw-r--r-- | tests/Wallabag/CoreBundle/WallabagCoreTestCase.php | 86 |
1 files changed, 76 insertions, 10 deletions
diff --git a/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php b/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php index 7bf4b43c..1eda5199 100644 --- a/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php +++ b/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php | |||
@@ -2,25 +2,72 @@ | |||
2 | 2 | ||
3 | namespace Tests\Wallabag\CoreBundle; | 3 | namespace Tests\Wallabag\CoreBundle; |
4 | 4 | ||
5 | use Symfony\Bundle\FrameworkBundle\Client; | ||
6 | use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
5 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | 7 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
6 | use Symfony\Component\BrowserKit\Cookie; | 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; | ||
7 | 13 | ||
8 | abstract class WallabagCoreTestCase extends WebTestCase | 14 | abstract class WallabagCoreTestCase extends WebTestCase |
9 | { | 15 | { |
16 | /** | ||
17 | * @var Client|null | ||
18 | */ | ||
10 | private $client = null; | 19 | private $client = null; |
11 | 20 | ||
21 | public function setUp() | ||
22 | { | ||
23 | parent::setUp(); | ||
24 | |||
25 | $this->client = static::createClient(); | ||
26 | } | ||
27 | |||
12 | public function getClient() | 28 | public function getClient() |
13 | { | 29 | { |
14 | return $this->client; | 30 | return $this->client; |
15 | } | 31 | } |
16 | 32 | ||
17 | public function setUp() | 33 | public function resetDatabase(Client $client) |
18 | { | 34 | { |
19 | parent::setUp(); | 35 | $application = new Application($client->getKernel()); |
20 | 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 | */ | ||
21 | $this->client = static::createClient(); | 63 | $this->client = static::createClient(); |
22 | } | 64 | } |
23 | 65 | ||
66 | public function getEntityManager() | ||
67 | { | ||
68 | return $this->client->getContainer()->get('doctrine.orm.entity_manager'); | ||
69 | } | ||
70 | |||
24 | /** | 71 | /** |
25 | * Login a user without making a HTTP request. | 72 | * Login a user without making a HTTP request. |
26 | * If we make a HTTP request we lose ability to mock service in the container. | 73 | * If we make a HTTP request we lose ability to mock service in the container. |
@@ -36,10 +83,10 @@ abstract class WallabagCoreTestCase extends WebTestCase | |||
36 | $loginManager = $container->get('fos_user.security.login_manager'); | 83 | $loginManager = $container->get('fos_user.security.login_manager'); |
37 | $firewallName = $container->getParameter('fos_user.firewall_name'); | 84 | $firewallName = $container->getParameter('fos_user.firewall_name'); |
38 | 85 | ||
39 | $user = $userManager->findUserBy(array('username' => $username)); | 86 | $user = $userManager->findUserBy(['username' => $username]); |
40 | $loginManager->loginUser($firewallName, $user); | 87 | $loginManager->logInUser($firewallName, $user); |
41 | 88 | ||
42 | $session->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken())); | 89 | $session->set('_security_' . $firewallName, serialize($container->get('security.token_storage')->getToken())); |
43 | $session->save(); | 90 | $session->save(); |
44 | 91 | ||
45 | $cookie = new Cookie($session->getName(), $session->getId()); | 92 | $cookie = new Cookie($session->getName(), $session->getId()); |
@@ -65,23 +112,42 @@ abstract class WallabagCoreTestCase extends WebTestCase | |||
65 | } | 112 | } |
66 | 113 | ||
67 | /** | 114 | /** |
68 | * Return the user id of the logged in user. | 115 | * Return the user of the logged in user. |
69 | * You should be sure that you called `logInAs` before. | 116 | * You should be sure that you called `logInAs` before. |
70 | * | 117 | * |
71 | * @return int | 118 | * @return User |
72 | */ | 119 | */ |
73 | public function getLoggedInUserId() | 120 | public function getLoggedInUser() |
74 | { | 121 | { |
75 | $token = static::$kernel->getContainer()->get('security.token_storage')->getToken(); | 122 | $token = static::$kernel->getContainer()->get('security.token_storage')->getToken(); |
76 | 123 | ||
77 | if (null !== $token) { | 124 | if (null !== $token) { |
78 | return $token->getUser()->getId(); | 125 | return $token->getUser(); |
79 | } | 126 | } |
80 | 127 | ||
81 | throw new \RuntimeException('No logged in User.'); | 128 | throw new \RuntimeException('No logged in User.'); |
82 | } | 129 | } |
83 | 130 | ||
84 | /** | 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 | /** | ||
85 | * Check if Redis is installed. | 151 | * Check if Redis is installed. |
86 | * If not, mark test as skip. | 152 | * If not, mark test as skip. |
87 | */ | 153 | */ |