PHP code example of kununu / testing-bundle

1. Go to this page and download the library: Download kununu/testing-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kununu / testing-bundle example snippets




return [
    ...
    Kununu\TestingBundle\KununuTestingBundle::class => ['dev' => true, 'test' => true],
];

// Creates and returns a Builder that you can use to do a GET request
public static function aGetRequest(): self;

// Creates and returns a Builder that you can use to do a POST request
public static function aPostRequest(): self;

// Creates and returns a Builder that you can use to do a DELETE request
public static function aDeleteRequest(): self;

// Creates and returns a Builder that you can use to do a PUT request
public static function aPutRequest(): self;

// Creates and returns a Builder that you can use to do a PATCH request
public static function aPatchRequest(): self;

// Set The Request parameters
public function withParameters(array $parameters): self;

// Change The request method
public function withMethod(string $method): self;

// Set the URI to fetch
public function withUri(string $uri): self;

// Set the content of the request as an array that internally is transformed to a json and provided as the raw body data
public function withContent(array $content): self;

// Set the Raw body data
public function withRawContent(string $content): self;

// Sets an HTTP_AUTHORIZATION header with the value of "Bearer $token"
public function withAuthorization(string $token): self;

// Sets an header. 
// In converts any header name to uppercase and prepends "HTTP_" if the header name does not contains it
public function withHeader(string $headerName, string $headerValue): self;

// Sets a server parameter (HTTP headers are referenced with an HTTP_ prefix as PHP does)
public function withServerParameter(string $parameterName, string $parameterValue): self;

final protected function doRequest(RequestBuilder $builder): Symfony\Component\HttpFoundation\Response

$client->request($builder->method, $builder->uri, $builder->parameters, $builder->files, $builder->server, $builder->content);



namespace App\Tests\Integration\Controller;

use App\Tests\Integration\Controller\DataFixtures\MySQL\CreateCompanyDataFixtures;
use Kununu\TestingBundle\Test\RequestBuilder;
use Kununu\TestingBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;

class CompaniesControllerTest extends WebTestCase
{
    public function testCreateCompany(): void
    {
        $this->loadDbFixtures('your_doctrine_connection_name', [CreateCompanyDataFixtures::class]);

        $data = [
            'name'        => 'kununu GmbH',
            'location'    => [
                'city'         => 'Wien',
                'country_code' => 'at',
            ],
        ];

        $response = $this->doRequest(
            RequestBuilder::aPostRequest()
                ->withUri('/companies')
                ->withContent($data)
                ->withAuthorization('eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjYyZDVkNzc5NmQxOTk')
                ->withServerParameter('REMOTE_ADDR', '127.0.0.1')
        );

        $this->assertNotNull($response->getContent());
        $this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode());

        $json = $response->getContent();
        $this->assertJson($json);

        $company = json_decode($json, true);

        $this->assertSame($data['name'], $company['name']);
        $this->assertSame($data['location']['city'], $company['location']['city']);
        $this->assertSame($data['location']['country_code'], $company['location']['country_code']);
    }
}