Skip to content

Commit

Permalink
Merge branch 'Issue-#2'
Browse files Browse the repository at this point in the history
  • Loading branch information
marius-meissner committed Mar 25, 2018
2 parents 46c7c0a + eb4c8c8 commit a9af423
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private function receiveData(int $timeout, int $size, string $previousData = '')
}
$data = $previousData . $data;

if (strlen($data) != $size) {
if (strlen($data) < $size) {
usleep(self::SLEEP_TIME);
return $this->receiveData($timeout, $size, $data);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Protocol/ExtensionResponseStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function decode(string $data): Response
$extensionSize = $baseData['res'];
$fullSize = $extensionSize + Response::BASE_SIZE;

if (strlen($data) != $fullSize) {
if (strlen($data) < $fullSize) {
throw new IncompleteDataException("Received data is incomplete => expected $fullSize bytes, but got " . strlen($data), $fullSize);
}

Expand Down
28 changes: 27 additions & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Volantus\Pigpio\Client;
use Volantus\Pigpio\Protocol\DefaultRequest;
use Volantus\Pigpio\Protocol\DefaultResponseStructure;
use Volantus\Pigpio\Protocol\ExtensionResponseStructure;
use Volantus\Pigpio\Protocol\IncompleteDataException;
use Volantus\Pigpio\Protocol\Response;
use Volantus\Pigpio\Protocol\ResponseStructure;
Expand Down Expand Up @@ -34,7 +35,7 @@ protected function setUp()
$this->service = new Client($this->socket);
}

public function test_sendRaw_correct()
public function test_sendRaw_correct_defaultRequest()
{
$expectedResponse = new Response(0, null);

Expand All @@ -59,6 +60,31 @@ public function test_sendRaw_correct()
self::assertSame($result, $expectedResponse);
}

public function test_sendRaw_correct_extensionRequest()
{
$expectedResponse = new Response(0, ['Q', 'R']);

$responseStructure = $this->getMockBuilder(ExtensionResponseStructure::class)->disableOriginalConstructor()->getMock();
$responseStructure->expects(self::once())
->method('decode')
->with(self::equalTo('ABCDEFGHIJKLMNOPQR'))
->willReturn($expectedResponse);

$request = new DefaultRequest(8, 21, 1500, $responseStructure);

$this->socket->expects(self::once())
->method('send')
->with(self::equalTo($request->encode()));

$this->socket->expects(self::once())
->method('listen')
->willReturn('ABCDEFGHIJKLMNOPQR');

$result = $this->service->sendRaw($request);

self::assertSame($result, $expectedResponse);
}

public function test_sendRaw_headerNotComplete()
{
$expectedResponse = new Response(0, null);
Expand Down

0 comments on commit a9af423

Please sign in to comment.