1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests; |
4
|
|
|
|
5
|
|
|
use App\Middleware\Api; |
6
|
|
|
use Zend\Diactoros\Uri; |
7
|
|
|
use tests\mocks\Delegate; |
8
|
|
|
use App\Middleware\Database; |
9
|
|
|
use App\Middleware\Redirect; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
12
|
|
|
|
13
|
|
|
class RedirectTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* HTTP Api Request |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
protected $request; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The PDO isntance from Eloquent. |
24
|
|
|
* |
25
|
|
|
* @var \PDO |
26
|
|
|
*/ |
27
|
|
|
protected $db; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Initialize our api and request. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function setUp(): void |
35
|
|
|
{ |
36
|
|
|
require_once __DIR__ . "/../app/helpers.php"; |
37
|
|
|
$request = ServerRequestFactory::fromGlobals( |
38
|
|
|
$server = [ |
39
|
|
|
'REQUEST_METHOD' => 'GET', |
40
|
|
|
'REQUEST_URI' => '/ghijkl' |
41
|
|
|
], |
42
|
|
|
$query = [], |
43
|
|
|
$body = [], |
44
|
|
|
$cookies = [], |
45
|
|
|
$files = [] |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
// Setup the database connection |
49
|
|
|
$config = config('tests/samples/config.php'); |
50
|
|
|
touch($config['database']['database']); |
51
|
|
|
$delegate = new Delegate(); |
52
|
|
|
$database = new Database(); |
53
|
|
|
$database->process($request->withAttribute('config', $config), $delegate); |
54
|
|
|
$this->request = $delegate->getRequest(); |
|
|
|
|
55
|
|
|
$this->db = $this->request->getAttribute('capsule')->getConnection()->getPdo(); |
56
|
|
|
$this->db->query("CREATE TABLE `redirects` ( |
57
|
|
|
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, |
58
|
|
|
`hash` TEXT NOT NULL, |
59
|
|
|
`redirect_to` TEXT NOT NULL, |
60
|
|
|
`count` INTEGER NOT NULL DEFAULT 0, |
61
|
|
|
`created_at` TEXT NOT NULL, |
62
|
|
|
`updated_at` TEXT NOT NULL |
63
|
|
|
);"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set up and tear down the database. |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function tearDown(): void |
72
|
|
|
{ |
73
|
|
|
unlink($this->request->getAttribute('config')['database']['database']); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Test the middleware delegate. |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function testFailedRedirect(): void |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$delegate = new Delegate(); |
84
|
|
|
$redirect = new Redirect(); |
85
|
|
|
$response = $redirect->process($this->request, $delegate); |
86
|
|
|
$this->assertEquals(404, $response->getStatusCode()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Test a successful redirect request. |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
public function testSuccessfulRedirect(): void |
95
|
|
|
{ |
96
|
|
|
$this->db->query( |
97
|
|
|
'INSERT INTO redirects (`hash`, `redirect_to`, `count`, `created_at`, `updated_at`) |
98
|
|
|
VALUES("abcdef", "http://example.com", 0, "2017-10-10", "2017-10-10")' |
99
|
|
|
); |
100
|
|
|
$request = $this->request->withUri(new Uri(config()['base_url'] . '/abcdef')); |
101
|
|
|
$delegate = new Delegate(); |
102
|
|
|
$redirect = new Redirect(); |
103
|
|
|
$response = $redirect->process($request, $delegate); |
104
|
|
|
$this->assertEquals(301, $response->getStatusCode()); |
105
|
|
|
$this->assertEquals('http://example.com', $response->getHeaderLine('Location')); |
106
|
|
|
$this->assertEquals(1, $this->db->query('SELECT `count` FROM `redirects`')->fetch(\PDO::FETCH_ASSOC)['count']); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.