From 0b1d7dea922f2f2df9f289e13005ab4f79d04812 Mon Sep 17 00:00:00 2001 From: "Kevin Veen-Birkenbach [aka. Frantz]" Date: Fri, 10 Apr 2020 14:49:26 +0200 Subject: [PATCH] Implemented tests for user login --- .../src/DataFixtures/DummyFixtures.php | 15 ++- .../tests/Functional/UserLoginTest.php | 95 +++++++++++++++++++ 2 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 application/symfony/tests/Functional/UserLoginTest.php diff --git a/application/symfony/src/DataFixtures/DummyFixtures.php b/application/symfony/src/DataFixtures/DummyFixtures.php index 7526c17..1b4b38a 100644 --- a/application/symfony/src/DataFixtures/DummyFixtures.php +++ b/application/symfony/src/DataFixtures/DummyFixtures.php @@ -17,8 +17,17 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class DummyFixtures extends Fixture implements ContainerAwareInterface { + /** + * @var ContainerInterface + */ private $container; + public const USER_NAME = 'test'; + + public const USER_EMAIL = 'test@test.de'; + + public const USER_PASSWORD = 'test'; + public function setContainer(ContainerInterface $container = null) { $this->container = $container; @@ -40,9 +49,9 @@ class DummyFixtures extends Fixture implements ContainerAwareInterface * @var User */ $testUser = $userManager->createUser(); - $testUser->setEmail('test@test.de'); - $testUser->setUsername('test'); - $testUser->setPlainPassword('test'); + $testUser->setEmail(self::USER_EMAIL); + $testUser->setUsername(self::USER_NAME); + $testUser->setPlainPassword(self::USER_PASSWORD); $testUser->setEnabled(true); $userManager->updateUser($testUser); diff --git a/application/symfony/tests/Functional/UserLoginTest.php b/application/symfony/tests/Functional/UserLoginTest.php new file mode 100644 index 0000000..02610d1 --- /dev/null +++ b/application/symfony/tests/Functional/UserLoginTest.php @@ -0,0 +1,95 @@ +client = $this->createClient(); + $this->crawler = $this->client->request('GET', '/login'); + } + + public function testIfLoginFormPageIsReachable(): void + { + $this->assertEquals(200, $this->client->getResponse() + ->getStatusCode()); + } + + /** + * @todo Implemnt test for success message + */ + public function testSuccessfullLogin(): void + { + $this->client->followRedirects(); + $form = $this->crawler->selectButton('Log in')->form(); + $form['_username']->setValue(DummyFixtures::USER_NAME); + $form['_password']->setValue(DummyFixtures::USER_PASSWORD); + $form['_remember_me']->setValue('on'); + $this->client->submit($form); + $this->assertContains( + 'edit profile', + $this->client->getResponse()->getContent() + ); + $this->assertContains( + 'logout', + $this->client->getResponse()->getContent() + ); + $this->assertContains( + 'user source', + $this->client->getResponse()->getContent() + ); + $this->assertContains( + DummyFixtures::USER_NAME, + $this->client->getResponse()->getContent() + ); + } + + public function testWrongPassword(): void + { + $this->client->followRedirects(); + $form = $this->crawler->selectButton('Log in')->form(); + $form['_username']->setValue(DummyFixtures::USER_NAME); + $form['_password']->setValue('wrong password'); + $form['_remember_me']->setValue('on'); + $this->client->submit($form); + $this->assertContains( + 'Invalid credentials.', + $this->client->getResponse()->getContent() + ); + } + + public function testWrongUsername(): void + { + $this->client->followRedirects(); + $form = $this->crawler->selectButton('Log in')->form(); + $form['_username']->setValue('unknown_username'); + $form['_password']->setValue(DummyFixtures::USER_PASSWORD); + $form['_remember_me']->setValue('on'); + $this->client->submit($form); + $this->assertContains( + 'Username could not be found.', + $this->client->getResponse()->getContent() + ); + } +}