3 namespace Tests\Wallabag\CoreBundle
;
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\BufferedOutput
;
11 use Symfony\Component\Console\Output\NullOutput
;
12 use Wallabag\CoreBundle\Entity\Config
;
13 use Wallabag\UserBundle\Entity\User
;
15 abstract class WallabagCoreTestCase
extends WebTestCase
20 private $client = null;
22 public function setUp()
26 $this->client
= static::createClient();
29 public function getNewClient()
31 return $this->client
= static::createClient();
34 public function getClient()
39 public function resetDatabase(Client
$client)
41 $application = new Application($client->getKernel());
42 $application->setAutoExit(false);
44 $output = new BufferedOutput();
45 $exitCode = $application->run(new ArrayInput([
46 'command' => 'doctrine:schema:drop',
47 '--no-interaction' => true,
52 if (0 !== $exitCode) {
53 var_dump('doctrine:schema:drop');
54 var_export($output->fetch()); die();
57 $output = new BufferedOutput();
58 $exitCode = $application->run(new ArrayInput([
59 'command' => 'doctrine:schema:create',
60 '--no-interaction' => true,
64 if (0 !== $exitCode) {
65 var_dump('doctrine:schema:create');
66 var_export($output->fetch()); die();
69 $output = new BufferedOutput();
70 $exitCode = $application->run(new ArrayInput([
71 'command' => 'doctrine:fixtures:load',
72 '--no-interaction' => true,
76 if (0 !== $exitCode) {
77 var_dump('doctrine:fixtures:load');
78 var_export($output->fetch()); die();
82 * Recreate client to avoid error:
84 * [Doctrine\DBAL\ConnectionException]
85 * Transaction commit failed because the transaction has been marked for rollback only.
87 $this->client
= static::createClient();
90 public function getEntityManager()
92 return $this->client
->getContainer()->get('doctrine.orm.entity_manager');
96 * Login a user without making a HTTP request.
97 * If we make a HTTP request we lose ability to mock service in the container.
99 * @param string $username User to log in
101 public function logInAs($username)
103 $container = $this->client
->getContainer();
104 $session = $container->get('session');
106 $userManager = $container->get('fos_user.user_manager.test');
107 $loginManager = $container->get('fos_user.security.login_manager.test');
108 $firewallName = $container->getParameter('fos_user.firewall_name');
110 $user = $userManager->findUserBy(['username' => $username]);
111 $loginManager->logInUser($firewallName, $user);
113 $session->set('_security_' . $firewallName, serialize($container->get('security.token_storage')->getToken()));
116 $cookie = new Cookie($session->getName(), $session->getId());
117 $this->client
->getCookieJar()->set($cookie);
121 * Instead of `logInAs` this method use a HTTP request to log in the user.
122 * Could be better for some tests.
124 * @param string $username User to log in
126 public function logInAsUsingHttp($username)
128 $crawler = $this->client
->request('GET', '/login');
129 $form = $crawler->filter('button[type=submit]')->form();
131 '_username' => $username,
132 '_password' => 'mypassword',
135 $this->client
->submit($form, $data);
139 * Return the user of the logged in user.
140 * You should be sure that you called `logInAs` before.
144 public function getLoggedInUser()
146 $token = static::$kernel->getContainer()->get('security.token_storage')->getToken();
148 if (null !== $token) {
149 return $token->getUser();
152 throw new \
RuntimeException('No logged in User.');
156 * Return the user id of the logged in user.
157 * You should be sure that you called `logInAs` before.
161 public function getLoggedInUserId()
163 return $this->getLoggedInUser()->getId();
166 public function useTheme($theme)
168 $config = $this->getEntityManager()->getRepository(Config
::class)->findOneByUser($this->getLoggedInUser());
169 $config->setTheme($theme);
170 $this->getEntityManager()->persist($config);
171 $this->getEntityManager()->flush();
175 * Check if Redis is installed.
176 * If not, mark test as skip.
178 protected function checkRedis()
181 $this->client
->getContainer()->get('wallabag_core.redis.client')->connect();
182 } catch (\Exception
$e) {
183 $this->markTestSkipped('Redis is not installed/activated');