2015-02-08 23:05:51 +01:00
|
|
|
<?php
|
|
|
|
|
2016-06-01 21:27:35 +02:00
|
|
|
namespace Tests\Wallabag\CoreBundle;
|
2015-02-08 23:05:51 +01:00
|
|
|
|
2022-08-28 02:01:46 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2017-05-15 20:47:59 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application;
|
2023-08-08 01:21:56 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
|
2015-02-08 23:05:51 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
2016-06-24 11:55:45 +02:00
|
|
|
use Symfony\Component\BrowserKit\Cookie;
|
2017-05-15 20:47:59 +02:00
|
|
|
use Symfony\Component\Console\Input\ArrayInput;
|
|
|
|
use Symfony\Component\Console\Output\NullOutput;
|
2022-08-28 02:01:46 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
2017-05-15 20:47:59 +02:00
|
|
|
use Wallabag\UserBundle\Entity\User;
|
2015-02-08 23:05:51 +01:00
|
|
|
|
2015-03-29 10:53:10 +02:00
|
|
|
abstract class WallabagCoreTestCase extends WebTestCase
|
2015-02-08 23:05:51 +01:00
|
|
|
{
|
2017-05-15 20:47:59 +02:00
|
|
|
/**
|
2023-08-08 01:21:56 +02:00
|
|
|
* @var KernelBrowser|null
|
2017-05-15 20:47:59 +02:00
|
|
|
*/
|
2015-02-08 23:05:51 +01:00
|
|
|
private $client = null;
|
|
|
|
|
2020-12-08 09:17:10 +01:00
|
|
|
protected function setUp(): void
|
2015-02-08 23:05:51 +01:00
|
|
|
{
|
2023-08-06 14:48:53 +02:00
|
|
|
static::ensureKernelShutdown();
|
|
|
|
|
2015-11-06 22:08:51 +01:00
|
|
|
parent::setUp();
|
|
|
|
|
2015-02-08 23:05:51 +01:00
|
|
|
$this->client = static::createClient();
|
|
|
|
}
|
|
|
|
|
2017-11-27 22:56:46 +01:00
|
|
|
public function getNewClient()
|
|
|
|
{
|
2023-08-06 14:48:53 +02:00
|
|
|
static::ensureKernelShutdown();
|
|
|
|
|
2017-11-27 22:56:46 +01:00
|
|
|
return $this->client = static::createClient();
|
|
|
|
}
|
|
|
|
|
2022-11-23 17:09:32 +01:00
|
|
|
public function getTestClient()
|
2017-07-01 09:52:38 +02:00
|
|
|
{
|
|
|
|
return $this->client;
|
|
|
|
}
|
|
|
|
|
2023-08-08 01:21:56 +02:00
|
|
|
public function resetDatabase(KernelBrowser $client)
|
2017-05-15 20:47:59 +02:00
|
|
|
{
|
|
|
|
$application = new Application($client->getKernel());
|
|
|
|
$application->setAutoExit(false);
|
|
|
|
|
|
|
|
$application->run(new ArrayInput([
|
|
|
|
'command' => 'doctrine:schema:drop',
|
|
|
|
'--no-interaction' => true,
|
|
|
|
'--force' => true,
|
|
|
|
'--env' => 'test',
|
|
|
|
]), new NullOutput());
|
|
|
|
|
|
|
|
$application->run(new ArrayInput([
|
|
|
|
'command' => 'doctrine:schema:create',
|
|
|
|
'--no-interaction' => true,
|
|
|
|
'--env' => 'test',
|
|
|
|
]), new NullOutput());
|
|
|
|
|
|
|
|
$application->run(new ArrayInput([
|
|
|
|
'command' => 'doctrine:fixtures:load',
|
|
|
|
'--no-interaction' => true,
|
|
|
|
'--env' => 'test',
|
|
|
|
]), new NullOutput());
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recreate client to avoid error:
|
|
|
|
*
|
|
|
|
* [Doctrine\DBAL\ConnectionException]
|
|
|
|
* Transaction commit failed because the transaction has been marked for rollback only.
|
|
|
|
*/
|
|
|
|
$this->client = static::createClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEntityManager()
|
|
|
|
{
|
2022-08-28 02:01:46 +02:00
|
|
|
return $this->client->getContainer()->get(EntityManagerInterface::class);
|
2017-05-15 20:47:59 +02:00
|
|
|
}
|
|
|
|
|
2016-06-24 11:55:45 +02:00
|
|
|
/**
|
|
|
|
* Login a user without making a HTTP request.
|
|
|
|
* If we make a HTTP request we lose ability to mock service in the container.
|
|
|
|
*
|
|
|
|
* @param string $username User to log in
|
|
|
|
*/
|
2015-02-10 22:32:42 +01:00
|
|
|
public function logInAs($username)
|
2016-06-24 11:55:45 +02:00
|
|
|
{
|
|
|
|
$container = $this->client->getContainer();
|
2022-08-28 02:01:46 +02:00
|
|
|
$session = $container->get(SessionInterface::class);
|
2016-06-24 11:55:45 +02:00
|
|
|
|
2018-10-04 14:07:20 +02:00
|
|
|
$userManager = $container->get('fos_user.user_manager.test');
|
|
|
|
$loginManager = $container->get('fos_user.security.login_manager.test');
|
2016-06-24 11:55:45 +02:00
|
|
|
$firewallName = $container->getParameter('fos_user.firewall_name');
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$user = $userManager->findUserBy(['username' => $username]);
|
2020-12-10 10:30:34 +01:00
|
|
|
|
|
|
|
if (null === $user) {
|
|
|
|
throw new \Exception('Unable to find user "' . $username . '". Does fixtures were loaded?');
|
|
|
|
}
|
|
|
|
|
2017-05-15 20:47:59 +02:00
|
|
|
$loginManager->logInUser($firewallName, $user);
|
2016-06-24 11:55:45 +02:00
|
|
|
|
2022-08-28 02:01:46 +02:00
|
|
|
$session->set('_security_' . $firewallName, serialize($container->get(TokenStorageInterface::class)->getToken()));
|
2016-06-24 11:55:45 +02:00
|
|
|
$session->save();
|
|
|
|
|
|
|
|
$cookie = new Cookie($session->getName(), $session->getId());
|
|
|
|
$this->client->getCookieJar()->set($cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instead of `logInAs` this method use a HTTP request to log in the user.
|
|
|
|
* Could be better for some tests.
|
|
|
|
*
|
|
|
|
* @param string $username User to log in
|
|
|
|
*/
|
|
|
|
public function logInAsUsingHttp($username)
|
2015-02-08 23:05:51 +01:00
|
|
|
{
|
|
|
|
$crawler = $this->client->request('GET', '/login');
|
|
|
|
$form = $crawler->filter('button[type=submit]')->form();
|
2016-04-12 11:36:01 +02:00
|
|
|
$data = [
|
2015-02-10 22:32:42 +01:00
|
|
|
'_username' => $username,
|
2015-02-17 21:03:23 +01:00
|
|
|
'_password' => 'mypassword',
|
2016-04-12 11:36:01 +02:00
|
|
|
];
|
2015-02-08 23:05:51 +01:00
|
|
|
|
|
|
|
$this->client->submit($form, $data);
|
|
|
|
}
|
2016-01-15 15:28:22 +01:00
|
|
|
|
|
|
|
/**
|
2017-05-15 20:47:59 +02:00
|
|
|
* Return the user of the logged in user.
|
2016-01-15 15:28:22 +01:00
|
|
|
* You should be sure that you called `logInAs` before.
|
|
|
|
*
|
2017-05-15 20:47:59 +02:00
|
|
|
* @return User
|
2016-01-15 15:28:22 +01:00
|
|
|
*/
|
2017-05-15 20:47:59 +02:00
|
|
|
public function getLoggedInUser()
|
2016-01-15 15:28:22 +01:00
|
|
|
{
|
2022-08-28 02:01:46 +02:00
|
|
|
$token = static::$kernel->getContainer()->get(TokenStorageInterface::class)->getToken();
|
2016-01-15 15:28:22 +01:00
|
|
|
|
|
|
|
if (null !== $token) {
|
2017-05-15 20:47:59 +02:00
|
|
|
return $token->getUser();
|
2016-01-15 15:28:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new \RuntimeException('No logged in User.');
|
|
|
|
}
|
2016-09-24 19:57:59 +02:00
|
|
|
|
2017-05-15 20:47:59 +02:00
|
|
|
/**
|
|
|
|
* Return the user id of the logged in user.
|
|
|
|
* You should be sure that you called `logInAs` before.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getLoggedInUserId()
|
|
|
|
{
|
|
|
|
return $this->getLoggedInUser()->getId();
|
|
|
|
}
|
|
|
|
|
2016-09-24 19:57:59 +02:00
|
|
|
/**
|
|
|
|
* Check if Redis is installed.
|
2016-09-28 10:02:31 +02:00
|
|
|
* If not, mark test as skip.
|
2016-09-24 19:57:59 +02:00
|
|
|
*/
|
|
|
|
protected function checkRedis()
|
|
|
|
{
|
|
|
|
try {
|
2022-04-24 18:20:46 +02:00
|
|
|
$this->client->getContainer()->get(\Predis\Client::class)->connect();
|
2016-09-24 19:57:59 +02:00
|
|
|
} catch (\Exception $e) {
|
2016-10-11 18:59:08 +02:00
|
|
|
$this->markTestSkipped('Redis is not installed/activated');
|
2016-09-24 19:57:59 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-08 23:05:51 +01:00
|
|
|
}
|