Skip to content
Snippets Groups Projects
Commit 0e46c11f authored by Camille Simiand's avatar Camille Simiand
Browse files

Add functional tests and refacto previous tests and UserFixtures

parent 3d606dda
Branches
Tags
1 merge request!32tuleap-50-create-a-capsule-for-an-unexisting-project-in-the-legacy
<?php
namespace App\DataFixtures;
use App\Entity\Capsule;
use App\Entity\User;
use DateTime;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Uid\Uuid;
class CapsuleFixtures extends Fixture implements DependentFixtureInterface
{
public function load(ObjectManager $manager): void
{
$user_repository = $manager->getRepository(User::class);
$verified_user1 = $user_repository->findOneBy(['email' => "defaultUser@localhost.com"]);
$verified_user2 = $user_repository->findOneBy(['email' => "defaultUser2@localhost.com"]);
$new_date_time = new DateTime();
$uuid_first_capsule = Uuid::v4();
$uuid_second_capsule = Uuid::v4();
$this->createCapsuleWithData(
$manager,
$verified_user1,
$new_date_time,
$uuid_first_capsule,
'Adele',
$uuid_first_capsule . '/?p=edit',
null,
$new_date_time
);
$this->createCapsuleWithData(
$manager,
$verified_user2,
new DateTime(),
$uuid_second_capsule,
'Pomme',
$uuid_second_capsule . '/?p=edit',
$verified_user1,
$new_date_time
);
$manager->flush();
}
private function createCapsuleWithData(
ObjectManager $manager,
User $author,
DateTime $date_time,
string $preview_link,
string $name,
string $edition_link,
?User $update_author,
DateTime $update_datetime
): void {
$capsule = new Capsule();
$capsule->setCreationAuthor($author);
$capsule->setCreationDate($date_time);
$capsule->setPreviewLink($preview_link);
$capsule->setEditionLink($edition_link);
$capsule->setName($name);
$capsule->setUpdateAuthor($update_author);
$capsule->setUpdatedDate($update_datetime);
$manager->persist($capsule);
}
public function getDependencies(): array
{
return [
UserFixtures::class,
];
}
}
......@@ -7,7 +7,7 @@ use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class AppUserRegistrationFixture extends Fixture
class UserFixtures extends Fixture
{
private UserPasswordHasherInterface $passwordHasher;
......@@ -18,28 +18,53 @@ class AppUserRegistrationFixture extends Fixture
public function load(ObjectManager $manager): void
{
$user = new User();
$user->setEmail("notVerified@localhost.com");
$user->setFirstName("Test");
$user->setLastName("Test LastName");
$user->setSalt("");
$user->setRoles(["ROLE_USER"]);
$password = $this->passwordHasher->hashPassword($user, 'password');
$user->setPassword($password);
$manager->persist($user);
$manager->flush($user);
$this->createUserWithData(
$manager,
"notVerified@localhost.com",
"Test",
"Test LastName",
"",
["ROLE_USER"]
);
$this->createUserWithData(
$manager,
"defaultUser@localhost.com",
"Test",
"Test LastName",
"",
["ROLE_USER"]
);
$this->createUserWithData(
$manager,
"defaultUser2@localhost.com",
"Test",
"Test LastName",
"",
["ROLE_USER"]
);
$manager->flush();
}
private function createUserWithData(
ObjectManager $manager,
string $email,
string $first_name,
string $lastname,
string $salt,
array $roles
): void {
$user = new User();
$user->setEmail("defaultUser@localhost.com");
$user->setFirstName("Test");
$user->setLastName("Test LastName");
$user->setSalt("");
$user->setRoles(["ROLE_USER"]);
$user->setIsVerified(true);
$user->setEmail($email);
$user->setFirstName($first_name);
$user->setLastName($lastname);
$user->setSalt($salt);
$user->setRoles($roles);
$password = $this->passwordHasher->hashPassword($user, 'password');
$user->setPassword($password);
$manager->persist($user);
$manager->flush();
$manager->persist($user);
}
}
......@@ -54,7 +54,7 @@ class Capsule
* @ORM\JoinColumn(name="aut_maj", referencedColumnName="id")
*
*/
private User $update_author;
private ?User $update_author;
/**
*
......@@ -145,7 +145,7 @@ class Capsule
return $this->update_author;
}
public function setUpdateAuthor(User $update_author): void
public function setUpdateAuthor(?User $update_author): void
{
$this->update_author = $update_author;
}
......
<?php
namespace App\Tests\functional;
use App\Entity\Capsule;
use App\Entity\User;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DomCrawler\Form;
use Symfony\Component\Filesystem\Filesystem;
class ProjectControllerTest extends WebTestCase
{
private KernelBrowser $client;
private $manager;
private Form $form;
private const TEST_DIR_PATH = __DIR__ . '/../../legacy/';
private const CAPSULE_NAME = 'TestCapsuleName';
protected function setUp(): void
{
chdir(self::TEST_DIR_PATH);
$this->client = static::createClient();
$this->manager = $this->client->getContainer()->get('doctrine')->getManager();
$user_repository = $this->manager->getRepository(User::class);
$verified_user = $user_repository->findOneByEmail('defaultUser@localhost.com');
$this->client->loginUser($verified_user);
$crawler = $this->client->request('GET', '/create');
$this->assertResponseIsSuccessful();
$this->client->enableProfiler();
$submit_button = $crawler->selectButton('Create a capsule');
$this->form = $submit_button->form();
}
protected function tearDown(): void
{
$file_system = new Filesystem();
$file_system->remove(self::TEST_DIR_PATH . self::CAPSULE_NAME);
}
public function testProjectDirectoryWithCorrespondingXMLFileIsCreatedWhenCapsuleCreationIsSuccessful(): void
{
$this->form['create_capsule_form[name]'] = self::CAPSULE_NAME;
$this->form['create_capsule_form[video_url]'] = "https://TestUrl";
$this->client->submit($this->form);
$this->assertResponseRedirects(
'/my_capsules',
302,
'Once the capsule is created, the user should be redirected to its capsules lists'
);
$this->client->followRedirect();
$capsule_repository = $this->manager->getRepository(Capsule::class);
$capsule_in_db = $capsule_repository->findOneByName(self::CAPSULE_NAME);
$capsule_name_in_db = $capsule_in_db->getName();
$this->assertResponseIsSuccessful('/my_capsules');
$this->assertDirectoryExists(self::CAPSULE_NAME);
$this->assertSame(self::CAPSULE_NAME, $capsule_name_in_db);
}
// public function testNoProjectDirectoryIsCreatedWhenCapsuleCreationFail(): void
// {
// $capsule_name = 'TestCapsuleName';
// $this->form['name'] = $capsule_name;
// $this->form['video_url'] = "https://TestUrl";
//
// $this->client->submit($this->form);
//
// $this->assertResponseRedirects(
// '/my_capsules',
// 302,
// 'Once the capsule is created, the user should be redirected to its capsules lists'
// );
//
// $this->client->followRedirect();
//
// $this->assertDirectoryDoesNotExist($capsule_name);
// }
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment