laravel-websockets/tests/Mocks/Connection.php

150 lines
2.8 KiB
PHP
Raw Normal View History

2018-11-25 22:43:43 +00:00
<?php
2020-09-10 19:59:26 +00:00
namespace BeyondCode\LaravelWebSockets\Test\Mocks;
2018-11-25 22:43:43 +00:00
use GuzzleHttp\Psr7\Request;
use PHPUnit\Framework\Assert as PHPUnit;
2020-03-04 09:58:39 +00:00
use Ratchet\ConnectionInterface;
2018-11-25 22:43:43 +00:00
class Connection implements ConnectionInterface
{
2020-08-30 17:00:45 +00:00
/**
* The request instance.
*
* @var Request
*/
2018-11-25 22:43:43 +00:00
public $httpRequest;
2020-08-30 17:00:45 +00:00
/**
* The sent data through the connection.
*
* @var array
*/
2018-11-25 23:20:18 +00:00
public $sentData = [];
2018-11-25 22:43:43 +00:00
2020-08-30 17:00:45 +00:00
/**
* The raw (unencoded) sent data.
*
* @var array
*/
2018-12-03 09:21:35 +00:00
public $sentRawData = [];
2020-08-30 17:00:45 +00:00
/**
* Wether the connection has been closed.
*
* @var bool
*/
2018-11-26 23:00:26 +00:00
public $closed = false;
2020-08-30 17:00:45 +00:00
/**
* Send the data through the connection.
*
* @param mixed $data
* @return void
*/
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
}
2020-08-30 17:00:45 +00:00
/**
* Mark the connection as closed.
*
* @return void
*/
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
}
2020-09-16 08:02:58 +00:00
/**
* Reset the events for assertions.
*
* @return $this
*/
public function resetEvents()
{
$this->sentData = [];
$this->sentRawData = [];
return $this;
}
/**
* Dump & stop execution.
*
* @return void
*/
public function dd()
{
dd([
'sentData' => $this->sentData,
'sentRawData' => $this->sentRawData,
]);
}
2020-08-30 17:00:45 +00:00
/**
* Assert that an event got sent.
*
* @param string $name
* @param array $additionalParameters
* @return $this
2020-08-30 17:00:45 +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);
}
return $this;
2018-11-25 22:43:43 +00:00
}
2018-11-26 23:00:26 +00:00
2020-08-30 17:00:45 +00:00
/**
* Assert that an event got not sent.
*
* @param string $name
* @return $this
2020-08-30 17:00:45 +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)
);
return $this;
2018-11-27 21:26:29 +00:00
}
/**
* Assert that no events occured within the connection.
*
* @return $this
*/
public function assertNothingSent()
{
PHPUnit::assertEquals([], $this->sentData);
return $this;
}
2020-08-30 17:00:45 +00:00
/**
* Assert the connection is closed.
*
* @return $this
2020-08-30 17:00:45 +00:00
*/
2018-11-26 23:00:26 +00:00
public function assertClosed()
{
PHPUnit::assertTrue($this->closed);
return $this;
2018-11-26 23:00:26 +00:00
}
2018-12-04 21:22:33 +00:00
}