aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php
diff options
context:
space:
mode:
authoradev <adev2000@gmail.com>2017-05-15 20:47:59 +0200
committeradev <adev2000@gmail.com>2017-05-31 16:03:54 +0200
commit7ab5eb9508921d84b4b4ec84a59135d536da748e (patch)
tree725d9fae04d07a2c0aec2d2f5a19e1b43ec7a5d1 /tests/Wallabag/CoreBundle/WallabagCoreTestCase.php
parent4423b88c5b2c2d530b0a83a822f521a61ca4d4b8 (diff)
downloadwallabag-7ab5eb9508921d84b4b4ec84a59135d536da748e.tar.gz
wallabag-7ab5eb9508921d84b4b4ec84a59135d536da748e.tar.zst
wallabag-7ab5eb9508921d84b4b4ec84a59135d536da748e.zip
Isolated tests
Use https://github.com/dmaicher/doctrine-test-bundle to have test isolation.
Diffstat (limited to 'tests/Wallabag/CoreBundle/WallabagCoreTestCase.php')
-rw-r--r--tests/Wallabag/CoreBundle/WallabagCoreTestCase.php76
1 files changed, 71 insertions, 5 deletions
diff --git a/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php b/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php
index 7bf4b43c..eec6939d 100644
--- a/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php
+++ b/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php
@@ -2,11 +2,20 @@
2 2
3namespace Tests\Wallabag\CoreBundle; 3namespace Tests\Wallabag\CoreBundle;
4 4
5use Symfony\Bundle\FrameworkBundle\Client;
6use Symfony\Bundle\FrameworkBundle\Console\Application;
5use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; 7use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6use Symfony\Component\BrowserKit\Cookie; 8use Symfony\Component\BrowserKit\Cookie;
9use Symfony\Component\Console\Input\ArrayInput;
10use Symfony\Component\Console\Output\NullOutput;
11use Wallabag\CoreBundle\Entity\Config;
12use Wallabag\UserBundle\Entity\User;
7 13
8abstract class WallabagCoreTestCase extends WebTestCase 14abstract class WallabagCoreTestCase extends WebTestCase
9{ 15{
16 /**
17 * @var Client|null
18 */
10 private $client = null; 19 private $client = null;
11 20
12 public function getClient() 21 public function getClient()
@@ -21,6 +30,44 @@ abstract class WallabagCoreTestCase extends WebTestCase
21 $this->client = static::createClient(); 30 $this->client = static::createClient();
22 } 31 }
23 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
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.
@@ -37,7 +84,7 @@ abstract class WallabagCoreTestCase extends WebTestCase
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(array('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();
@@ -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 */