Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Mar 3, 2024
1 parent 078377f commit 0937209
Show file tree
Hide file tree
Showing 21 changed files with 379 additions and 151 deletions.
4 changes: 3 additions & 1 deletion src/Transport/Frame/Visitor/ChunkArguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public function __invoke(Stream $arguments): Maybe
*/
$frame = $this->frames->match(
static fn($first, $rest) => Frame\Composite::of(
static fn(Value ...$values) => Sequence::of(...$values),
static fn(Unpacked ...$values) => Sequence::of(...$values)->map(
static fn($unpacked) => $unpacked->unwrap(),
),
$first,
...$rest->toList(),
),
Expand Down
120 changes: 81 additions & 39 deletions tests/Transport/Connection/FrameReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
Model\Connection\MaxFrameSize,
TimeContinuum\Format\Timestamp as TimestampFormat,
};
use Innmind\Stream\Readable\Stream;
use Innmind\IO\IO;
use Innmind\Stream\{
Readable\Stream,
Watch\Select,
};
use Innmind\TimeContinuum\Earth\{
ElapsedPeriod,
PointInTime\Now,
Expand All @@ -56,8 +60,6 @@ public function setUp(): void

public function testReadCommand()
{
$read = new FrameReader;

$file = \tmpfile();
\fwrite(
$file,
Expand All @@ -72,20 +74,23 @@ public function testReadCommand()
)->pack()->toString(),
);
\fseek($file, 0);
$stream = Stream::of($file);

$frame = $read($stream, $this->protocol)->match(
static fn($frame) => $frame,
static fn() => null,
);
$frame = IO::of(Select::waitForever(...))
->readable()
->wrap(Stream::of($file))
->toEncoding(Str\Encoding::ascii)
->frames((new FrameReader)($this->protocol))
->one()
->match(
static fn($frame) => $frame,
static fn() => null,
);

$this->assertInstanceOf(Frame::class, $frame);
}

public function testReturnNothingWhenFrameEndMarkerInvalid()
{
$read = new FrameReader;

$file = \tmpfile();
$frame = Frame::method(
new Channel(0),
Expand All @@ -100,18 +105,23 @@ public function testReturnNothingWhenFrameEndMarkerInvalid()
$frame .= (UnsignedOctet::of(0xCD))->pack()->toString();
\fwrite($file, $frame);
\fseek($file, 0);
$stream = Stream::of($file);

$this->assertNull($read($stream, $this->protocol)->match(
static fn($frame) => $frame,
static fn() => null,
));
$frame = IO::of(Select::waitForever(...))
->readable()
->wrap(Stream::of($file))
->toEncoding(Str\Encoding::ascii)
->frames((new FrameReader)($this->protocol))
->one()
->match(
static fn($frame) => $frame,
static fn() => null,
);

$this->assertNull($frame);
}

public function testReturnNothingWhenPayloadTooShort()
{
$read = new FrameReader;

$file = \tmpfile();
$frame = Frame::method(
new Channel(0),
Expand All @@ -120,25 +130,39 @@ public function testReturnNothingWhenPayloadTooShort()
$frame = \mb_substr($frame, 0, -2, 'ASCII');
\fwrite($file, $frame);
\fseek($file, 0);
$stream = Stream::of($file);

$this->assertNull($read($stream, $this->protocol)->match(
static fn($frame) => $frame,
static fn() => null,
));
$frame = IO::of(Select::waitForever(...))
->readable()
->wrap(Stream::of($file))
->toEncoding(Str\Encoding::ascii)
->frames((new FrameReader)($this->protocol))
->one()
->match(
static fn($frame) => $frame,
static fn() => null,
);

$this->assertNull($frame);
}

public function testReturnNothingWhenNoFrameDeteted()
{
$file = \tmpfile();
\fwrite($file, $content = "AMQP\x00\x00\x09\x01");
\fseek($file, 0);
$stream = Stream::of($file);

$this->assertNull((new FrameReader)($stream, $this->protocol)->match(
static fn($frame) => $frame,
static fn() => null,
));
$frame = IO::of(Select::waitForever(...))
->readable()
->wrap(Stream::of($file))
->toEncoding(Str\Encoding::ascii)
->frames((new FrameReader)($this->protocol))
->one()
->match(
static fn($frame) => $frame,
static fn() => null,
);

$this->assertNull($frame);
}

public function testReadHeader()
Expand Down Expand Up @@ -177,10 +201,16 @@ public function testReadHeader()
\fwrite($file, $header->pack()->toString());
\fseek($file, 0);

$frame = (new FrameReader)(Stream::of($file), $this->protocol)->match(
static fn($frame) => $frame,
static fn() => null,
);
$frame = IO::of(Select::waitForever(...))
->readable()
->wrap(Stream::of($file))
->toEncoding(Str\Encoding::ascii)
->frames((new FrameReader)($this->protocol))
->one()
->match(
static fn($frame) => $frame,
static fn() => null,
);

$this->assertInstanceOf(Frame::class, $frame);
$this->assertSame(Type::header, $frame->type());
Expand Down Expand Up @@ -430,10 +460,16 @@ public function testReadBody()
)->pack()->toString());
\fseek($file, 0);

$frame = (new FrameReader)(Stream::of($file), $this->protocol)->match(
static fn($frame) => $frame,
static fn() => null,
);
$frame = IO::of(Select::waitForever(...))
->readable()
->wrap(Stream::of($file))
->toEncoding(Str\Encoding::ascii)
->frames((new FrameReader)($this->protocol))
->one()
->match(
static fn($frame) => $frame,
static fn() => null,
);

$this->assertInstanceOf(Frame::class, $frame);
$this->assertSame(Type::body, $frame->type());
Expand All @@ -451,10 +487,16 @@ public function testReadHeartbeat()
\fwrite($file, Frame::heartbeat()->pack()->toString());
\fseek($file, 0);

$frame = (new FrameReader)(Stream::of($file), $this->protocol)->match(
static fn($frame) => $frame,
static fn() => null,
);
$frame = IO::of(Select::waitForever(...))
->readable()
->wrap(Stream::of($file))
->toEncoding(Str\Encoding::ascii)
->frames((new FrameReader)($this->protocol))
->one()
->match(
static fn($frame) => $frame,
static fn() => null,
);

$this->assertInstanceOf(Frame::class, $frame);
$this->assertSame(Type::heartbeat, $frame->type());
Expand Down
19 changes: 9 additions & 10 deletions tests/Transport/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ public function testInterface()
static fn() => null,
);

$this->assertSame(
$connection,
$this->assertInstanceOf(
SideEffect::class,
$connection
->send(
static fn($protocol) => $protocol->channel()->open(new Channel(1)),
)
->connection()
->match(
static fn($connection) => $connection,
static fn($sideEffect) => $sideEffect,
static fn() => null,
),
);
Expand Down Expand Up @@ -106,9 +105,10 @@ public function testReturnFailureWhenReceivedFrameIsNotTheExpectedOne()
$this->assertSame(
Failure\Kind::unexpectedFrame,
$connection
->send(static fn($protocol) => $protocol->channel()->open(new Channel(2)))
->wait(Method::connectionOpen)
->connection()
->request(
static fn($protocol) => $protocol->channel()->open(new Channel(2)),
Method::connectionOpen,
)
->match(
static fn() => null,
static fn($failure) => $failure->kind(),
Expand All @@ -132,14 +132,13 @@ public function testReturnFailureWhenConnectionClosedByServer()
static fn() => null,
);

$connection = $connection->send(static fn() => Sequence::of(Frame::method(
$_ = $connection->send(static fn() => Sequence::of(Frame::method(
new Channel(0),
Method::of(20, 10),
//missing arguments
)))
->connection()
->match(
static fn($connection) => $connection,
static fn() => null,
static fn() => null,
);
$this->assertSame(
Expand Down
25 changes: 19 additions & 6 deletions tests/Transport/Frame/Value/BitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
Value\Bits,
Value,
};
use Innmind\Stream\Readable\Stream;
use Innmind\Immutable\Sequence;
use Innmind\IO\IO;
use Innmind\Stream\{
Readable\Stream,
Watch\Select,
};
use Innmind\Immutable\{
Sequence,
Str,
};
use PHPUnit\Framework\TestCase;

class BitsTest extends TestCase
Expand All @@ -34,10 +41,16 @@ public function testStringCast($bits, $expected)
*/
public function testFromStream($expected, $string)
{
$value = Bits::unpack(Stream::ofContent($string))->match(
static fn($value) => $value,
static fn() => null,
);
$value = IO::of(Select::waitForever(...))
->readable()
->wrap(Stream::ofContent($string))
->toEncoding(Str\Encoding::ascii)
->frames(Bits::frame())
->one()
->match(
static fn($value) => $value->unwrap(),
static fn() => null,
);

$this->assertInstanceOf(Bits::class, $value);
$this->assertSame($expected, $value->original()->toList());
Expand Down
21 changes: 16 additions & 5 deletions tests/Transport/Frame/Value/DecimalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
Transport\Frame\Value,
};
use Innmind\Math\Exception\OutOfDefinitionSet;
use Innmind\Stream\Readable\Stream;
use Innmind\IO\IO;
use Innmind\Stream\{
Readable\Stream,
Watch\Select,
};
use Innmind\Immutable\Str;
use PHPUnit\Framework\TestCase;

class DecimalTest extends TestCase
Expand Down Expand Up @@ -36,10 +41,16 @@ public function testStringCast($number, $scale, $expected)
*/
public function testFromStream($number, $scale, $string)
{
$value = Decimal::unpack(Stream::ofContent($string))->match(
static fn($value) => $value,
static fn() => null,
);
$value = IO::of(Select::waitForever(...))
->readable()
->wrap(Stream::ofContent($string))
->toEncoding(Str\Encoding::ascii)
->frames(Decimal::frame())
->one()
->match(
static fn($value) => $value->unwrap(),
static fn() => null,
);

$this->assertInstanceOf(Decimal::class, $value);
$this->assertSame(($number / (10**$scale)), $value->original());
Expand Down
20 changes: 15 additions & 5 deletions tests/Transport/Frame/Value/LongStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
Value\LongString,
Value,
};
use Innmind\Stream\Readable\Stream;
use Innmind\IO\IO;
use Innmind\Stream\{
Readable\Stream,
Watch\Select,
};
use Innmind\Immutable\Str;
use PHPUnit\Framework\TestCase;

Expand All @@ -33,10 +37,16 @@ public function testStringCast($string, $expected)
*/
public function testFromStream($expected, $string)
{
$value = LongString::unpack(Stream::ofContent($string))->match(
static fn($value) => $value,
static fn() => null,
);
$value = IO::of(Select::waitForever(...))
->readable()
->wrap(Stream::ofContent($string))
->toEncoding(Str\Encoding::ascii)
->frames(LongString::frame())
->one()
->match(
static fn($value) => $value->unwrap(),
static fn() => null,
);

$this->assertInstanceOf(LongString::class, $value);
$this->assertInstanceOf(Str::class, $value->original());
Expand Down
Loading

0 comments on commit 0937209

Please sign in to comment.