Skip to content

Commit

Permalink
Improve unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Pavlovichev committed Oct 8, 2017
1 parent 602cecc commit 2db6fed
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 71 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"php": ">=5.3"
},
"require-dev": {
"phpunit/phpunit": "~5.7"
"phpunit/phpunit": "~5.7",
"mockery/mockery": "^0.9.9",
"codeception/aspect-mock": "^2.0"
},
"license": "MIT",
"authors": [
Expand Down
13 changes: 12 additions & 1 deletion src/Zabbix/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ protected function sendData($body)
protected function parseResponse($socket)
{
// Get 1024 bytes from socket
socket_recv($socket, $response, 1024, 0);
$response = $this->socketReceive($socket);

// Length of header in response 13 bytes
$headerLength = 13;
Expand All @@ -163,6 +163,17 @@ protected function parseResponse($socket)
return $this;
}

/**
* Socket receive wrapper
*
* @param resource $socket
* @return int
*/
protected function socketReceive($socket)
{
return socket_recv($socket, $response, 1024, 0);
}

/**
* Clear request data
*
Expand Down
156 changes: 87 additions & 69 deletions tests/SenderTest.php
Original file line number Diff line number Diff line change
@@ -1,102 +1,114 @@
<?php

namespace Disc\Zabbix\Tests;

use AspectMock\Kernel;
use AspectMock\Test;
use Disc\Zabbix\Sender;
use PHPUnit_Framework_MockObject_MockObject;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery\Mock;
use PHPUnit_Framework_TestCase;
use ReflectionMethod;

/**
* Class SenderTest
* @package Disc\Zabbix\Tests
* @package \Disc\Zabbix\Tests
*
* @covers Disc\Zabbix\Sender
* @coversDefaultClass Disc\Zabbix\Sender
* @covers \Disc\Zabbix\Sender
* @coversDefaultClass \Disc\Zabbix\Sender
*/
class SenderTest extends PHPUnit_Framework_TestCase
{
use MockeryPHPUnitIntegration;

/**
* @var PHPUnit_Framework_MockObject_MockObject|\Disc\Zabbix\Sender
* Prepare
*/
protected $sender;

public function setUp()
{
$this->sender = new Sender('localhost', 10051);

$this->sender = $this->getMockBuilder(Sender::class)
->disableOriginalConstructor()
->setMethods(['getResponse', 'sendData'])
->getMock();

$this->sender
->expects($this->any())
->method('getResponse')
->willReturn([
'response' => 'success',
'info' => 'processed: 1; failed: 1; total: 2; seconds spent: 0.000021',
]);
$kernel = Kernel::getInstance();
$kernel->init([
'debug' => true,
'includePaths' => [__DIR__.'/../src']
]);
}

/**
* Test for getData
*
* @covers ::getData
* @covers ::send
*/
public function testAddData()
{
$method = new ReflectionMethod(Sender::class, 'getData');
$method->setAccessible(true);

$this->assertCount(0, $method->invoke($this->sender));
$this->sender->addData('Host', 'test.key', 'some value');
$this->sender->addData('Host', 'another.key', 123);
$this->sender->addData('Host', 'one.more.key', 0.001, time());
$this->assertCount(3, $method->invoke($this->sender));
/** @var Mock|Sender $sender */
$sender = \Mockery::mock(Sender::class . '[sendData]', ['localhost']);
$sender->shouldAllowMockingProtectedMethods();
$sender->shouldReceive('sendData')->once()->with(json_encode([
'request' => 'sender data',
'data' => [],
]));

$sender->send();

$sender->addData('Host', 'test.key', 'some value');
$sender->addData('Host', 'another.key', 123);
$expectedTime = time();
$sender->addData('Host', 'one.more.key', 0.001, $expectedTime);

$sender->shouldReceive('sendData')->once()->with(json_encode([
'request' => 'sender data',
'data' => [
[
'host' => 'Host',
'key' => 'test.key',
'value' => 'some value',
],
[
'host' => 'Host',
'key' => 'another.key',
'value' => 123,
],
[
'host' => 'Host',
'key' => 'one.more.key',
'value' => 0.001,
'clock' => $expectedTime,
],
],
]));
$sender->send();
}

/**
* Test for clear data
*
* @covers ::getData
* @covers ::clearData
*/
public function testClearData()
{
$methodGetData = new ReflectionMethod(Sender::class, 'getData');
$methodGetData->setAccessible(true);

$methodClearData = new ReflectionMethod(Sender::class, 'clearData');
$methodClearData->setAccessible(true);

$this->sender->addData('Host', 'test.key', 'some value');
$this->sender->addData('Host', 'another.key', 123);
$this->assertCount(2, $methodGetData->invoke($this->sender));

$methodClearData->invoke($this->sender);
$this->assertCount(0, $methodGetData->invoke($this->sender));
}

/**
* Test for send
*
* @covers ::send
*/
public function testSend()
public function testClearData()
{
$method = new ReflectionMethod(Sender::class, 'buildRequestBody');
$method->setAccessible(true);

$this->sender->addData('host', 'some.key', 'test value');
$this->sender->addData('host', 'some.key.2', 134);

$this->sender
->expects($this->once())
->method('sendData')
->with($method->invoke($this->sender));

$this->sender->send();
$this->assertNotEmpty($this->sender->getResponse());
/** @var Mock|Sender $sender */
$sender = \Mockery::mock(Sender::class . '[sendData]', ['localhost']);
$sender->shouldAllowMockingProtectedMethods();

$sender->addData('Host', 'test.key', 'some value');
$sender->shouldReceive('sendData')->once()->with(json_encode([
'request' => 'sender data',
'data' => [
[
'host' => 'Host',
'key' => 'test.key',
'value' => 'some value',
]
],
]));
$sender->send();
$sender->shouldReceive('sendData')->once()->with(json_encode([
'request' => 'sender data',
'data' => [],
]));
$sender->send();
}

/**
Expand All @@ -106,10 +118,16 @@ public function testSend()
*/
public function testGetResponse()
{
$this->sender->addData('host', 'some.key.2', 134);
$this->sender->send();
$response = $this->sender->getResponse();
$this->assertArrayHasKey('response', $response);
$this->assertArrayHasKey('info', $response);
test::func('Disc\Zabbix', 'socket_create', '');
test::func('Disc\Zabbix', 'socket_connect', '');
test::func('Disc\Zabbix', 'socket_send', '');
test::func('Disc\Zabbix', 'socket_close', '');

/** @var Mock|Sender $sender */
$sender = \Mockery::mock(Sender::class)->makePartial();
$sender->shouldAllowMockingProtectedMethods();
$sender->shouldReceive('socketReceive')->andReturn('header {"code": 100}');
$sender->send();
$this->assertSame(["code" => 100], $sender->getResponse());
}
}

0 comments on commit 2db6fed

Please sign in to comment.