|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Tarantool Client package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Tarantool\Client\Tests\Unit\Handler; |
|
15
|
|
|
|
|
16
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
17
|
|
|
use PHPUnit\Framework\TestCase; |
|
18
|
|
|
use Tarantool\Client\Handler\Handler; |
|
19
|
|
|
use Tarantool\Client\Handler\MiddlewareHandler; |
|
20
|
|
|
use Tarantool\Client\Middleware\Middleware; |
|
21
|
|
|
use Tarantool\Client\Request\Request; |
|
22
|
|
|
use Tarantool\Client\Tests\Unit\ResponseFactory; |
|
23
|
|
|
|
|
24
|
|
|
final class MiddlewareHandlerTest extends TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var Request|MockObject |
|
28
|
|
|
*/ |
|
29
|
|
|
private $request; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Handler|MockObject |
|
33
|
|
|
*/ |
|
34
|
|
|
private $handler; |
|
35
|
|
|
|
|
36
|
|
|
protected function setUp() : void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->request = $this->createMock(Request::class); |
|
39
|
|
|
$this->handler = $this->createMock(Handler::class); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testCreateMiddlewareExecutionOrder() : void |
|
43
|
|
|
{ |
|
44
|
|
|
$trace = new \ArrayObject(); |
|
45
|
|
|
|
|
46
|
|
|
/** @var Middleware $middleware1 */ |
|
47
|
|
|
$middleware1 = $this->createMock(Middleware::class); |
|
48
|
|
|
$middleware1->expects($this->once())->method('process')->willReturnCallback( |
|
|
|
|
|
|
49
|
|
|
static function (Request $request, Handler $handler) use (&$trace) { |
|
50
|
|
|
$trace[] = 1; |
|
51
|
|
|
|
|
52
|
|
|
return $handler->handle($request); |
|
53
|
|
|
} |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
/** @var Middleware $middleware2 */ |
|
57
|
|
|
$middleware2 = $this->createMock(Middleware::class); |
|
58
|
|
|
$middleware2->expects($this->once())->method('process')->willReturnCallback( |
|
59
|
|
|
static function (Request $request, Handler $handler) use (&$trace) { |
|
60
|
|
|
$trace[] = 2; |
|
61
|
|
|
|
|
62
|
|
|
return $handler->handle($request); |
|
63
|
|
|
} |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$this->handler->method('handle')->willReturn(ResponseFactory::create()); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
$handler = MiddlewareHandler::create($this->handler, $middleware1, $middleware2); |
|
69
|
|
|
$handler->handle($this->request); |
|
70
|
|
|
|
|
71
|
|
|
self::assertSame('1,2', implode(',', $trace->getArrayCopy())); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testMiddlewareRemainsAfterExecution() : void |
|
75
|
|
|
{ |
|
76
|
|
|
$middlewareCallback = static function (Request $request, Handler $handler) { |
|
77
|
|
|
return $handler->handle($request); |
|
78
|
|
|
}; |
|
79
|
|
|
|
|
80
|
|
|
/** @var Middleware $middleware */ |
|
81
|
|
|
$middleware = $this->createMock(Middleware::class); |
|
82
|
|
|
$middleware->expects($this->exactly(2))->method('process')->willReturnCallback($middlewareCallback); |
|
83
|
|
|
|
|
84
|
|
|
$this->handler->method('handle')->willReturn(ResponseFactory::create()); |
|
85
|
|
|
|
|
86
|
|
|
$handler = MiddlewareHandler::create($this->handler, $middleware); |
|
87
|
|
|
$handler->handle($this->request); |
|
88
|
|
|
$handler->handle($this->request); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.