diff options
Diffstat (limited to 'tests/Integration')
5 files changed, 129 insertions, 0 deletions
diff --git a/tests/Integration/Extensions/ServerExtension/PostRunSubscriber.php b/tests/Integration/Extensions/ServerExtension/PostRunSubscriber.php new file mode 100644 index 0000000..2495230 --- /dev/null +++ b/tests/Integration/Extensions/ServerExtension/PostRunSubscriber.php @@ -0,0 +1,19 @@ +<?php + +namespace Tests\Integration\Extensions\ServerExtension; + +use PHPUnit\Event\TestRunner\ExecutionFinished; +use PHPUnit\Event\TestRunner\ExecutionFinishedSubscriber; + +class PostRunSubscriber implements ExecutionFinishedSubscriber +{ + public function __construct(private readonly \stdClass $dataObject) + {} + + public function notify(ExecutionFinished $event): void + { + if ($this->dataObject->process_id > 0) { + exec("kill {$this->dataObject->process_id}"); + } + } +} diff --git a/tests/Integration/Extensions/ServerExtension/PreRunSubscriber.php b/tests/Integration/Extensions/ServerExtension/PreRunSubscriber.php new file mode 100644 index 0000000..4ab4180 --- /dev/null +++ b/tests/Integration/Extensions/ServerExtension/PreRunSubscriber.php @@ -0,0 +1,18 @@ +<?php + +namespace Tests\Integration\Extensions\ServerExtension; + +use PHPUnit\Event\TestRunner\ExecutionStarted; +use PHPUnit\Event\TestRunner\ExecutionStartedSubscriber; + +class PreRunSubscriber implements ExecutionStartedSubscriber +{ + public function __construct(private \stdClass $dataObject) + {} + + public function notify(ExecutionStarted $event): void + { + $this->dataObject->process_id = (int)shell_exec("php -S localhost:8080 -t public > /dev/null 2>&1 & echo $!"); + sleep(1); + } +} diff --git a/tests/Integration/Extensions/ServerExtension/ServerExtension.php b/tests/Integration/Extensions/ServerExtension/ServerExtension.php new file mode 100644 index 0000000..a94bcb3 --- /dev/null +++ b/tests/Integration/Extensions/ServerExtension/ServerExtension.php @@ -0,0 +1,19 @@ +<?php + +namespace Tests\Integration\Extensions\ServerExtension; + +use PHPUnit\Runner\Extension\Extension; +use PHPUnit\Runner\Extension\Facade; +use PHPUnit\Runner\Extension\ParameterCollection; +use PHPUnit\TextUI\Configuration\Configuration; + +class ServerExtension implements Extension +{ + public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void + { + $dataObject = new \stdClass(); + + $facade->registerSubscriber(new PreRunSubscriber($dataObject)); + $facade->registerSubscriber(new PostRunSubscriber($dataObject)); + } +} diff --git a/tests/Integration/ServerImplementationTest.php b/tests/Integration/ServerImplementationTest.php new file mode 100644 index 0000000..c1c1823 --- /dev/null +++ b/tests/Integration/ServerImplementationTest.php @@ -0,0 +1,36 @@ +<?php + +namespace Tests\Integration; + +use Symfony\Component\HttpFoundation\Response; +use Tests\Integration\TestCases\HttpResponseTestCase; + +class ServerImplementationTest extends HttpResponseTestCase +{ + public function testVersion(): void + { + $response = $this->request("GET", "/_matrix/federation/v1/version"); + + $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); + $this->assertTrue($this->hasJsonBody($response)); + + $body = json_decode((string)$response->getBody(), true); + + $this->assertArrayHasKey("server", $body); + $this->assertArrayHasKey("name", $body["server"]); + $this->assertArrayHasKey("version", $body["server"]); + } + + public function testVersions(): void + { + $response = $this->request("GET", "/_matrix/client/versions"); + + $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); + $this->assertTrue($this->hasJsonBody($response)); + + $body = json_decode((string)$response->getBody(), true); + + $this->assertIsArray($body["versions"]); + $this->assertTrue(count($body["versions"]) > 0); + } +} diff --git a/tests/Integration/TestCases/HttpResponseTestCase.php b/tests/Integration/TestCases/HttpResponseTestCase.php new file mode 100644 index 0000000..43b4859 --- /dev/null +++ b/tests/Integration/TestCases/HttpResponseTestCase.php @@ -0,0 +1,37 @@ +<?php + +namespace Tests\Integration\TestCases; + +use GuzzleHttp\Client; +use GuzzleHttp\Psr7\Response; +use PHPUnit\Framework\TestCase; + +class HttpResponseTestCase extends TestCase +{ + protected Client $client; + + protected function setUp(): void + { + $this->client = new Client([ + "base_uri" => "http://localhost:8080", + ]); + } + + /** + * @param array $options + */ + public function request(string $method, string $path, array $options = []): Response + { + return $this->client->request( + $method, + $path, + ["http_errors" => false] + $options + ); + } + + public function hasJsonBody(Response $response): bool + { + json_decode((string)$response->getBody()); + return json_last_error() === JSON_ERROR_NONE; + } +} |