1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace ChatApp\Tests; |
4
|
|
|
use PHPUnit_Framework_TestCase; |
5
|
|
|
use ChatApp\Register; |
6
|
|
|
use ChatApp\Search; |
7
|
|
|
use ChatApp\Reply; |
8
|
|
|
use ChatApp\Session; |
9
|
|
|
|
10
|
|
|
use Dotenv\Dotenv; |
11
|
|
|
$dotenv = new Dotenv(dirname(__DIR__)); |
12
|
|
|
$dotenv->load(); |
13
|
|
|
session_start(); |
14
|
|
|
|
15
|
|
|
class TestReply |
16
|
|
|
extends |
|
|
|
|
17
|
|
|
PHPUnit_Framework_TestCase |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
protected $obRegister; |
21
|
|
|
protected $obCompose; |
22
|
|
|
protected $obReply; |
23
|
|
|
|
24
|
|
|
public function setUp() |
25
|
|
|
{ |
26
|
|
|
$this->obRegister = new Register(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
View Code Duplication |
public function test_authRegister() |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
$output = $this->obRegister->authRegister( |
34
|
|
|
[ |
35
|
|
|
"name" => 'Test', |
36
|
|
|
"email" => '[email protected]', |
37
|
|
|
"username" => 'test', |
38
|
|
|
"mob" => '1234567890', |
39
|
|
|
"passRegister" => 'testing' |
40
|
|
|
] |
41
|
|
|
); |
42
|
|
|
$output = (array)json_decode($output); |
43
|
|
|
$this->assertEquals([ |
44
|
|
|
'location' => 'http://127.0.0.1/openchat/views/account.php' |
45
|
|
|
], $output); |
46
|
|
|
$userId = Session::get('start'); |
47
|
|
|
return $userId; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @depends test_authRegister |
52
|
|
|
* Testing for the register with empty username |
53
|
|
|
*/ |
54
|
|
|
public function test_authRegister2($userId) |
55
|
|
|
{ |
56
|
|
|
$msg = [ |
57
|
|
|
"name" => $userId, |
58
|
|
|
"reply" => [ |
59
|
|
|
0 => "Hello World" |
60
|
|
|
] |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
$msg = json_encode($msg); |
64
|
|
|
|
65
|
|
|
$expectedOutput = [ |
|
|
|
|
66
|
|
|
"Compose" => [ |
67
|
|
|
"0" => (object)[ |
68
|
|
|
"login_id" => "1", |
69
|
|
|
"name" => "Test", |
70
|
|
|
"email" => "[email protected]", |
71
|
|
|
"username" => "test", |
72
|
|
|
"mobile" => "1234567890", |
73
|
|
|
"login_status" => "0" |
74
|
|
|
] |
75
|
|
|
] |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
$output = $this->obRegister->authRegister( |
79
|
|
|
[ |
80
|
|
|
"name" => 'Test2', |
81
|
|
|
"email" => '[email protected]', |
82
|
|
|
"username" => 'test2', |
83
|
|
|
"mob" => '1234567890', |
84
|
|
|
"passRegister" => 'testing' |
85
|
|
|
] |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$output = (array)json_decode($output); |
89
|
|
|
$this->assertEquals([ |
90
|
|
|
'location' => 'http://127.0.0.1/openchat/views/account.php' |
91
|
|
|
], $output); |
92
|
|
|
|
93
|
|
|
$sessionId = session_id(); |
94
|
|
|
$search = new Reply($sessionId); |
95
|
|
|
|
96
|
|
|
$output = $search->replyTo($msg); |
97
|
|
|
$this->assertEquals("Messages is sent", $output); |
98
|
|
|
|
99
|
|
|
$output = $search->replyTo(json_encode([])); |
100
|
|
|
$this->assertEquals("Failed", $output); |
101
|
|
|
|
102
|
|
|
$output = $search->replyTo(json_encode([ |
103
|
|
|
"name" => -1, |
104
|
|
|
"reply" => [ |
105
|
|
|
0 => "Hello World" |
106
|
|
|
] |
107
|
|
|
])); |
108
|
|
|
$this->assertEquals("Invalid Authentication", $output); |
109
|
|
|
|
110
|
|
|
$output = $search->replyTo(json_encode([ |
111
|
|
|
"name" => $userId, |
112
|
|
|
"reply" => [ |
113
|
|
|
0 => "Hello" |
114
|
|
|
] |
115
|
|
|
])); |
116
|
|
|
$this->assertEquals("Messages is sent", $output); |
117
|
|
|
|
118
|
|
|
// For suggestion matched but not in total messages |
119
|
|
|
// $output = $search->replyTo((object)["value" => "T"]); |
|
|
|
|
120
|
|
|
// $output = (array)json_decode($output); |
|
|
|
|
121
|
|
|
// $this->assertEquals(["Search" => "Not Found"], $output); |
|
|
|
|
122
|
|
|
|
123
|
|
|
// // Not Found |
124
|
|
|
// $output = $search->replyTo((object)["value" => ""]); |
|
|
|
|
125
|
|
|
// $output = (array)json_decode($output); |
|
|
|
|
126
|
|
|
// $this->assertEquals(["Search" => "Not Found"], $output); |
|
|
|
|
127
|
|
|
|
128
|
|
|
// // Query Failed |
129
|
|
|
// $output = $search->replyTo((object)["value" => "'"]); |
|
|
|
|
130
|
|
|
// $output = (array)json_decode($output); |
|
|
|
|
131
|
|
|
// $this->assertEquals(["Search" => "Not Found"], $output); |
|
|
|
|
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @depends test_authRegister2 |
137
|
|
|
* Empty the DB |
138
|
|
|
*/ |
139
|
|
|
// public function test_EmptyDB() |
|
|
|
|
140
|
|
|
// { |
141
|
|
|
// $connect = mysqli_connect( |
142
|
|
|
// getenv('DB_HOST'), |
|
|
|
|
143
|
|
|
// getenv('DB_USER'), |
|
|
|
|
144
|
|
|
// getenv('DB_PASSWORD'), |
|
|
|
|
145
|
|
|
// getenv('DB_NAME') |
146
|
|
|
// ); |
147
|
|
|
// $query = "TRUNCATE `login`"; |
|
|
|
|
148
|
|
|
// $this->assertTrue($connect->query($query)); |
|
|
|
|
149
|
|
|
// $query = "TRUNCATE `profile`"; |
|
|
|
|
150
|
|
|
// $this->assertTrue($connect->query($query)); |
|
|
|
|
151
|
|
|
// $query = "TRUNCATE `messages`"; |
|
|
|
|
152
|
|
|
// $this->assertTrue($connect->query($query)); |
|
|
|
|
153
|
|
|
// $query = "TRUNCATE `total_message`"; |
|
|
|
|
154
|
|
|
// $this->assertTrue($connect->query($query)); |
|
|
|
|
155
|
|
|
// $query = "TRUNCATE `register`"; |
|
|
|
|
156
|
|
|
// $this->assertTrue($connect->query($query)); |
|
|
|
|
157
|
|
|
// } |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.