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 setUp()
25 $this->client
= static::createClient();
28 public function getNewClient()
30 return $this->client
= static::createClient();
33 public function getClient()
38 public function resetDatabase(Client
$client)
40 $application = new Application($client->getKernel());
41 $application->setAutoExit(false);
43 $application->run(new ArrayInput([
44 'command' => 'doctrine:schema:drop',
45 '--no-interaction' => true,
48 ]), new NullOutput());
50 $application->run(new ArrayInput([
51 'command' => 'doctrine:schema:create',
52 '--no-interaction' => true,
54 ]), new NullOutput());
56 $application->run(new ArrayInput([
57 'command' => 'doctrine:fixtures:load',
58 '--no-interaction' => true,
60 ]), new NullOutput());
63 * Recreate client to avoid error:
65 * [Doctrine\DBAL\ConnectionException]
66 * Transaction commit failed because the transaction has been marked for rollback only.
68 $this->client
= static::createClient();
71 public function getEntityManager()
73 return $this->client
->getContainer()->get('doctrine.orm.entity_manager');
77 * Login a user without making a HTTP request.
78 * If we make a HTTP request we lose ability to mock service in the container.
80 * @param string $username User to log in
82 public function logInAs($username)
84 $container = $this->client
->getContainer();
85 $session = $container->get('session');
87 $userManager = $container->get('fos_user.user_manager.test');
88 $loginManager = $container->get('fos_user.security.login_manager.test');
89 $firewallName = $container->getParameter('fos_user.firewall_name');
91 $user = $userManager->findUserBy(['username' => $username]);
92 $loginManager->logInUser($firewallName, $user);
94 $session->set('_security_' . $firewallName, serialize($container->get('security.token_storage')->getToken()));
97 $cookie = new Cookie($session->getName(), $session->getId());
98 $this->client
->getCookieJar()->set($cookie);
102 * Instead of `logInAs` this method use a HTTP request to log in the user.
103 * Could be better for some tests.
105 * @param string $username User to log in
107 public function logInAsUsingHttp($username)
109 $crawler = $this->client
->request('GET', '/login');
110 $form = $crawler->filter('button[type=submit]')->form();
112 '_username' => $username,
113 '_password' => 'mypassword',
116 $this->client
->submit($form, $data);
120 * Return the user of the logged in user.
121 * You should be sure that you called `logInAs` before.
125 public function getLoggedInUser()
127 $token = static::$kernel->getContainer()->get('security.token_storage')->getToken();
129 if (null !== $token) {
130 return $token->getUser();
133 throw new \
RuntimeException('No logged in User.');
137 * Return the user id of the logged in user.
138 * You should be sure that you called `logInAs` before.
142 public function getLoggedInUserId()
144 return $this->getLoggedInUser()->getId();
147 public function useTheme($theme)
149 $config = $this->getEntityManager()->getRepository(Config
::class)->findOneByUser($this->getLoggedInUser());
150 $config->setTheme($theme);
151 $this->getEntityManager()->persist($config);
152 $this->getEntityManager()->flush();
156 * Check if Redis is installed.
157 * If not, mark test as skip.
159 protected function checkRedis()
162 $this->client
->getContainer()->get('wallabag_core.redis.client')->connect();
163 } catch (\Exception
$e) {
164 $this->markTestSkipped('Redis is not installed/activated');