1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Rpc\Test\Tests; |
4
|
|
|
|
5
|
|
|
use Bankiru\Api\Rpc\Exception\InvalidMethodParametersException; |
6
|
|
|
use Bankiru\Api\Rpc\Routing\Exception\MethodNotFoundException; |
7
|
|
|
use Bankiru\Api\Rpc\Test\Kernel; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
9
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
10
|
|
|
|
11
|
|
|
class RpcTest extends WebTestCase |
12
|
|
|
{ |
13
|
|
|
public function getValidArgumentVariants() |
14
|
|
|
{ |
15
|
|
|
return [ |
16
|
|
|
'correct' => [['noDefault' => 1, 'array' => ['test1' => 2, 'abc']]], |
17
|
|
|
'correct 2' => [['noDefault' => 1, 'array' => ['test', 'test2']]], |
18
|
|
|
'correct w explicit default override' => [ |
19
|
|
|
['default' => 'new_value', 'noDefault' => 1, 'array' => []], |
20
|
|
|
], |
21
|
|
|
]; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function getInvalidArgumentVariants() |
25
|
|
|
{ |
26
|
|
|
return [ |
27
|
|
|
'missing all' => [[]], |
28
|
|
|
'missing array' => [['noDefault' => 1]], |
29
|
|
|
'not an array' => [['noDefault' => 1, 'array' => 2]], |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @dataProvider getValidArgumentVariants |
35
|
|
|
* |
36
|
|
|
* @param array $args |
37
|
|
|
* |
38
|
|
|
* @throws InvalidMethodParametersException |
39
|
|
|
*/ |
40
|
3 |
View Code Duplication |
public function testValidController(array $args) |
|
|
|
|
41
|
|
|
{ |
42
|
3 |
|
$client = self::createClient(); |
43
|
|
|
|
44
|
3 |
|
$client->request( |
45
|
3 |
|
'POST', |
46
|
3 |
|
'/test/', |
47
|
3 |
|
array_replace(['method' => 'test/method',], $args) |
48
|
3 |
|
); |
49
|
|
|
|
50
|
3 |
|
self::assertTrue($client->getResponse()->isSuccessful()); |
51
|
3 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @dataProvider getInvalidArgumentVariants |
55
|
|
|
* |
56
|
|
|
* @param array $args |
57
|
|
|
* |
58
|
|
|
* @expectedException \Bankiru\Api\Rpc\Exception\InvalidMethodParametersException |
59
|
|
|
*/ |
60
|
3 |
|
public function testInvalidController(array $args) |
61
|
|
|
{ |
62
|
3 |
|
$client = self::createClient(); |
63
|
|
|
|
64
|
3 |
|
$client->request( |
65
|
3 |
|
'POST', |
66
|
3 |
|
'/test/', |
67
|
3 |
|
array_replace(['method' => 'test/method',], $args) |
68
|
3 |
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getExceptionTestData() |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
|
|
'unknown method' => [ |
75
|
|
|
'/not_found_endpoint/', |
76
|
|
|
['method' => 'test/method', 'arg' => 1], |
77
|
|
|
NotFoundHttpException::class, |
78
|
|
|
], |
79
|
|
|
'uknown endpoint' => [ |
80
|
|
|
'/test/', |
81
|
|
|
['method' => 'unknown/method', 'arg' => 1], |
82
|
|
|
MethodNotFoundException::class, |
83
|
|
|
], |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @dataProvider getExceptionTestData |
89
|
|
|
* |
90
|
|
|
* @param string $endpoint |
91
|
|
|
* @param array $args |
92
|
|
|
* @param string $exception FQCN |
93
|
|
|
*/ |
94
|
2 |
|
public function testException($endpoint, $args, $exception) |
95
|
|
|
{ |
96
|
2 |
|
if (method_exists($this, 'expectException')) { |
97
|
|
|
$this->expectException($exception); |
98
|
2 |
|
} elseif (method_exists($this, 'setExpectedException')) { |
99
|
2 |
|
$this->setExpectedException($exception); |
|
|
|
|
100
|
2 |
|
} else { |
101
|
|
|
throw new \BadMethodCallException('Unsupported PHPUnit version'); |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
$client = self::createClient(); |
105
|
|
|
|
106
|
2 |
|
$client->request( |
107
|
2 |
|
'POST', |
108
|
2 |
|
$endpoint, |
109
|
|
|
$args |
110
|
2 |
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected static function getKernelClass() |
114
|
|
|
{ |
115
|
|
|
return Kernel::class; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.