2015-02-08 22:05:51 +00:00
|
|
|
<?php
|
|
|
|
|
2016-06-01 19:27:35 +00:00
|
|
|
namespace Tests\Wallabag\CoreBundle;
|
2015-02-08 22:05:51 +00:00
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2017-05-15 18:47:59 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application;
|
2023-08-07 23:21:56 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
|
2015-02-08 22:05:51 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
2017-05-15 18:47:59 +00:00
|
|
|
use Symfony\Component\Console\Input\ArrayInput;
|
|
|
|
use Symfony\Component\Console\Output\NullOutput;
|
2022-08-28 00:01:46 +00:00
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
2017-05-15 18:47:59 +00:00
|
|
|
use Wallabag\UserBundle\Entity\User;
|
2015-02-08 22:05:51 +00:00
|
|
|
|
2015-03-29 08:53:10 +00:00
|
|
|
abstract class WallabagCoreTestCase extends WebTestCase
|
2015-02-08 22:05:51 +00:00
|
|
|
{
|
2017-05-15 18:47:59 +00:00
|
|
|
/**
|
2023-08-07 23:21:56 +00:00
|
|
|
* @var KernelBrowser|null
|
2017-05-15 18:47:59 +00:00
|
|
|
*/
|
2024-01-01 18:11:01 +00:00
|
|
|
private $client;
|
2015-02-08 22:05:51 +00:00
|
|
|
|
2020-12-08 08:17:10 +00:00
|
|
|
protected function setUp(): void
|
2015-02-08 22:05:51 +00:00
|
|
|
{
|
2023-08-06 12:48:53 +00:00
|
|
|
static::ensureKernelShutdown();
|
|
|
|
|
2015-11-06 21:08:51 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
2015-02-08 22:05:51 +00:00
|
|
|
$this->client = static::createClient();
|
|
|
|
}
|
|
|
|
|
2017-11-27 21:56:46 +00:00
|
|
|
public function getNewClient()
|
|
|
|
{
|
2023-08-06 12:48:53 +00:00
|
|
|
static::ensureKernelShutdown();
|
|
|
|
|
2017-11-27 21:56:46 +00:00
|
|
|
return $this->client = static::createClient();
|
|
|
|
}
|
|
|
|
|
2022-11-23 16:09:32 +00:00
|
|
|
public function getTestClient()
|
2017-07-01 07:52:38 +00:00
|
|
|
{
|
|
|
|
return $this->client;
|
|
|
|
}
|
|
|
|
|
2023-08-07 23:21:56 +00:00
|
|
|
public function resetDatabase(KernelBrowser $client)
|
2017-05-15 18:47:59 +00: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.
|
|
|
|
*/
|
2023-12-24 19:37:54 +00:00
|
|
|
$this->client = $this->getNewClient();
|
2017-05-15 18:47:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getEntityManager()
|
|
|
|
{
|
2022-08-28 00:01:46 +00:00
|
|
|
return $this->client->getContainer()->get(EntityManagerInterface::class);
|
2017-05-15 18:47:59 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 09:55:45 +00: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 21:32:42 +00:00
|
|
|
public function logInAs($username)
|
2016-06-24 09:55:45 +00:00
|
|
|
{
|
|
|
|
$container = $this->client->getContainer();
|
|
|
|
|
2018-10-04 12:07:20 +00:00
|
|
|
$userManager = $container->get('fos_user.user_manager.test');
|
2016-06-24 09:55:45 +00:00
|
|
|
$firewallName = $container->getParameter('fos_user.firewall_name');
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$user = $userManager->findUserBy(['username' => $username]);
|
2020-12-10 09:30:34 +00:00
|
|
|
|
|
|
|
if (null === $user) {
|
|
|
|
throw new \Exception('Unable to find user "' . $username . '". Does fixtures were loaded?');
|
|
|
|
}
|
|
|
|
|
2024-01-01 18:51:22 +00:00
|
|
|
$this->client->loginUser($user, $firewallName);
|
2016-06-24 09:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 22:05:51 +00:00
|
|
|
{
|
|
|
|
$crawler = $this->client->request('GET', '/login');
|
|
|
|
$form = $crawler->filter('button[type=submit]')->form();
|
2016-04-12 09:36:01 +00:00
|
|
|
$data = [
|
2015-02-10 21:32:42 +00:00
|
|
|
'_username' => $username,
|
2015-02-17 20:03:23 +00:00
|
|
|
'_password' => 'mypassword',
|
2016-04-12 09:36:01 +00:00
|
|
|
];
|
2015-02-08 22:05:51 +00:00
|
|
|
|
|
|
|
$this->client->submit($form, $data);
|
|
|
|
}
|
2016-01-15 14:28:22 +00:00
|
|
|
|
|
|
|
/**
|
2017-05-15 18:47:59 +00:00
|
|
|
* Return the user of the logged in user.
|
2016-01-15 14:28:22 +00:00
|
|
|
* You should be sure that you called `logInAs` before.
|
|
|
|
*
|
2017-05-15 18:47:59 +00:00
|
|
|
* @return User
|
2016-01-15 14:28:22 +00:00
|
|
|
*/
|
2017-05-15 18:47:59 +00:00
|
|
|
public function getLoggedInUser()
|
2016-01-15 14:28:22 +00:00
|
|
|
{
|
2022-08-28 00:01:46 +00:00
|
|
|
$token = static::$kernel->getContainer()->get(TokenStorageInterface::class)->getToken();
|
2016-01-15 14:28:22 +00:00
|
|
|
|
|
|
|
if (null !== $token) {
|
2023-08-08 01:27:21 +00:00
|
|
|
\assert($token->getUser() instanceof User);
|
|
|
|
|
2017-05-15 18:47:59 +00:00
|
|
|
return $token->getUser();
|
2016-01-15 14:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new \RuntimeException('No logged in User.');
|
|
|
|
}
|
2016-09-24 17:57:59 +00:00
|
|
|
|
2017-05-15 18:47:59 +00: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 17:57:59 +00:00
|
|
|
/**
|
|
|
|
* Check if Redis is installed.
|
2016-09-28 08:02:31 +00:00
|
|
|
* If not, mark test as skip.
|
2016-09-24 17:57:59 +00:00
|
|
|
*/
|
|
|
|
protected function checkRedis()
|
|
|
|
{
|
|
|
|
try {
|
2022-04-24 16:20:46 +00:00
|
|
|
$this->client->getContainer()->get(\Predis\Client::class)->connect();
|
2016-09-24 17:57:59 +00:00
|
|
|
} catch (\Exception $e) {
|
2016-10-11 16:59:08 +00:00
|
|
|
$this->markTestSkipped('Redis is not installed/activated');
|
2016-09-24 17:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-08 22:05:51 +00:00
|
|
|
}
|