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() |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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
|
|
|
|
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.