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

    @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());
    
        }
    
    }
    Ответ написан
    Комментировать