PHP code example of zfegg / expressive-test

1. Go to this page and download the library: Download zfegg/expressive-test 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/ */

    

zfegg / expressive-test example snippets



use Psr\Http\Message\ResponseInterface;
use Zfegg\ExpressiveTest\AbstractActionTestCase;

class HomePageTest extends AbstractActionTestCase {

    public function testHome() {
        $response = $this->runApp(
            'POST',
            '/?test=1',
            ['body' => '2'],
            ['HTTP_CONTENT_TYPE' => 'application/json'],
            '{"a":"b"}',
            ['cookie' => '3']
        );

        $this->assertInstanceOf(ResponseInterface::class, $response);
    }

    public function testPassMiddlewareOrMockService() {

        $this->container->setService('some middleware', new PassMiddleware());
        
        $mock = $this->createMock(SomeService::class);
        $this->container->setService('mock some service', $mock);

        $response = $this->runApp(
            'POST',
            '/?test=1',
            ['body' => '2'],
            ['HTTP_CONTENT_TYPE' => 'application/json'],
            '{"a":"b"}',
            ['cookie' => '3']
        );

        $this->assertInstanceOf(ResponseInterface::class, $response);
    }
}


use Psr\Http\Message\ResponseInterface;
use Zfegg\ExpressiveTest\AbstractActionTestCase;

class HomePageTest extends AbstractActionTestCase {

    public function testHome() {
        /*
        $this->get($uri, $headers = []);
        $this->getJson($uri, $headers = []);
        $this->post($uri, $data = [], $headers = []);
        $this->postJson($uri, $data = [], $headers = []);
        $this->put($uri, $data = [], $headers = []);
        $this->putJson($uri, $data = [], $headers = []);
        $this->patch($uri, $data = [], $headers = []);
        $this->patchJson($uri, $data = [], $headers = []);
        $this->delete($uri, $data = [], $headers = []);
        $this->json($method, $uri, $data = [], $headers = []);
        $this->call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null);
        */
        $response = $this->getJson('/?test=1');
        $response->assertOk();
        $response->assertSuccessful();
    }

    public function testRedirectLogin() {
        $response = $this->getJson('/redirect');
        $response->assertRedirect('/login');
    }
}

use Psr\Http\Message\ResponseInterface;
use Zfegg\ExpressiveTest\AbstractActionTestCase;
use Zfegg\ExpressiveTest\PassMiddleware;

class HomePageTest extends AbstractActionTestCase {

    public function testHome() {
        // Pass a authentication middleware.
        $this->container->setService(AuthenticationMiddleware::class, PassMiddleware::class); 

        $response = $this->getJson('/api/users');
        $response->assertOk();
    }
}