Answer the question
In order to leave comments, you need to log in
How to properly test a crud controller?
Good time dear colleagues. I am a beginner in web development. I'm trying to test a simple crud controller that interacts with a database. The test itself works, but there is one caveat, after testing the create action, you need to test index, view, update and delete with the same data that the create method created. I tried to connect the doctrine through the setUp() method, but it doesn't work. It seems to me that I'm clearly doing something wrong. I look forward to your advice. Thank you.
<?php
namespace App\Tests\Controller;
use App\Entity\AttributesFields;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class AttributesFieldsControllerTest extends WebTestCase
{
public $title = 'unique title';
public $id;
private $entityManager;
public function testCreate() {
$client = static::createClient();
$client->request('POST', '/attributes/fields/create', [
'attributes_fields' => [
'title' => $this->title,
'type' => 10,
'value_default' => 'default value 123',
'value_list' => 'list value 123',
]
]);
$this->assertEquals(302, $client->getResponse()->getStatusCode());
}
protected function setUp()
{
// parent::setUp();
$kernel = self::bootKernel();
$this->entityManager = $kernel->getContainer()
->get('doctrine')
->getManager();
$this->id = $this->entityManager->getRepository(AttributesFields::class)->findOneBy(['title' => $this->title])->getId();
// dd($this->id);
}
// public function testIndex() {
//
// $client = static::createClient();
//
// $client->request('GET', '/attributes/fields');
//
// $this->assertEquals(200, $client->getResponse()->getStatusCode());
//
// }
//
// public function testView() {
//
// $client = static::createClient();
//
// $client->request('GET', '/attributes/fields/' . $this->id);
//
// $this->assertEquals(200, $client->getResponse()->getStatusCode());
//
// }
}
Answer the question
In order to leave comments, you need to log in
Who cares, I solved the problem in this way!
<?php
namespace App\Tests\Controller;
use App\Entity\AttributesFields;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class AttributesFieldsControllerTest extends WebTestCase
{
public $title = 'unique title';
public $id;
private $entityManager;
public function testCreate() {
$client = static::createClient();
$client->request('POST', '/attributes/fields/create', [
'attributes_fields' => [
'title' => $this->title,
'type' => 10,
'value_default' => 'default value 123',
'value_list' => 'list value 123',
]
]);
$this->assertEquals(302, $client->getResponse()->getStatusCode());
}
public function testIndex() {
$client = static::createClient();
$client->request('GET', '/attributes/fields');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
public function testView() {
$client = static::createClient();
$kernel = self::bootKernel();
$this->entityManager = $kernel->getContainer()
->get('doctrine')
->getManager();
$this->id = $this->entityManager->getRepository(AttributesFields::class)->findOneBy(['title' => $this->title])->getId();
$client->request('GET', '/attributes/fields/' . $this->id);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
public function testUpdate() {
sleep(30);
$client = static::createClient();
$kernel = self::bootKernel();
$this->entityManager = $kernel->getContainer()
->get('doctrine')
->getManager();
$this->id = $this->entityManager->getRepository(AttributesFields::class)->findOneBy(['title' => $this->title])->getId();
$client->request('POST', '/attributes/fields/update/' . $this->id, [
'attributes_fields' => [
'title' => $this->title,
'type' => 10,
'value_default' => 'default value XXX',
'value_list' => 'list value 123',
]
]);
$this->assertEquals(302, $client->getResponse()->getStatusCode());
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question