Symfony сообщение об ошибке в модульном тесте с phpUnit - PullRequest
1 голос
/ 20 июня 2020

Я пытаюсь протестировать почтовый запрос в функциональном тесте с phpunit, но обнаружил эту ошибку:

Тип содержимого «application / x- www-form-urlencoded» не поддерживается. Поддерживаемые типы MIME: application / ld + json, application / json, text / html.

code test:

<?php 

    namespace App\Tests\Func;

    use Faker\Factory;
    use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;

    class UserTest extends AbstractEndPoint
    {
        private string $userPayload = '{"email": "%s", "password": "password"}';
    public function testGetUser(){
        $response= $this->getResponseFromRequest(Request::METHOD_GET, '/api/users','');
        $responseContent = $response->getContent();
        $responseDecode = json_decode($responseContent);

        $this->assertEquals(Response::HTTP_OK,$response->getStatusCode());
        $this->assertJson($responseContent);
        $this->assertNotEmpty($responseDecode);
    }

    public function testPostUser(){
        $response= $this->getResponseFromRequest(
            Request::METHOD_POST,
            '/api/users',
            $this->getPayload());
        
        $responseContent = $response->getContent();
        $responseDecode = json_decode($responseContent);
        $this->assertEquals(Response::HTTP_OK,$response->getStatusCode());
        $this->assertJson($responseContent);
        $this->assertNotEmpty($responseDecode);
    }

    private function getPayload(): string
    {
        $faker = Factory::create();

        return sprintf($this->userPayload, $faker->email);
    }
    }

code abstractEndPoint:

        <?php

    namespace App\Tests\Func;

    use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
    use Symfony\Component\HttpFoundation\Response;

    abstract class AbstractEndPoint extends WebTestCase
    {
        private array $serverInformations = ['ACCEPT' => 'application/json', 'CONTENT-TYPE' => 'application/json'];

        public function getResponseFromRequest(string $method, string $uri, string $payload):Response
        {
            $client = self::createClient();

            $client->request(
                $method,
                $uri.'.json',
                [],
                [],
                $this->serverInformations,
                $payload,
            );

            return $client->getResponse();
        }
    }

Как устранить эту ошибку? Я преобразовал полезную нагрузку в json, но у меня также была ошибка.

Спасибо

...