laravel-websockets/tests/Mocks/Connection.php

57 lines
1.2 KiB
PHP
Raw Normal View History

2018-11-25 22:43:43 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\Tests\Mocks;
use GuzzleHttp\Psr7\Request;
use Ratchet\ConnectionInterface;
use PHPUnit\Framework\Assert as PHPUnit;
class Connection implements ConnectionInterface
{
/** @var Request */
public $httpRequest;
2018-11-25 23:20:18 +00:00
public $sentData = [];
2018-11-25 22:43:43 +00:00
2018-12-03 09:21:35 +00:00
public $sentRawData = [];
2018-11-26 23:00:26 +00:00
public $closed = false;
2018-11-28 22:59:58 +00:00
public function send($data)
2018-11-25 22:43:43 +00:00
{
$this->sentData[] = json_decode($data, true);
2018-12-03 09:21:35 +00:00
$this->sentRawData[] = $data;
2018-11-25 22:43:43 +00:00
}
2018-11-28 22:59:58 +00:00
public function close()
2018-11-25 22:43:43 +00:00
{
2018-11-26 23:00:26 +00:00
$this->closed = true;
2018-11-25 22:43:43 +00:00
}
2018-11-25 23:20:18 +00:00
public function assertSentEvent(string $name, array $additionalParameters = [])
2018-11-25 22:43:43 +00:00
{
2018-11-25 23:20:18 +00:00
$event = collect($this->sentData)->firstWhere('event', '=', $name);
2018-11-25 22:43:43 +00:00
PHPUnit::assertTrue(
2018-11-25 23:20:18 +00:00
! is_null($event)
2018-11-25 22:43:43 +00:00
);
2018-11-25 23:20:18 +00:00
foreach ($additionalParameters as $parameter => $value) {
PHPUnit::assertSame($event[$parameter], $value);
}
2018-11-25 22:43:43 +00:00
}
2018-11-26 23:00:26 +00:00
2018-11-27 21:26:29 +00:00
public function assertNotSentEvent(string $name)
{
$event = collect($this->sentData)->firstWhere('event', '=', $name);
PHPUnit::assertTrue(
is_null($event)
);
}
2018-11-26 23:00:26 +00:00
public function assertClosed()
{
PHPUnit::assertTrue($this->closed);
}
2018-11-25 22:43:43 +00:00
}