/
ApiSpecTestCaseTest.php
127 lines (109 loc) · 3.05 KB
/
ApiSpecTestCaseTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace Test\ApiSpec;
use Illuminate\Routing\Router;
use Illuminate\Foundation\Application;
use ApiSpec\ApiSpecTestCase;
use Illuminate\Foundation\Testing\Concerns\MocksApplicationServices;
use Illuminate\Foundation\Testing\TestResponse;
use Illuminate\Http\Response;
use Illuminate\Filesystem\FilesystemAdapter;
use Mockery as m;
/**
* Confirm ApiSpecTestCase output spec file
*/
class ApiSpecTestCaseTest extends ApiSpecTestCase
{
use MocksApplicationServices;
private $acceptor;
public function createApplication()
{
$app = new class extends Application {
private $acceptor;
public function __construct($basePath = null)
{
//
}
public function setAcceptor($acceptor)
{
$this->acceptor = $acceptor;
}
public function make($class, array $param = []) {
$mock = m::mock(FilesystemAdapter::class);
$mock->shouldReceive('drive')->andReturn($this->acceptor);
return $mock;
}
};
$app->setAcceptor($this->acceptor);
$this->app = $app;
}
protected function setUp()
{
$this->acceptor = new class {
public $filename;
public $str;
public function put($filename, $str) {
$this->filename = $filename;
$this->str = $str;
}
};
$this->createApplication();
}
/**
* @test
* @dataProvider provideJsonRequests
*/
public function canOutputHttpSpec($method, $url, $expectedFilename)
{
$this->isExportSpec = true;
$methodName = strtoupper(str_replace('Json', '', $method));
$this->{$method}($url);
// output file correspondented to url structure
$this->assertEquals($expectedFilename, $this->acceptor->filename);
$expectedStr = <<< EOS
$methodName $url
Content-Type: application/json
Accept: application/json
# Response:
#{
# "name": "huga",
# "rank": "foo"
#}
EOS;
// .http str
$this->assertEquals($expectedStr, $this->acceptor->str);
}
public function json($method, $uri, array $data = [], array $headers = [])
{
return new TestResponse(new Response(['name' => 'huga', 'rank' => 'foo']));
}
public function provideJsonRequests()
{
return [
[
'getJson',
'foo/bar',
'foo/bar/GET.http'
],
[
'postJson',
'jazz/rock/progressive',
'jazz/rock/progressive/POST.http'
],
[
'putJson',
'foo/bar',
'foo/bar/PUT.http'
],
[
'deleteJson',
'jazz/rock/progressive',
'jazz/rock/progressive/DELETE.http'
],
[
'patchJson',
'person/bob',
'person/bob/PATCH.http'
]
];
}
}