This repository was archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginTest.php
More file actions
145 lines (122 loc) · 3.79 KB
/
PluginTest.php
File metadata and controls
145 lines (122 loc) · 3.79 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
<?php
/**
* Save HTTP request information to the database.
*
* @author Josantonius <hello@josantonius.com>
* @copyright 2017 - 2018 (c) Josantonius - HTTP Request Logger
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
* @link https://github.com/eliasis-framework/http-request-logger.git
* @since 1.0.1
*/
namespace Eliasis\Plugins\HttpRequestLogger;
use Eliasis\Complement\Type\Plugin;
use Eliasis\Framework\App;
use Josantonius\Database\Database;
use PHPUnit\Framework\TestCase;
/**
* Tests class for HTTP Request Logger Eliasis plugin.
*
* @runTestsInSeparateProcesses
*/
final class PluginTest extends TestCase
{
/**
* Table name.
*
* @var string
*/
protected $table = 'test_request';
/**
* App instance.
*
* @var object
*/
protected $app;
/**
* Launcher instance.
*
* @var object
*/
protected $launcher;
/**
* Root path.
*
* @var string
*/
protected $root;
/**
* Database connection.
*
* @var object
*/
protected $db;
/**
* Set up.
*/
public function setUp()
{
parent::setUp();
$this->app = new App();
$this->root = $_SERVER['DOCUMENT_ROOT'];
$app = $this->app;
$app::run($this->root);
$this->db = Database::getConnection('app');
$this->launcher = Plugin::HttpRequestLogger()->getControllerInstance(
'Launcher',
'controller'
);
}
/**
* Check if it is an instance of.
*/
public function testIsInstanceOf()
{
$this->assertInstanceOf('Eliasis\Framework\App', $this->app);
$this->assertInstanceOf('Josantonius\Database\Database', $this->db);
}
/**
* A table should be created when install the plugin.
*/
public function testTableShouldBeCreatedWhenInstallThePlugin()
{
Plugin::HttpRequestLogger()->doAction('activation');
$rows = $this->db->query("SELECT count(*)
FROM information_schema.tables
WHERE table_schema = '{$GLOBALS['DB_NAME']}'
AND table_name = '$this->table'");
$result = array_values((array) $rows[0]);
$this->assertSame((int) $result[0], 1);
}
/**
* Should add a record for each request.
*/
public function testShouldAddRecordForEachRequest()
{
Plugin::HttpRequestLogger()->doAction('activation');
$result = $this->db->select()
->from($this->table)
->where(['request_id = 1'])
->execute();
$this->assertSame($result[0]->request_id, '1');
$this->assertSame($result[0]->request_ip, '87.142.85.70');
$this->assertSame($result[0]->request_uri, '/sample-app/');
$this->assertSame($result[0]->request_protocol, 'HTTP/1.1');
$this->assertSame($result[0]->request_method, 'GET');
$this->assertSame($result[0]->request_referer, 'http://www.google.es/');
$this->assertSame($result[0]->request_user_agent, 'Mozilla/5.0');
$this->assertSame($result[0]->request_http_state, '0');
}
/**
* A table should be deleted when uninstall the plugin.
*/
public function testTableShouldBeDeletedWhenUninstallThePlugin()
{
Plugin::HttpRequestLogger()->doAction('uninstallation');
$rows = $this->db->query("SELECT count(*)
FROM information_schema.tables
WHERE table_schema = '{$GLOBALS['DB_NAME']}'
AND table_name = '$this->table'");
$result = array_values((array) $rows[0]);
$this->assertSame((int) $result[0], 0);
}
}