I am a student, and I quite new to php. For my practical education I am required to write functional tests using PHPUnit on a big project. For last 3 days I am stuck trying to work with database during PHPUnit tests. For example I have made tests for users to register to the website and login. But the problem was as users register I have to activate them. I managed to use crawler to activate, but it would be way easier if I could just access the database, make a query and than go forward with tests. Even excluding this, I wanted to create a mock (I believe this is how it's called) and just run the tests in database so I would not have to use crawler and long way to login as admin and manualy delete user.
I am not skilled enough to know how to solve this task. I tried installing dbUnit and reading the tutorial. But when i tried using it I did not manage to complete anything. Using SetUp() and TearDown() also did not work.
Here are the links I found on stackoverflow with questions similar to mine: Delete test entity after testing with phpunit symfony -this guy has almost the same use of crawler as I do, but I do not understand the solution. Phpunit testing with database -The answer here is good, and that's where I decided to get dbunit. However I can not manage to do anything with it because of my lack of understanding.
Anyway, here is my code (stripped out, as I have 500 lines of crawler crawling on website) I am running all tests in one class as if they are running one after another I can follow the process of registering, logging in and deleting. If I was able to know how to use database interractions in between life would be so much easier.
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Doctrine\ORM\EntityRepository;
use PHPUnit_Extensions_Database_TestCase_Trait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Doctrine\Common\Persistence\ObjectManager;
use PDO;
use Doctrine\ORM\Mapping as ORM;
class LoginRegistroTest extends WebTestCase
{
+public function testUserRegisterFail()
+public function testUserRegister() //check if new user can be registered and this also writes the user to the database
+public function testAdminLogin() //this uses the website admin panel and crawler to login and find the registered user and change Active from 0 to 1.
+public function testLoginFail()
+public function testUserLogin() //check if new user can login
+public function testUserDelete() //delete the user created for testing - again using the website admin panel and admin account.
}
Edit: here is a code that I would like to work for example but i don't know how to execute query. Note at the end where I would like to change the name of a user for example.
/**
* @var \Doctrine\ORM\EntityManager
*/
private $em;
/**
* {@inheritDoc}
*/
protected function setUp()
{
self::bootKernel();
$this->em = static::$kernel->getContainer()
->get('doctrine')
->getManager();
}
public function testUserRegister() //check if new artista can be registered
{
$client = static::createClient();
$crawler=$client->request('GET', "/");
// Create a new client to browse the application
$link = $crawler->filter('a:contains("Regístrate")')->eq(0)->link();
$crawler = $client->click($link);
// var_dump($crawler->html());
$this->assertEquals(1, $crawler->filter('h1:contains("REGISTRO")')->count());
$buttonCrawlerNode = $crawler->selectButton("Entra");
$form = $buttonCrawlerNode->form();
$crawler->filter('#registro_category option:contains("Usuario")');
$form['frontendbundle_artista[nombre]'] = 'Test404';
$form['frontendbundle_artista[apellidos]'] = 'Test404';
$form['frontendbundle_artista[email]'] = 'Test404@gmail.com';
$form['frontendbundle_artista[password][first]'] = 'Test404';
$form['frontendbundle_artista[password][second]'] = 'Test404';
$form['frontendbundle_artista[condiciones]'] ->tick();
// submit the form
$client->followRedirects();
$crawler = $client->submit($form);
$this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET ");
$this->assertTrue($crawler->filter('body:contains("Test404@gmail.com")')->count() >= 1 ); //if user was created this will be true
$this->em
->CreateQuery('UPDATE usuario SET nombre = \'Alfred Schmidt\' WHERE UsuarioID = 2106;')
;
$client->getResponse()->getContent();
}
this is with what i was able to fix all my problems:
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$nombre = 'Test405';
$usuario =$em->getRepository('AppBundle:usuario')->findOneByNombre($nombre);
$em->remove($usuario);
$em->flush();