From 9a6a4751525185cb717fb56037512cb10b4e4481 Mon Sep 17 00:00:00 2001 From: edefimov Date: Thu, 23 Jul 2015 23:47:33 +0300 Subject: [PATCH] Added RawFramePicker test --- tests/unit/Frame/RawFramePickerTest.php | 59 +++++++++++++++++++ tests/unit/Socket/Io/DatagramClientIoTest.php | 40 +++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 tests/unit/Frame/RawFramePickerTest.php diff --git a/tests/unit/Frame/RawFramePickerTest.php b/tests/unit/Frame/RawFramePickerTest.php new file mode 100644 index 0000000..f2a2f6d --- /dev/null +++ b/tests/unit/Frame/RawFramePickerTest.php @@ -0,0 +1,59 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Tests\AsyncSockets\Frame; + +use AsyncSockets\Frame\FramePickerInterface; +use AsyncSockets\Frame\RawFramePicker; + +/** + * Class RawFramePickerTest + */ +class RawFramePickerTest extends AbstractFramePickerTest +{ + /** {@inheritdoc} */ + protected function createFramePicker() + { + return new RawFramePicker(); + } + + /** + * testReadSingleFrame + * + * @return void + */ + public function testReadSingleFrame() + { + $data = md5(microtime(true)); + $picker = $this->createFramePicker(); + $result = $picker->pickUpData($data); + + self::assertEmpty($result, 'Raw frame picker must always collect data on first call'); + self::assertEquals($data, (string) $picker->createFrame(), 'Incorrect frame data'); + self::assertTrue($picker->isEof(), 'EOF flag is not set'); + } + + /** + * testCantReadSecondTime + * + * @return void + */ + public function testCantReadSecondTime() + { + $data = md5(microtime(true)); + $picker = $this->createFramePicker(); + $picker->pickUpData($data); + + $secondData = sha1(microtime(true)); + $result = $picker->pickUpData($secondData); + self::assertEquals($secondData, $result, 'Raw frame picker must ignore data on second call'); + self::assertEquals($data, (string) $picker->createFrame(), 'Incorrect frame data'); + } +} diff --git a/tests/unit/Socket/Io/DatagramClientIoTest.php b/tests/unit/Socket/Io/DatagramClientIoTest.php index f6d0abe..bd0d47e 100644 --- a/tests/unit/Socket/Io/DatagramClientIoTest.php +++ b/tests/unit/Socket/Io/DatagramClientIoTest.php @@ -128,6 +128,46 @@ function ($handle, $size, $flags, &$address) use (&$data) { self::assertEquals($expectedData, (string) $frame, 'Incorrect frame'); } + /** + * testReadUnreachedFrame + * + * @param string|null $remoteAddress Remote address for I/O object + * + * @return void + * @dataProvider remoteAddressDataProvider + * @expectedException \AsyncSockets\Exception\FrameSocketException + */ + public function testReadUnreachedFrame($remoteAddress) + { + $this->setUpIoObject($remoteAddress); + + $data = '1'; + $picker = $this->getMockForAbstractClass( + 'AsyncSockets\Frame\FramePickerInterface', + [], + '', + true, + true, + true, + ['isEof'] + ); + $picker->expects(self::any())->method('isEof')->willReturn(false); + + PhpFunctionMocker::getPhpFunctionMocker('stream_socket_recvfrom')->setCallable( + function ($handle, $size, $flags, &$address) use (&$data) { + $address = $this->address; + $result = $data; + + if (!($flags & MSG_PEEK)) { + $data = ''; + } + return $result; + } + ); + + $this->object->read($picker); + } + /** * testWriteData *