Passed
Pull Request — master (#9)
by Pavel
06:21
created

RpcTest::testInvalidController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1.0156
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
41
    {
42 3
        $client = self::createClient();
43 3
        $client->request('POST', '/test/', array_replace(['method' => 'test/method'], $args));
44
45 3
        self::assertTrue($client->getResponse()->isSuccessful());
46 3
    }
47
48
    /**
49
     * @dataProvider getInvalidArgumentVariants
50
     *
51
     * @param array $args
52
     *
53
     * @expectedException \Bankiru\Api\Rpc\Exception\InvalidMethodParametersException
54
     */
55 3
    public function testInvalidController(array $args)
56
    {
57 3
        $client = self::createClient();
58 3
        $client->request('POST', '/test/', array_replace(['method' => 'test/method'], $args));
59
    }
60
61
    public function getExceptionTestData()
62
    {
63
        return [
64
            'unknown method'  => [
65
                '/not_found_endpoint/',
66
                ['method' => 'test/method', 'arg' => 1],
67
                NotFoundHttpException::class,
68
            ],
69
            'uknown endpoint' => [
70
                '/test/',
71
                ['method' => 'unknown/method', 'arg' => 1],
72
                MethodNotFoundException::class,
73
            ],
74
        ];
75
    }
76
77
    /**
78
     * @dataProvider getExceptionTestData
79
     *
80
     * @param string $endpoint
81
     * @param array  $args
82
     * @param string $exception FQCN
83
     */
84 2
    public function testException($endpoint, $args, $exception)
85
    {
86 2
        if (method_exists($this, 'expectException')) {
87
            $this->expectException($exception);
88 2
        } elseif (method_exists($this, 'setExpectedException')) {
89 2
            $this->setExpectedException($exception);
90 2
        } else {
91
            throw new \BadMethodCallException('Unsupported PHPUnit version');
92
        }
93
94 2
        $client = self::createClient();
95
96 2
        $client->request(
97 2
            'POST',
98 2
            $endpoint,
99
            $args
100 2
        );
101
    }
102
103
    protected static function getKernelClass()
104
    {
105
        return Kernel::class;
106
    }
107
}
108