summaryrefslogtreecommitdiff
path: root/libs/jsonrpc/tests/MiddlewareHandlerTest.php
blob: be70cbf78225641efba57f39e819bc4469fd6de7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php

use JsonRPC\Exception\AuthenticationFailureException;
use JsonRPC\MiddlewareHandler;
use JsonRPC\MiddlewareInterface;

require_once __DIR__.'/../../../vendor/autoload.php';

class FirstMiddleware implements MiddlewareInterface
{
    public function execute($username, $password, $procedureName)
    {
    }
}

class SecondMiddleware implements MiddlewareInterface
{
    public function execute($username, $password, $procedureName)
    {
        if ($username === 'myUsername' && $password === 'myPassword' && $procedureName === 'myProcedure') {
            throw new AuthenticationFailureException('Bad user');
        }
    }
}

class MiddlewareHandlerTest extends PHPUnit_Framework_TestCase
{
    public function testMiddlewareCanRaiseException()
    {
        $this->setExpectedException('JsonRpc\Exception\AuthenticationFailureException');

        $middlewareHandler = new MiddlewareHandler();
        $middlewareHandler->withUsername('myUsername');
        $middlewareHandler->withPassword('myPassword');
        $middlewareHandler->withProcedure('myProcedure');
        $middlewareHandler->withMiddleware(new FirstMiddleware());
        $middlewareHandler->withMiddleware(new SecondMiddleware());
        $middlewareHandler->execute();
    }
}