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\NullOutput
;
11 use Wallabag\CoreBundle\Entity\Config
;
12 use Wallabag\UserBundle\Entity\User
;
14 abstract class WallabagCoreTestCase
extends WebTestCase
19 private $client = null;
21 public function getClient()
26 public function setUp()
30 $this->client
= static::createClient();
33 public function resetDatabase(Client
$client)
35 $application = new Application($client->getKernel());
36 $application->setAutoExit(false);
38 $application->run(new ArrayInput([
39 'command' => 'doctrine:schema:drop',
40 '--no-interaction' => true,
43 ]), new NullOutput());
45 $application->run(new ArrayInput([
46 'command' => 'doctrine:schema:create',
47 '--no-interaction' => true,
49 ]), new NullOutput());
51 $application->run(new ArrayInput([
52 'command' => 'doctrine:fixtures:load',
53 '--no-interaction' => true,
55 ]), new NullOutput());
58 * Recreate client to avoid error:
60 * [Doctrine\DBAL\ConnectionException]
61 * Transaction commit failed because the transaction has been marked for rollback only.
63 $this->client
= static::createClient();
66 public function getEntityManager()
68 return $this->client
->getContainer()->get('doctrine.orm.entity_manager');
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.
75 * @param string $username User to log in
77 public function logInAs($username)
79 $container = $this->client
->getContainer();
80 $session = $container->get('session');
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');
86 $user = $userManager->findUserBy(array('username' => $username));
87 $loginManager->logInUser($firewallName, $user);
89 $session->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken()));
92 $cookie = new Cookie($session->getName(), $session->getId());
93 $this->client
->getCookieJar()->set($cookie);
97 * Instead of `logInAs` this method use a HTTP request to log in the user.
98 * Could be better for some tests.
100 * @param string $username User to log in
102 public function logInAsUsingHttp($username)
104 $crawler = $this->client
->request('GET', '/login');
105 $form = $crawler->filter('button[type=submit]')->form();
107 '_username' => $username,
108 '_password' => 'mypassword',
111 $this->client
->submit($form, $data);
115 * Return the user of the logged in user.
116 * You should be sure that you called `logInAs` before.
120 public function getLoggedInUser()
122 $token = static::$kernel->getContainer()->get('security.token_storage')->getToken();
124 if (null !== $token) {
125 return $token->getUser();
128 throw new \
RuntimeException('No logged in User.');
132 * Return the user id of the logged in user.
133 * You should be sure that you called `logInAs` before.
137 public function getLoggedInUserId()
139 return $this->getLoggedInUser()->getId();
142 public function useTheme($theme)
144 $config = $this->getEntityManager()->getRepository(Config
::class)->findOneByUser($this->getLoggedInUser());
145 $config->setTheme($theme);
146 $this->getEntityManager()->persist($config);
147 $this->getEntityManager()->flush();
151 * Check if Redis is installed.
152 * If not, mark test as skip.
154 protected function checkRedis()
157 $this->client
->getContainer()->get('wallabag_core.redis.client')->connect();
158 } catch (\Exception
$e) {
159 $this->markTestSkipped('Redis is not installed/activated');