Passed
Pull Request — master (#9)
by Pavel
12:36
created

RpcTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 11.21 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 49.06%

Importance

Changes 0
Metric Value
dl 12
loc 107
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2
ccs 26
cts 53
cp 0.4906
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getValidArgumentVariants() 0 10 1
A getInvalidArgumentVariants() 0 8 1
A testValidController() 12 12 1
A testInvalidController() 0 10 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)
1 ignored issue
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
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);
1 ignored issue
show
Bug introduced by
The method setExpectedException() does not exist on Bankiru\Api\Rpc\Test\Tests\RpcTest. Did you maybe mean setExpectedExceptionFromAnnotation()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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