|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: batanov.pavel |
|
5
|
|
|
* Date: 16.05.2016 |
|
6
|
|
|
* Time: 13:44 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Bankiru\Api\Rpc\Tests; |
|
10
|
|
|
|
|
11
|
|
|
use Bankiru\Api\Rpc\Exception\InvalidMethodParametersException; |
|
12
|
|
|
use Bankiru\Api\Rpc\Routing\Exception\MethodNotFoundException; |
|
13
|
|
|
use Bankiru\Api\Rpc\Tests\Fixtures\Kernel; |
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
|
15
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
16
|
|
|
|
|
17
|
|
|
class RpcTest extends WebTestCase |
|
18
|
|
|
{ |
|
19
|
|
|
public function getArgumentVariants() |
|
20
|
|
|
{ |
|
21
|
|
|
return [ |
|
22
|
|
|
'missing all' => [[], false], |
|
23
|
|
|
'missing array' => [['noDefault' => 1], false], |
|
24
|
|
|
'not an array' => [['noDefault' => 1, 'array' => 2], false], |
|
25
|
|
|
'correct' => [['noDefault' => 1, 'array' => ['test1' => 2, 'abc']], true], |
|
26
|
|
|
'correct 2' => [['noDefault' => 1, 'array' => ['test', 'test2']], true], |
|
27
|
|
|
'correct w explicit default override' => [ |
|
28
|
|
|
['default' => 'new_value', 'noDefault' => 1, 'array' => []], |
|
29
|
|
|
true, |
|
30
|
|
|
], |
|
31
|
|
|
]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @dataProvider getArgumentVariants |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $args |
|
38
|
|
|
* @param bool $valid |
|
39
|
|
|
* |
|
40
|
|
|
* @throws InvalidMethodParametersException |
|
41
|
|
|
* @throws \Error |
|
42
|
|
|
*/ |
|
43
|
6 |
|
public function testController(array $args, $valid) |
|
44
|
|
|
{ |
|
45
|
6 |
|
$client = self::createClient(); |
|
46
|
|
|
try { |
|
47
|
6 |
|
$client->request( |
|
48
|
6 |
|
'POST', |
|
49
|
6 |
|
'/test/', |
|
50
|
6 |
|
array_replace(['method' => 'test/method',], $args) |
|
51
|
6 |
|
); |
|
52
|
6 |
|
} catch (InvalidMethodParametersException $e) { |
|
53
|
3 |
|
if ($valid) { |
|
54
|
|
|
throw $e; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
6 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getExceptionTestData() |
|
60
|
|
|
{ |
|
61
|
|
|
return [ |
|
62
|
|
|
'unknown method' => [ |
|
63
|
|
|
'/not_found_endpoint/', |
|
64
|
|
|
['method' => 'test/method', 'arg' => 1], |
|
65
|
|
|
NotFoundHttpException::class, |
|
66
|
|
|
], |
|
67
|
|
|
'uknown endpoint' => [ |
|
68
|
|
|
'/test/', |
|
69
|
|
|
['method' => 'unknown/method', 'arg' => 1], |
|
70
|
|
|
MethodNotFoundException::class, |
|
71
|
|
|
], |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @dataProvider getExceptionTestData |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $endpoint |
|
79
|
|
|
* @param array $args |
|
80
|
|
|
* @param string $exception FQCN |
|
81
|
|
|
*/ |
|
82
|
2 |
|
public function testException($endpoint, $args, $exception) |
|
83
|
|
|
{ |
|
84
|
2 |
|
$this->setExpectedExceptionRegExp($exception); |
|
85
|
2 |
|
$client = self::createClient(); |
|
86
|
|
|
|
|
87
|
2 |
|
$client->request( |
|
88
|
2 |
|
'POST', |
|
89
|
2 |
|
$endpoint, |
|
90
|
|
|
$args |
|
91
|
2 |
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
protected static function getKernelClass() |
|
95
|
|
|
{ |
|
96
|
|
|
return Kernel::class; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|