infinito/application/symfony/tests/Functional/UserFunctionTest.php

135 lines
3.7 KiB
PHP
Raw Permalink Normal View History

2020-04-10 14:49:26 +02:00
<?php
namespace Tests\Functional;
use Infinito\DataFixtures\DummyFixtures;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DomCrawler\Crawler;
/**
* Tests if test user can login.
*
* @author Kevin Veen-Birkenbach [aka. Frantz]
*/
2020-04-16 19:31:29 +02:00
class UserFunctionTest extends WebTestCase
2020-04-10 14:49:26 +02:00
{
2020-04-16 19:31:29 +02:00
/**
*
* @var string
*/
private $username = '';
/**
*
* @var string
*/
private $password = '';
/**
*
* @var string
*/
private $email = '';
2020-04-10 14:49:26 +02:00
/**
* @var Crawler
*/
private $crawler;
private $client;
2020-04-16 19:31:29 +02:00
private function initAttributs():void{
$this->username = 'function_test_user';
$this->password = 'function_test_user_password';
$this->email = 'function_test_user@test.test';
}
private function crawlUrl(string $url):void{
$this->crawler = $this->client->request('GET', $url);
$this->client->followRedirects();
}
private function registerUser():void{
$this->crawlUrl('/register/');
$form = $this->crawler->selectButton('Register')->form();
$form['fos_user_registration_form[username]']->setValue($this->username);
$form['fos_user_registration_form[email]']->setValue($this->email);
$form['fos_user_registration_form[plainPassword][first]']->setValue($this->password);
$form['fos_user_registration_form[plainPassword][second]']->setValue($this->password);
$this->client->submit($form);
}
2020-04-10 14:49:26 +02:00
/**
* Prepares the environment before running a test.
*/
protected function setUp()
{
parent::setUp();
$this->client = $this->createClient();
2020-04-16 19:31:29 +02:00
$this->initAttributs();
$this->registerUser();
2020-04-10 14:49:26 +02:00
}
2020-04-16 19:31:29 +02:00
2020-04-10 14:49:26 +02:00
public function testIfLoginFormPageIsReachable(): void
{
$this->assertEquals(200, $this->client->getResponse()
->getStatusCode());
}
2020-04-16 19:31:29 +02:00
public function testLoginSuccessfull(): void
2020-04-10 14:49:26 +02:00
{
2020-04-16 19:31:29 +02:00
$this->crawlUrl('/login/');
2020-04-10 14:49:26 +02:00
$form = $this->crawler->selectButton('Log in')->form();
2020-04-16 19:31:29 +02:00
$form['_username']->setValue($this->username);
$form['_password']->setValue($this->password);
2020-04-10 14:49:26 +02:00
$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(
2020-04-16 19:31:29 +02:00
$this->username,
2020-04-10 14:49:26 +02:00
$this->client->getResponse()->getContent()
);
}
2020-04-16 19:31:29 +02:00
public function testLoginWrongPassword(): void
2020-04-10 14:49:26 +02:00
{
2020-04-16 19:31:29 +02:00
$this->crawlUrl('/login/');
2020-04-10 14:49:26 +02:00
$form = $this->crawler->selectButton('Log in')->form();
2020-04-16 19:31:29 +02:00
$form['_username']->setValue($this->username);
2020-04-10 14:49:26 +02:00
$form['_password']->setValue('wrong password');
$form['_remember_me']->setValue('on');
$this->client->submit($form);
$this->assertContains(
'Invalid credentials.',
$this->client->getResponse()->getContent()
);
}
2020-04-16 19:31:29 +02:00
public function testLoginWrongUsername(): void
2020-04-10 14:49:26 +02:00
{
2020-04-16 19:31:29 +02:00
$this->crawlUrl('/login/');
2020-04-10 14:49:26 +02:00
$form = $this->crawler->selectButton('Log in')->form();
$form['_username']->setValue('unknown_username');
2020-04-16 19:31:29 +02:00
$form['_password']->setValue($this->password);
2020-04-10 14:49:26 +02:00
$form['_remember_me']->setValue('on');
$this->client->submit($form);
$this->assertContains(
'Username could not be found.',
$this->client->getResponse()->getContent()
);
}
}