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

RpcTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 7.22 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 40%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 7
loc 97
ccs 18
cts 45
cp 0.4
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getValidArgumentVariants() 0 10 1
A getInvalidArgumentVariants() 0 8 1
A testValidController() 7 7 1
A testInvalidController() 0 5 1
A getExceptionTestData() 0 15 1
A testException() 0 18 3
A getKernelClass() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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