-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCase.php
More file actions
183 lines (163 loc) · 4.38 KB
/
Copy pathTestCase.php
File metadata and controls
183 lines (163 loc) · 4.38 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace Tests;
use Orchestra\Testbench\TestCase as OrchestraTestCase;
/**
* Class TestCase
* @author MohammadNiknab <MohammadNiknab@gmail.com>
*/
abstract class TestCase extends OrchestraTestCase
{
/**
* Setup the test environment.
*/
protected function setUp(): void
{
parent::setUp();
}
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
*
* @return void
*/
protected function getEnvironmentSetUp($app)
{
// Setup default database to use sqlite :memory:
$app['config']->set('database.default', 'test');
$app['config']->set('database.connections.test', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
// setup routes from file
\Route::group([], __DIR__ . '/../src/Http/Routes/web.php');
// setup configs
\Config::set('surl.identifier_hash_length', 5);
\Config::set('surl.cache_prefix', 'surl.identifier-');
\Config::set('surl.items_per_page', 5);
}
/**
* Get package providers. At a minimum this is the package being tested, but also
* would include packages upon which our package depends, e.g. Cartalyst/Sentry
* In a normal app environment these would be added to the 'providers' array in
* the config/app.php file.
*
* @param \Illuminate\Foundation\Application $app
*
* @return array
*/
protected function getPackageProviders($app)
{
return [
'\Mniknab\Surl\SurlServiceProvider',
];
}
/**
* Get random id (fake) for tests
*
* @return int
*/
protected function getRandomId(): int
{
return rand(1, 99);
}
/**
* Get short url (redirect url) string
*
* @param $identifier
* @return string
*/
protected function getShortUrl($identifier): string
{
return route('surl.redirect',$identifier);
}
/**
* Create surl with POST method
*
* @return \Illuminate\Testing\TestResponse
*/
protected function newSurl()
{
return $this->getJson(route('surl.create'));
}
/**
* Create surl with POST Json method
*
* @param array $parameters
* @param bool $returnJson
* @return \Illuminate\Testing\TestResponse|mixed
*/
protected function createSurl(array $parameters = [], $returnJson = false)
{
$response = $this->postJson(route('surl.store'), $parameters);
return $returnJson ? $response->json() : $response;
}
/**
* Create surl with POST method - Not json
*
* @param array $parameters
* @return \Illuminate\Testing\TestResponse|mixed
*/
protected function createSurlWithNormalPostMethod(array $parameters = [])
{
return $this->post(route('surl.store'), $parameters);
}
/**
* Update surl with PUT method
*
* @param $id
* @param array $parameters
* @return \Illuminate\Testing\TestResponse
*/
protected function updateSurl($id, array $parameters = [],$withNormalPutMethod = false)
{
if($withNormalPutMethod){
return $this->put(route('surl.update',$id), $parameters);
}
return $this->putJson(route('surl.update',$id), $parameters);
}
/**
* Get edit surl page
*
* @param $id
* @return \Illuminate\Testing\TestResponse
*/
protected function editSurl($id)
{
return $this->getJson(route('surl.edit',$id));
}
/**
* Destroy surl with DELETE method
*
* @param $id
* @return \Illuminate\Testing\TestResponse
*/
protected function destroySurl($id,$withNormalDeleteMethod = false)
{
if($withNormalDeleteMethod){
return $this->delete(route('surl.destroy',$id));
}
return $this->deleteJson(route('surl.destroy',$id));
}
/**
* Get all surl list
*
* @return \Illuminate\Testing\TestResponse
*/
protected function listSurl()
{
return $this->getJson(route('surl.list'));
}
/**
* Get redirect page
*
* @param $identifier
* @return \Illuminate\Testing\TestResponse
*/
protected function redirectSurl($identifier)
{
$shortUrl = $this->getShortUrl($identifier);
return $this->getJson($shortUrl);
}
}