PHP code example of dzentota / regression

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

    

dzentota / regression example snippets




declare(strict_types=1);

use GuzzleHttp\Psr7\Request;
use Regression\Scenario;

class ExampleRegression extends Scenario
{
    /**
     * @return string
     */
    public function getRegressionDescription(): string
    {
        return 'Response does not fit our expectations';
    }

    /**
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Regression\RegressionException
     */
    public function run(): void
    {
        $request = new Request(
            'GET',
            '/'
        );

        $this->send($request)
            ->expectStatusCode(200)
            ->expectSubstring('Example Domain');
    }
}



declare(strict_types=1);

namespace Regression;

use Psr\Http\Message\RequestInterface;

class OAuthSession implements Session
{
    private string $accessToken;

    public function __construct(string $token)
    {
        $this->accessToken = $token;
    }

    public function init(RequestInterface $request): RequestInterface
    {
        return $request->withHeader('OAuth-Token', $this->accessToken);
    }
}


declare(strict_types=1);

namespace Regression;

use GuzzleHttp\Psr7;

abstract class OAuthScenario extends Scenario
{
    public function login(string $username, string $password): self
    {
        $payload = json_encode([
            'username' => $username,
            'password' => $password,
            'grant_type' => 'password',
        ]);

        $tokenRequest = new Psr7\Request(
            'POST',
            '/oauth2/token',
            ['Content-Type' => 'application/json'],
            $payload
        );
        $this->send($tokenRequest);
        if (($token = (json_decode((string)$this->lastResponse->getBody()))->access_token) === null) {
            throw new \RuntimeException("Login failed");
        }
        $this->session = new OAuthSession($token);
        return $this;
    }
}