Skip to content

Commit 0cc85f8

Browse files
author
Saurav
committed
test cases added
1 parent 5547562 commit 0cc85f8

File tree

6 files changed

+267
-12
lines changed

6 files changed

+267
-12
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
}
1010
],
1111
"require": {
12-
"php-amqplib/php-amqplib": "^2.9"
12+
"php-amqplib/php-amqplib": "^2.9",
13+
"phpunit/phpunit": "^7.5"
1314
},
1415
"autoload": {
1516
"psr-4": {

phpunit.xml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit colors="true"
3+
convertErrorsToExceptions="true"
4+
convertNoticesToExceptions="true"
5+
convertWarningsToExceptions="true"
6+
stopOnFailure="false"
7+
bootstrap="./vendor/autoload.php">
8+
<testsuites>
9+
<testsuite name="Logging Test Suite">
10+
<directory suffix="Test.php">./test</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

src/Amqp.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct($host, $port, $user, $password)
2727

2828
$this->channel = $this->connection->channel();
2929
} catch (\Exception $e) {
30-
echo $e;
30+
throw new \Exception("amqp_connectio_error-".$e);
3131
}
3232
}
3333

@@ -43,7 +43,7 @@ public function insertMessage($message, $rmqQueueName)
4343
$msg = new AMQPMessage($message);
4444
$this->channel->basic_publish($msg, '', $rmqQueueName);
4545
} catch (\Exception $e) {
46-
echo $e;
46+
throw new \Exception("queue_message_insert_faliure-".$e);
4747
}
4848
}
4949
}

src/Logging.php

+26-8
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,22 @@ public function setLoggerTimeFormat($format)
121121
*/
122122
public function setQueueNames($queueArray)
123123
{
124-
$this->QueueNames = $queueArray;
124+
$KeysToMatch = array(
125+
"Api",
126+
"Debug",
127+
"Info",
128+
"Warn",
129+
"Error",
130+
"Critical"
131+
);
132+
$KeysRecieved = array_keys($queueArray);
133+
$result = array_diff_key($KeysToMatch, $KeysRecieved);
134+
if(count($result)>0){
135+
throw new \Exception("Parameter Mismatch");
136+
}else{
137+
$this->QueueNames = $queueArray;
138+
}
139+
125140
}
126141

127142
/**
@@ -151,8 +166,8 @@ public function connect()
151166
$this->RabbitmqUser,
152167
$this->RabbitmqPassword
153168
);
154-
} catch (Exception $e) {
155-
throw new Exception("rabbitmq_connection_failure-".$e);
169+
} catch (\Exception $e) {
170+
throw new \Exception("rabbitmq_connection_failure-" . $e);
156171
}
157172
}
158173

@@ -161,10 +176,12 @@ public function connect()
161176
public function publishMessage($message_details, $queueName)
162177
{
163178
try {
164-
$this->amqpConnection->insertMessage($message_details, $this->QueuePrefix.".".$queueName);
179+
//echo "call";
180+
$this->amqpConnection->insertMessage($message_details, $this->QueuePrefix . "." . $queueName);
165181
return true;
166-
} catch (Exception $e) {
167-
throw new Exception("queue_message_insert_faliure-".$e);
182+
} catch (\Exception $e) {
183+
// echo "called";
184+
throw new \Exception("queue_message_insert_faliure-" . $e);
168185
}
169186
}
170187

@@ -252,10 +269,11 @@ private function log($type, $message, array $data)
252269
*/
253270
private function sendLog($log, $type = '')
254271
{
272+
255273
try {
256274
$this->publishMessage($log, $type);
257-
} catch (Exception $e) {
258-
throw new Exception("queue_message_insert_faliure-".$e);
275+
} catch (\Exception $e) {
276+
throw new \Exception("queue_message_insert_faliure-" . $e);
259277
}
260278
}
261279

src/LoggingException.php

-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ class LoggingException extends Exception
66
{
77

88
}
9-
?>

test/LoggingTest.php

+224
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
<?php
2+
namespace LoggingTest;
3+
4+
use Logging\Logging;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class LoggingTest extends TestCase
8+
{
9+
10+
11+
protected $Logger;
12+
protected $Loggertest;
13+
/**
14+
* Set up.
15+
*/
16+
public function setUp()
17+
{
18+
parent::setUp();
19+
}
20+
/**
21+
* @expectedException Exception
22+
*/
23+
public function testConnectionInvalidHost()
24+
{
25+
$this->Loggertest = Logging::getInstance();
26+
$this->Loggertest->setRabbitmqHost("192.168.0.4");
27+
$this->Loggertest->setRabbitmqPort("5672");
28+
$this->Loggertest->setRabbitmqUser("guest");
29+
$this->Loggertest->setRabbitmqPassword("guest");
30+
$this->Loggertest->setLoggerTimeFormat("time.RFC3339");
31+
$this->Loggertest->setQueuePrefix("ayopop");
32+
$this->Loggertest->setQueueNames(array(
33+
"Api" => "api",
34+
"Debug" => "debug",
35+
"Info" => "info",
36+
"Warn" => "warning",
37+
"Error" => "error",
38+
"Critical" => "critical"
39+
));
40+
41+
$this->Loggertest->connect();
42+
}
43+
44+
/**
45+
* @expectedException Exception
46+
*/
47+
public function testConnectionInvalidPort()
48+
{
49+
$this->Loggertest = Logging::getInstance();
50+
$this->Loggertest->setRabbitmqHost("127.0.0.1");
51+
$this->Loggertest->setRabbitmqPort("56720");
52+
$this->Loggertest->setRabbitmqUser("guest");
53+
$this->Loggertest->setRabbitmqPassword("guest");
54+
$this->Loggertest->setLoggerTimeFormat("time.RFC3339");
55+
$this->Loggertest->setQueuePrefix("ayopop");
56+
$this->Loggertest->setQueueNames(array(
57+
"Api" => "api",
58+
"Debug" => "debug",
59+
"Info" => "info",
60+
"Warn" => "warning",
61+
"Error" => "error",
62+
"Critical" => "critical"
63+
));
64+
65+
$this->Loggertest->connect();
66+
}
67+
68+
/**
69+
* @expectedException Exception
70+
*/
71+
public function testConnectionInvalidUser()
72+
{
73+
$this->Loggertest = Logging::getInstance();
74+
$this->Loggertest->setRabbitmqHost("127.0.0.1");
75+
$this->Loggertest->setRabbitmqPort("5672");
76+
$this->Loggertest->setRabbitmqUser("saurav");
77+
$this->Loggertest->setRabbitmqPassword("guest");
78+
$this->Loggertest->setLoggerTimeFormat("time.RFC3339");
79+
$this->Loggertest->setQueuePrefix("ayopop");
80+
$this->Loggertest->setQueueNames(array(
81+
"Api" => "api",
82+
"Debug" => "debug",
83+
"Info" => "info",
84+
"Warn" => "warning",
85+
"Error" => "error",
86+
"Critical" => "critical"
87+
));
88+
89+
$this->Loggertest->connect();
90+
}
91+
92+
93+
public function testConnectionInvalidQueueName()
94+
{
95+
try {
96+
$this->Loggertest = Logging::getInstance();
97+
$this->Loggertest->setRabbitmqHost("127.0.0.1");
98+
$this->Loggertest->setRabbitmqPort("5672");
99+
$this->Loggertest->setRabbitmqUser("guest");
100+
$this->Loggertest->setRabbitmqPassword("guest");
101+
$this->Loggertest->setLoggerTimeFormat("time.RFC3339");
102+
$this->Loggertest->setQueuePrefix("ayopop");
103+
$this->Loggertest->setQueueNames(array(
104+
"Api" => "api",
105+
"Debug" => "debug",
106+
"Info" => "info",
107+
"Warn" => "warning",
108+
"Error" => "error",
109+
//"Critical" => "critical"
110+
));
111+
$this->Loggertest->connect();
112+
} catch (\Exception $e) {
113+
$emess = $e->getMessage();
114+
// echo $emess;
115+
}
116+
$this->assertEquals($emess, 'Parameter Mismatch');
117+
}
118+
119+
120+
public function testGetInstance()
121+
{
122+
$Logger = Logging::getInstance();
123+
$Logger->setRabbitmqHost("127.0.0.1");
124+
$Logger->setRabbitmqPort("5672");
125+
$Logger->setRabbitmqUser("guest");
126+
$Logger->setRabbitmqPassword("guest");
127+
$Logger->setLoggerTimeFormat("time.RFC3339");
128+
$Logger->setQueuePrefix("ayopop");
129+
$Logger->setQueueNames(array(
130+
"Api" => "api",
131+
"Debug" => "debug",
132+
"Info" => "info",
133+
"Warn" => "warning",
134+
"Error" => "error",
135+
"Critical" => "critical"
136+
));
137+
138+
$Logger->connect();
139+
$first = $Logger;
140+
$second = Logging::getInstance();
141+
$this->assertSame($first, $second);
142+
}
143+
144+
/**
145+
* @expectedException \ArgumentCountError
146+
*/
147+
public function testpublishMessage()
148+
{
149+
150+
$Logger = Logging::getInstance();
151+
$Logger->setRabbitmqHost("127.0.0.1");
152+
$Logger->setRabbitmqPort("5672");
153+
$Logger->setRabbitmqUser("guest");
154+
$Logger->setRabbitmqPassword("guest");
155+
$Logger->setLoggerTimeFormat("time.RFC3339");
156+
$Logger->setQueuePrefix("ayopop");
157+
$Logger->setQueueNames(array(
158+
"Api" => "api",
159+
"Debug" => "debug",
160+
"Info" => "info",
161+
"Warn" => "warning",
162+
"Error" => "error",
163+
"Critical" => "critical"
164+
));
165+
166+
$Logger->connect();
167+
168+
$Logger->publishMessage();
169+
}
170+
171+
/**
172+
* @expectedException \TypeError
173+
*/
174+
175+
public function testTypeInfo()
176+
{
177+
178+
$Logger = Logging::getInstance();
179+
$Logger->setRabbitmqHost("127.0.0.1");
180+
$Logger->setRabbitmqPort("5672");
181+
$Logger->setRabbitmqUser("guest");
182+
$Logger->setRabbitmqPassword("guest");
183+
$Logger->setLoggerTimeFormat("time.RFC3339");
184+
$Logger->setQueuePrefix("ayopop");
185+
$Logger->setQueueNames(array(
186+
"Api" => "api",
187+
"Debug" => "debug",
188+
"Info" => "info",
189+
"Warn" => "warning",
190+
"Error" => "error",
191+
"Critical" => "critical"
192+
));
193+
194+
$Logger->connect();
195+
$Logger->info("saurav", "test");
196+
}
197+
198+
/**
199+
* @expectedException \ArgumentCountError
200+
*/
201+
202+
public function testArgumentInfo()
203+
{
204+
205+
$Logger = Logging::getInstance();
206+
$Logger->setRabbitmqHost("127.0.0.1");
207+
$Logger->setRabbitmqPort("5672");
208+
$Logger->setRabbitmqUser("guest");
209+
$Logger->setRabbitmqPassword("guest");
210+
$Logger->setLoggerTimeFormat("time.RFC3339");
211+
$Logger->setQueuePrefix("ayopop");
212+
$Logger->setQueueNames(array(
213+
"Api" => "api",
214+
"Debug" => "debug",
215+
"Info" => "info",
216+
"Warn" => "warning",
217+
"Error" => "error",
218+
"Critical" => "critical"
219+
));
220+
221+
$Logger->connect();
222+
$Logger->info();
223+
}
224+
}

0 commit comments

Comments
 (0)