@Suleyman95

Как правильно протестировать crud контроллер?

Доброго времени уважаемые коллеги. Я начинающий в веб разработке. Пытаюсь протестировать простой crud контроллер, который взаимодействует с базой данных. Сам тест работает, но есть один нюанс, после протестирования экшена create, нужно чтобы теми же данными которые создал метод create протестировать так же и index, view, update и delete. Пытался подключить доктрину через setUp() метод, но не получается. Мне кажется что я явно что-то делаю неправильно. Жду ваших советов. Спасибо.
<?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());
//
//    }


}
  • Вопрос задан
  • 155 просмотров
Решения вопроса 1
@Suleyman95 Автор вопроса
Кому интересно, решил проблему таким образом!

<?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());

    }

}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы