DiceAppTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 41.18 %

Coupling/Cohesion

Components 1
Dependencies 18

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 18
dl 35
loc 85
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 20 1
A testAppCanRolld6AndReturnJson() 7 7 1
A testAppCanRolld6AndReturnHtml() 7 7 1
A testAppCanRolld20AndReturnHtml() 7 7 1
A testAppCanRolldSteveandReturnJson() 7 7 1
A testAppCanRolldFateandReturnJson() 7 7 1
A runApp() 0 5 1
A requestForPath() 0 8 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
use MeadSteve\DiceApi\Counters\NullCounter;
4
use MeadSteve\DiceApi\Dice\Factories\DiceFactoryCollection;
5
use MeadSteve\DiceApi\Dice\Factories\NumericDiceFactory;
6
use MeadSteve\DiceApi\Dice\Factories\SpecialDiceFactory;
7
use MeadSteve\DiceApi\Renderer\Html;
8
use MeadSteve\DiceApi\Renderer\Json;
9
use MeadSteve\DiceApi\Renderer\Plain;
10
use MeadSteve\DiceApi\UrlDiceGenerator;
11
use MeadSteve\DiceApi\DiceApp;
12
use MeadSteve\DiceApi\Renderer\RendererCollection;
13
use MeadSteve\DiceApi\RequestHandler\DiceRequestHandler;
14
use PHPUnit\Framework\TestCase;
15
use Slim\Http\Body;
16
use Slim\Http\Request;
17
use Slim\Http\Response;
18
19
class DiceAppTest extends TestCase
20
{
21
    private $app;
22
23
24
    protected function setUp() : void
25
    {
26
        $diceGenerator = new UrlDiceGenerator(
27
            new DiceFactoryCollection([
28
                new NumericDiceFactory(),
29
                new SpecialDiceFactory()
30
            ])
31
        );
32
        $rendererCollection = new RendererCollection([
33
            new Json(),
34
            new Html('http://test.com'),
35
            new Plain()
36
        ]);
37
        $nullCounter = new NullCounter();
38
        $diceRequestHandler = new DiceRequestHandler($diceGenerator, $rendererCollection, $nullCounter);
39
        $this->app = new DiceApp(
40
            $diceRequestHandler,
41
            $nullCounter
42
        );
43
    }
44
45 View Code Duplication
    public function testAppCanRolld6AndReturnJson()
0 ignored issues
show
Duplication introduced by steve.brazier
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...
46
    {
47
        $request = $this->requestForPath("/json/d6");
48
        $responseOut = $this->runApp($request);
49
        $this->assertEquals($responseOut->getStatusCode(), 200);
50
        $this->assertEquals($responseOut->getHeader("Content-Type")[0], "application/json");
51
    }
52
53 View Code Duplication
    public function testAppCanRolld6AndReturnHtml()
0 ignored issues
show
Duplication introduced by steve.brazier
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...
54
    {
55
        $request = $this->requestForPath("/html/d6");
56
        $responseOut = $this->runApp($request);
57
        $this->assertEquals($responseOut->getStatusCode(), 200);
58
        $this->assertEquals($responseOut->getHeader("Content-Type")[0], "text/html");
59
    }
60
61 View Code Duplication
    public function testAppCanRolld20AndReturnHtml()
0 ignored issues
show
Duplication introduced by bachmac
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...
62
    {
63
        $request = $this->requestForPath("/html/d20");
64
        $responseOut = $this->runApp($request);
65
        $this->assertEquals($responseOut->getStatusCode(), 200);
66
        $this->assertEquals($responseOut->getHeader("Content-Type")[0], "text/html");
67
    }
68
69 View Code Duplication
    public function testAppCanRolldSteveandReturnJson()
0 ignored issues
show
Duplication introduced by steve.brazier
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...
70
    {
71
        $request = $this->requestForPath("/json/2dSteve");
72
        $responseOut = $this->runApp($request);
73
        $this->assertEquals($responseOut->getStatusCode(), 200);
74
        $this->assertEquals($responseOut->getHeader("Content-Type")[0], "application/json");
75
    }
76
77 View Code Duplication
    public function testAppCanRolldFateandReturnJson()
0 ignored issues
show
Duplication introduced by Steve B
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...
78
    {
79
        $request = $this->requestForPath("/json/2dFate");
80
        $responseOut = $this->runApp($request);
81
        $this->assertEquals($responseOut->getStatusCode(), 200);
82
        $this->assertEquals($responseOut->getHeader("Content-Type")[0], "application/json");
83
    }
84
85
    /**
86
     * @param $request
87
     * @return Psr\Http\Message\ResponseInterface
88
     */
89
    private function runApp($request)
90
    {
91
        $app = $this->app;
92
        return $app($request, new Response());
93
    }
94
95
    private function requestForPath($path)
96
    {
97
        $path = new \Slim\Http\Uri("http", "diceapi.com", null, $path);
98
        $body = new Body(fopen('php://temp', 'r+'));
99
        $headers = new \Slim\Http\Headers();
100
        $request = new Request("GET", $path, $headers, [], [], $body);
101
        return $request;
102
    }
103
}
104