Skip to content

Commit 8887b51

Browse files
committed
Added some more tests with mocks for otherwise untestable conditions.
1 parent c28e378 commit 8887b51

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

tests/Misc/ConnectionlessTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
use DateInterval;
66
use DateTime;
77
use DateTimeZone;
8+
use PEAR2\Net\RouterOS\Client;
89
use PEAR2\Net\RouterOS\Communicator;
910
use PEAR2\Net\RouterOS\InvalidArgumentException;
1011
use PEAR2\Net\RouterOS\LengthException;
1112
use PEAR2\Net\RouterOS\NotSupportedException;
1213
use PEAR2\Net\RouterOS\Query;
1314
use PEAR2\Net\RouterOS\Request;
15+
use PEAR2\Net\RouterOS\Response;
1416
use PEAR2\Net\RouterOS\Script;
1517
use PEAR2\Net\RouterOS\UnexpectedValueException;
1618
use PEAR2\Net\Transmitter as T;
@@ -946,6 +948,66 @@ public function providerControlByte()
946948
);
947949
}
948950

951+
public function testInvalidResponseType()
952+
{
953+
$transMock = $this->createMock('PEAR2\Net\Transmitter\TcpClient');
954+
$transMock->method('isPersistent')->will(
955+
$this->onConsecutiveCalls(false, true)
956+
);
957+
$transMock->method('isAvailable')->willReturn(true);
958+
$transMock->method('isDataAwaiting')->willReturn(true);
959+
960+
$comMock = $this->createMock(ROS_NAMESPACE . '\Communicator');
961+
$comMock->method('getTransmitter')->willReturn($transMock);
962+
$comMock->method('getNextWord')->willReturn('TEST');
963+
964+
//Non persistent connection
965+
try {
966+
new Response($comMock);
967+
$this->fail('Getting unknown types should throw an exception.');
968+
} catch (UnexpectedValueException $e) {
969+
$this->assertSame(
970+
UnexpectedValueException::CODE_RESPONSE_TYPE_UNKNOWN,
971+
$e->getCode()
972+
);
973+
}
974+
975+
//Persistent connection
976+
try {
977+
new Response($comMock);
978+
$this->fail('Getting unknown types should throw an exception.');
979+
} catch (UnexpectedValueException $e) {
980+
$this->assertSame(
981+
UnexpectedValueException::CODE_RESPONSE_TYPE_UNKNOWN,
982+
$e->getCode()
983+
);
984+
}
985+
}
986+
987+
public function testUnexpectedLoginException()
988+
{
989+
$newLockException = new T\LockException('TEST');
990+
$transMock = $this->createMock('PEAR2\Net\Transmitter\TcpClient');
991+
$transMock->method('isPersistent')->willReturn(true);
992+
$transMock->method('isAvailable')->willReturn(true);
993+
$transMock->method('isDataAwaiting')->willReturn(true);
994+
$transMock->method('lock')->will(
995+
$this->throwException($newLockException)
996+
);
997+
998+
$comMock = $this->createMock(ROS_NAMESPACE . '\Communicator');
999+
$comMock->method('getTransmitter')->willReturn($transMock);
1000+
1001+
try {
1002+
Client::login($comMock, 'TEST', 'TEST');
1003+
$this->fail(
1004+
'Unexpected exceptions during login should be re-thrown'
1005+
);
1006+
} catch (T\LockException $e) {
1007+
$this->assertSame($e, $newLockException);
1008+
}
1009+
}
1010+
9491011
public function testPrepareScript()
9501012
{
9511013
$msg = 'testing';

0 commit comments

Comments
 (0)