PHP code example of macrominds / website-testing

1. Go to this page and download the library: Download macrominds/website-testing 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/ */

    

macrominds / website-testing example snippets


$this->testServer = new EmbeddedServerController(HOST,PORT,DOCROOT,'./tests/web/router.php');
$this->testServer->start();
//...
$this->testServer->stopAndWaitForConnectionLoss();


use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use macrominds\website\testing\EmbeddedServerController;

class FunctionalPageTest extends \PHPUnit_Framework_TestCase{
    /**
     *
     * @var EmbeddedServerController 
     */
    private static $serverController;
    private static $client;

// setup server

    public static function setupBeforeClass()
    {
        //see phpunit.xml for configuration of PORT, DOCROOT, ROUTERSCRIPT
        self::$serverController = new EmbeddedServerController('0.0.0.0',PORT,DOCROOT,ROUTERSCRIPT);
        self::$serverController->start();
        $host = self::$serverController->getHost();
        $port = PORT;
        self::$client = new Client([
            'base_uri' => "http://$host:$port"
        ]);
    }
  
// tear down server
  
    public static function tearDownAfterClass()
    {
        if (self::$serverController !== null) {
            self::$serverController->stopAndWaitForConnectionLoss();
        }
        self::$serverController = null;
    }

// arbitrary tests

    /**
     * @test
     */
    public function shouldSendCorrectRedirectHeaders()
    {
        // we expect a temporary redirect
        $this->assertCorrectnessOfRedirectHeader(302);
        // we expect a permanent redirect
        $this->assertCorrectnessOfRedirectHeader(301);
    }
    
    private function assertCorrectnessOfRedirectHeader($code)
    {
        // our test scenario is setup to answer requests to /301.html 
        // or /302.html with the corresponding redirect.
        $response = self::$client->get("/$code.html", [
            RequestOptions::ALLOW_REDIRECTS=>false
        ]);
        $this->assertEquals($code, $response->getStatusCode());
    }
}