|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bolt\tests\cls; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Bolt\protocol\V1; |
| 7 | + |
| 8 | +/** |
| 9 | + * Class V1Test |
| 10 | + * |
| 11 | + * @author Michal Stefanak |
| 12 | + * @link https://github.com/stefanak-michal/Bolt |
| 13 | + * |
| 14 | + * @covers \Bolt\protocol\V1 |
| 15 | + * |
| 16 | + * @package Bolt\tests\cls |
| 17 | + * @requires PHP >= 7.1 |
| 18 | + * @requires extension mbstring |
| 19 | + */ |
| 20 | +class V1Test extends TestCase |
| 21 | +{ |
| 22 | + private static $clsSocketPath = __DIR__ . DS . '..' . DS . '..' . DS . 'Socket.php'; |
| 23 | + |
| 24 | + public static function setUpBeforeClass(): void |
| 25 | + { |
| 26 | + $content = file_get_contents(self::$clsSocketPath); |
| 27 | + if (preg_match("/^final class Socket/m", $content)) |
| 28 | + file_put_contents(self::$clsSocketPath, str_replace('final class Socket', 'class Socket', $content)); |
| 29 | + } |
| 30 | + |
| 31 | + public static function tearDownAfterClass(): void |
| 32 | + { |
| 33 | + $content = file_get_contents(self::$clsSocketPath); |
| 34 | + if (preg_match("/^class Socket/m", $content)) |
| 35 | + file_put_contents(self::$clsSocketPath, str_replace('class Socket', 'final class Socket', $content)); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @return V1 |
| 40 | + */ |
| 41 | + public function test__construct() |
| 42 | + { |
| 43 | + $socket = $this->getMockBuilder(\Bolt\Socket::class) |
| 44 | + ->disableOriginalConstructor() |
| 45 | + ->onlyMethods(['write', 'read']) |
| 46 | + ->getMock(); |
| 47 | + |
| 48 | + $socket |
| 49 | + ->method('write') |
| 50 | + ->with( |
| 51 | + $this->callback(function ($buffer) { |
| 52 | + return $buffer == self::$buffer; |
| 53 | + }) |
| 54 | + ); |
| 55 | + |
| 56 | + $socket |
| 57 | + ->method('read') |
| 58 | + ->will($this->returnCallback([$this, 'readCallback'])); |
| 59 | + |
| 60 | + $cls = new V1(new \Bolt\PackStream\v1\Packer, new \Bolt\PackStream\v1\Unpacker, $socket); |
| 61 | + $this->assertInstanceOf(V1::class, $cls); |
| 62 | + |
| 63 | + return $cls; |
| 64 | + } |
| 65 | + |
| 66 | + static $i = 0; |
| 67 | + static $buffer = ''; |
| 68 | + |
| 69 | + public function readCallback() |
| 70 | + { |
| 71 | + $output = ''; |
| 72 | + switch (self::$i) { |
| 73 | + case 0: |
| 74 | + $output = hex2bin('0003'); // header of length 3 |
| 75 | + break; |
| 76 | + case 1: |
| 77 | + $output = hex2bin('B170A0'); // success {} |
| 78 | + break; |
| 79 | + case 2: |
| 80 | + $output = hex2bin('0000'); // end |
| 81 | + break; |
| 82 | + |
| 83 | + case 3: |
| 84 | + |
| 85 | + break; |
| 86 | + } |
| 87 | + |
| 88 | + self::$i++; |
| 89 | + return $output; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @depends test__construct |
| 94 | + * @param V1 $cls |
| 95 | + */ |
| 96 | + public function testInit(V1 $cls) |
| 97 | + { |
| 98 | + self::$i = 0; |
| 99 | + self::$buffer = 0x001fb40188546573742f312e3085626173696384757365728870617373776f72640000; |
| 100 | + $this->assertTrue($cls->init('Test/1.0', 'basic', 'user', 'password')); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @depends test__construct |
| 105 | + * @param V1 $cls |
| 106 | + */ |
| 107 | + public function testRun(V1 $cls) |
| 108 | + { |
| 109 | + self::$i = 0; |
| 110 | + $this->assertNotFalse($cls->run('RETURN 1')); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @depends test__construct |
| 115 | + * @param V1 $cls |
| 116 | + */ |
| 117 | + public function testPullAll(V1 $cls) |
| 118 | + { |
| 119 | + self::$i = 3; |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments