From 865f003b980ff05908ce6cb57fc3e99a55a14bbe Mon Sep 17 00:00:00 2001 From: Shift Date: Mon, 7 Oct 2024 22:26:20 +0000 Subject: [PATCH 1/8] Add Pest dependencies --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index c9f6469..2b9f5d3 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ } ], "minimum-stability": "dev", - "prefer-stable" : true, + "prefer-stable": true, "require": { "php": "^8.1", "ext-curl": "*", @@ -29,9 +29,9 @@ "mockery/mockery": "^1.4", "phpmd/phpmd": "^2.9", "phpstan/phpstan": "^1.0", - "phpunit/phpunit": "^9.5", "rector/rector": "^0.14.8", - "scrutinizer/ocular": "^1.8" + "scrutinizer/ocular": "^1.8", + "pestphp/pest": "^2.2" }, "autoload": { "psr-4": { From 6556e2352e19b97581e1e9fba3ff5591ce5265ca Mon Sep 17 00:00:00 2001 From: Shift Date: Mon, 7 Oct 2024 22:26:20 +0000 Subject: [PATCH 2/8] Add base Pest file --- tests/Pest.php | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/Pest.php diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..fc70c72 --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,40 @@ + Date: Mon, 7 Oct 2024 22:26:20 +0000 Subject: [PATCH 3/8] Convert test cases --- tests/AdminManagerTest.php | 63 ++- tests/ArangoClientTest.php | 387 ++++++++--------- tests/ExceptionsTest.php | 37 +- tests/MonitorManagerTest.php | 65 ++- tests/SchemaManagerAnalyzersTest.php | 146 +++---- tests/SchemaManagerCollectionsTest.php | 303 +++++++------- tests/SchemaManagerDatabasesTest.php | 79 ++-- tests/SchemaManagerGraphsTest.php | 558 ++++++++++++------------- tests/SchemaManagerIndexesTest.php | 162 ++++--- tests/SchemaManagerUsersTest.php | 237 +++++------ tests/SchemaManagerViewsTest.php | 160 +++---- tests/StatementTest.php | 225 +++++----- tests/SupportsTransactionsTest.php | 188 ++++----- tests/TransactionManagerTest.php | 244 +++++------ 14 files changed, 1318 insertions(+), 1536 deletions(-) diff --git a/tests/AdminManagerTest.php b/tests/AdminManagerTest.php index 5d0a8e7..303f4fa 100644 --- a/tests/AdminManagerTest.php +++ b/tests/AdminManagerTest.php @@ -2,42 +2,31 @@ declare(strict_types=1); -namespace Tests; - use ArangoClient\Admin\AdminManager; -class AdminManagerTest extends TestCase -{ - protected AdminManager $adminManager; - - protected function setUp(): void - { - parent::setUp(); - - $this->adminManager = new AdminManager($this->arangoClient); - } - - public function testGetVersion() - { - $result = $this->adminManager->getVersion(); - - $this->assertSame('arango', $result->server); - $this->assertSame('community', $result->license); - $this->assertIsString($result->version); - } - - public function testGetVersionWithDetails() - { - $result = $this->adminManager->getVersion(true); - - $this->assertSame('arango', $result->server); - $this->assertSame('community', $result->license); - $this->assertIsString($result->version); - } - - public function testGetRunningTransactions() - { - $transactions = $this->adminManager->getRunningTransactions(); - $this->assertEmpty($transactions); - } -} +uses(Tests\TestCase::class); +beforeEach(function () { + $this->adminManager = new AdminManager($this->arangoClient); +}); + + +test('get version', function () { + $result = $this->adminManager->getVersion(); + + $this->assertSame('arango', $result->server); + $this->assertSame('community', $result->license); + $this->assertIsString($result->version); +}); + +test('get version with details', function () { + $result = $this->adminManager->getVersion(true); + + $this->assertSame('arango', $result->server); + $this->assertSame('community', $result->license); + $this->assertIsString($result->version); +}); + +test('get running transactions', function () { + $transactions = $this->adminManager->getRunningTransactions(); + $this->assertEmpty($transactions); +}); diff --git a/tests/ArangoClientTest.php b/tests/ArangoClientTest.php index 2f81718..bb8da98 100644 --- a/tests/ArangoClientTest.php +++ b/tests/ArangoClientTest.php @@ -2,8 +2,6 @@ declare(strict_types=1); -namespace Tests; - use ArangoClient\Admin\AdminManager; use ArangoClient\ArangoClient; use ArangoClient\Schema\SchemaManager; @@ -16,255 +14,236 @@ use Mockery; use stdClass; -class ArangoClientTest extends TestCase -{ - public function testGetConfig() - { - $defaultConfig = [ - 'endpoint' => 'http://localhost:8529', - 'host' => null, - 'port' => null, - 'version' => 1.1, - 'connection' => 'Keep-Alive', - 'allow_redirects' => false, - 'connect_timeout' => 0.0, - 'username' => 'root', - 'password' => null, - 'database' => $this->testDatabaseName, - 'jsonStreamDecoderThreshold' => 1048576, - ]; - - $config = $this->arangoClient->getConfig(); - $this->assertSame($defaultConfig, $config); - } +uses(Tests\TestCase::class); + +test('get config', function () { + $defaultConfig = [ + 'endpoint' => 'http://localhost:8529', + 'host' => null, + 'port' => null, + 'version' => 1.1, + 'connection' => 'Keep-Alive', + 'allow_redirects' => false, + 'connect_timeout' => 0.0, + 'username' => 'root', + 'password' => null, + 'database' => $this->testDatabaseName, + 'jsonStreamDecoderThreshold' => 1048576, + ]; + + $config = $this->arangoClient->getConfig(); + $this->assertSame($defaultConfig, $config); +}); + +test('get config with endpoint without host port', function () { + $config = [ + 'endpoint' => 'http://localhost:8529', + 'username' => 'root', + ]; + + $returnedConfig = $this->arangoClient->getConfig(); + $this->assertSame($config['endpoint'], $returnedConfig['endpoint']); +}); + +test('client with host port config', function () { + $config = [ + 'host' => 'http://127.0.0.1', + 'port' => '1234', + 'username' => 'root', + ]; + $client = new ArangoClient($config); + $retrievedConfig = $client->getConfig(); + + $this->assertEquals('http://127.0.0.1:1234', $retrievedConfig['endpoint']); +}); + +test('config with alien properties', function () { + $config = [ + 'name' => 'arangodb', + 'driver' => 'arangodb', + 'host' => 'http://127.0.0.1', + 'port' => '1234', + 'username' => 'root', + ]; + $client = new ArangoClient($config); + $retrievedConfig = $client->getConfig(); - public function testGetConfigWithEndpointWithoutHostPort() - { - $config = [ - 'endpoint' => 'http://localhost:8529', - 'username' => 'root', - ]; + $this->assertArrayNotHasKey('name', $retrievedConfig); + $this->assertArrayNotHasKey('driver', $retrievedConfig); +}); - $returnedConfig = $this->arangoClient->getConfig(); - $this->assertSame($config['endpoint'], $returnedConfig['endpoint']); - } +test('set and get http client', function () { + $oldClient = $this->arangoClient->getHttpClient(); - public function testClientWithHostPortConfig() - { - $config = [ - 'host' => 'http://127.0.0.1', - 'port' => '1234', - 'username' => 'root', - ]; - $client = new ArangoClient($config); - $retrievedConfig = $client->getConfig(); - - $this->assertEquals('http://127.0.0.1:1234', $retrievedConfig['endpoint']); - } + $newClient = Mockery::mock(Client::class); + $this->arangoClient->setHttpClient($newClient); + $retrievedClient = $this->arangoClient->getHttpClient(); - public function testConfigWithAlienProperties() - { - $config = [ - 'name' => 'arangodb', - 'driver' => 'arangodb', - 'host' => 'http://127.0.0.1', - 'port' => '1234', - 'username' => 'root', - ]; - $client = new ArangoClient($config); - $retrievedConfig = $client->getConfig(); - - $this->assertArrayNotHasKey('name', $retrievedConfig); - $this->assertArrayNotHasKey('driver', $retrievedConfig); - } + $this->assertInstanceOf(Client::class, $oldClient); + $this->assertEquals($newClient::class, $retrievedClient::class); +}); - public function testSetAndGetHttpClient() - { - $oldClient = $this->arangoClient->getHttpClient(); +test('request', function () { + $result = $this->arangoClient->request('get', '/_api/version', []); - $newClient = Mockery::mock(Client::class); - $this->arangoClient->setHttpClient($newClient); - $retrievedClient = $this->arangoClient->getHttpClient(); + $this->assertSame('arango', $result->server); + $this->assertSame('community', $result->license); + $this->assertIsString($result->version); +}); - $this->assertInstanceOf(Client::class, $oldClient); - $this->assertEquals($newClient::class, $retrievedClient::class); - } +test('get user', function () { + $user = $this->arangoClient->getUser(); + $this->assertSame('root', $user); +}); - public function testRequest() - { - $result = $this->arangoClient->request('get', '/_api/version', []); +test('set and get database name', function () { + $database = $this->arangoClient->getDatabase(); + $this->assertSame($this->testDatabaseName, $database); - $this->assertSame('arango', $result->server); - $this->assertSame('community', $result->license); - $this->assertIsString($result->version); - } + $newDatabaseName = 'ArangoClientDB'; + $this->arangoClient->setDatabase($newDatabaseName); + + $database = $this->arangoClient->getDatabase(); + $this->assertSame($newDatabaseName, $database); +}); - public function testGetUser() - { - $user = $this->arangoClient->getUser(); - $this->assertSame('root', $user); +test('database name is used in requests', function () { + $database = 'some_database'; + if (!$this->arangoClient->schema()->hasDatabase($database)) { + $this->arangoClient->schema()->createDatabase($database); } - public function testSetAndGetDatabaseName() - { - $database = $this->arangoClient->getDatabase(); - $this->assertSame($this->testDatabaseName, $database); + $uri = '/_api/collection'; - $newDatabaseName = 'ArangoClientDB'; - $this->arangoClient->setDatabase($newDatabaseName); + $container = []; + $history = Middleware::history($container); + $mock = new MockHandler([ + new Response(200, ['X-Foo' => 'Bar'], '{}'), + ]); + $handlerStack = HandlerStack::create($mock); + $handlerStack->push($history); - $database = $this->arangoClient->getDatabase(); - $this->assertSame($newDatabaseName, $database); - } + $this->arangoClient->setDatabase($database); - public function testDatabaseNameIsUsedInRequests() - { - $database = 'some_database'; - if (!$this->arangoClient->schema()->hasDatabase($database)) { - $this->arangoClient->schema()->createDatabase($database); - } + $this->arangoClient->request('get', $uri, ['handler' => $handlerStack]); - $uri = '/_api/collection'; + foreach ($container as $transaction) { + $this->assertSame('/_db/' . $database . $uri, $transaction['request']->getUri()->getPath()); + } - $container = []; - $history = Middleware::history($container); - $mock = new MockHandler([ - new Response(200, ['X-Foo' => 'Bar'], '{}'), - ]); - $handlerStack = HandlerStack::create($mock); - $handlerStack->push($history); + $this->arangoClient->schema()->deleteDatabase($database); +}); - $this->arangoClient->setDatabase($database); +test('schema', function () { + $result = $this->arangoClient->schema(); + $this->assertInstanceOf(SchemaManager::class, $result); - $this->arangoClient->request('get', $uri, ['handler' => $handlerStack]); + $database = $this->arangoClient->schema()->getCurrentDatabase(); - foreach ($container as $transaction) { - $this->assertSame('/_db/' . $database . $uri, $transaction['request']->getUri()->getPath()); - } + $this->assertObjectHasProperty('name', $database); +}); - $this->arangoClient->schema()->deleteDatabase($database); - } +test('admin', function () { + $result = $this->arangoClient->admin(); + $this->assertInstanceOf(AdminManager::class, $result); - public function testSchema() - { - $result = $this->arangoClient->schema(); - $this->assertInstanceOf(SchemaManager::class, $result); + $version = $this->arangoClient->admin()->getVersion(); - $database = $this->arangoClient->schema()->getCurrentDatabase(); + $this->assertObjectHasProperty('version', $version); +}); - $this->assertObjectHasProperty('name', $database); - } +test('prepare', function () { + $statement = $this->arangoClient->prepare('FOR doc IN users RETURN doc'); - public function testAdmin() - { - $result = $this->arangoClient->admin(); - $this->assertInstanceOf(AdminManager::class, $result); + $this->assertInstanceOf(Statement::class, $statement); +}); - $version = $this->arangoClient->admin()->getVersion(); +test('connection protocol version', function () { + checkHttp2Support(); - $this->assertObjectHasProperty('version', $version); - } + $uri = '/_api/version'; - public function testPrepare() - { - $statement = $this->arangoClient->prepare('FOR doc IN users RETURN doc'); + $options = []; + $options['version'] = 2; + $response = $this->arangoClient->debugRequest('get', $uri, $options); - $this->assertInstanceOf(Statement::class, $statement); - } + $this->assertEquals(2, $response->getProtocolVersion()); +}); - public function testConnectionProtocolVersion() - { - $this->checkHttp2Support(); +test('connection protocol version with default setting', function () { + checkHttp2Support(); - $uri = '/_api/version'; + $uri = '/_api/version'; - $options = []; - $options['version'] = 2; - $response = $this->arangoClient->debugRequest('get', $uri, $options); + $client = new ArangoClient(['username' => 'root', 'version' => 2.0]); - $this->assertEquals(2, $response->getProtocolVersion()); - } + $options = []; + $options['version'] = 2; + $response = $this->arangoClient->debugRequest('get', $uri, $options); - public function testConnectionProtocolVersionWithDefaultSetting() - { - $this->checkHttp2Support(); + $this->assertEquals(2, $response->getProtocolVersion()); +}); - $uri = '/_api/version'; +test('json encode', function () { + $results = $this->arangoClient->jsonEncode([]); - $client = new ArangoClient(['username' => 'root', 'version' => 2.0]); + $this->assertSame('{}', $results); +}); - $options = []; - $options['version'] = 2; - $response = $this->arangoClient->debugRequest('get', $uri, $options); +test('json encode empty array', function () { + $results = $this->arangoClient->jsonEncode([]); - $this->assertEquals(2, $response->getProtocolVersion()); - } + $this->assertSame('{}', $results); +}); - public function testJsonEncode() - { - $results = $this->arangoClient->jsonEncode([]); +test('json encode empty string', function () { + $results = $this->arangoClient->jsonEncode(''); - $this->assertSame('{}', $results); - } + $this->assertSame('""', $results); +}); - public function testJsonEncodeEmptyArray() - { - $results = $this->arangoClient->jsonEncode([]); +test('json encode invalid data', function () { + $data = []; + $data[] = "\xB1\x31"; + $this->expectExceptionCode(JSON_ERROR_UTF8); + $this->arangoClient->jsonEncode($data); +}); - $this->assertSame('{}', $results); +test('response data matches request data', function () { + $collection = 'users'; + if (!$this->schemaManager->hasCollection($collection)) { + $this->schemaManager->createCollection($collection); } + $location = new stdClass(); + $location->address = 'Voughtstreet 10'; + $location->city = 'New York'; - public function testJsonEncodeEmptyString() - { - $results = $this->arangoClient->jsonEncode(''); + $user = new stdClass(); + $user->name = 'Soldier Boy'; + $user->location = $location; - $this->assertSame('""', $results); - } + $insertQuery = 'INSERT ' . json_encode($user, JSON_THROW_ON_ERROR) . ' INTO ' . $collection . ' RETURN NEW'; + $insertStatement = $this->arangoClient->prepare($insertQuery); + $insertStatement->execute(); + $insertResult = $insertStatement->fetchAll(); - public function testJsonEncodeInvalidData() - { - $data = []; - $data[] = "\xB1\x31"; - $this->expectExceptionCode(JSON_ERROR_UTF8); - $this->arangoClient->jsonEncode($data); - } + $query = 'FOR doc IN ' . $collection . ' RETURN doc'; + $statement = $this->arangoClient->prepare($query); + $statement->execute(); + $users = $statement->fetchAll(); - public function testResponseDataMatchesRequestData() - { - $collection = 'users'; - if (!$this->schemaManager->hasCollection($collection)) { - $this->schemaManager->createCollection($collection); - } - $location = new stdClass(); - $location->address = 'Voughtstreet 10'; - $location->city = 'New York'; - - $user = new stdClass(); - $user->name = 'Soldier Boy'; - $user->location = $location; - - $insertQuery = 'INSERT ' . json_encode($user, JSON_THROW_ON_ERROR) . ' INTO ' . $collection . ' RETURN NEW'; - $insertStatement = $this->arangoClient->prepare($insertQuery); - $insertStatement->execute(); - $insertResult = $insertStatement->fetchAll(); - - $query = 'FOR doc IN ' . $collection . ' RETURN doc'; - $statement = $this->arangoClient->prepare($query); - $statement->execute(); - $users = $statement->fetchAll(); - - $this->assertEquals($insertResult[0], $users[0]); - - $this->schemaManager->deleteCollection($collection); - } + $this->assertEquals($insertResult[0], $users[0]); - protected function checkHttp2Support() - { - // First assert that CURL supports http2! - if (!curl_version()['features'] || CURL_VERSION_HTTP2 === 0) { - $this->markTestSkipped('The installed version of CURL does not support the HTTP2 protocol.'); - } - // HTTP/2 is only supported by ArangoDB 3.7 and up. - $this->skipTestOnArangoVersions('3.7'); + $this->schemaManager->deleteCollection($collection); +}); + +// Helpers +function checkHttp2Support() +{ + // First assert that CURL supports http2! + if (!curl_version()['features'] || CURL_VERSION_HTTP2 === 0) { + test()->markTestSkipped('The installed version of CURL does not support the HTTP2 protocol.'); } + // HTTP/2 is only supported by ArangoDB 3.7 and up. + test()->skipTestOnArangoVersions('3.7'); } diff --git a/tests/ExceptionsTest.php b/tests/ExceptionsTest.php index 5a122a2..3292201 100644 --- a/tests/ExceptionsTest.php +++ b/tests/ExceptionsTest.php @@ -1,28 +1,23 @@ schemaManager->hasDatabase($database)) { - $this->schemaManager->createDatabase($database); - } +declare(strict_types=1); - $this->expectExceptionCode(409); +test('test409 conflict exception', function () { + $database = 'test_arangodb_php_existing_database'; + if (!$this->schemaManager->hasDatabase($database)) { $this->schemaManager->createDatabase($database); - - $this->schemaManager->deleteDatabase($database); } - public function testCallsToNoneExistingDbThrow() - { - $this->arangoClient->setDatabase('NoneExistingDb'); - $this->expectExceptionCode(404); - $this->schemaManager->hasCollection('dummy'); - } -} + $this->expectExceptionCode(409); + $this->schemaManager->createDatabase($database); + + $this->schemaManager->deleteDatabase($database); +}); + +test('calls to none existing db throw', function () { + $this->arangoClient->setDatabase('NoneExistingDb'); + $this->expectExceptionCode(404); + $this->schemaManager->hasCollection('dummy'); +}); diff --git a/tests/MonitorManagerTest.php b/tests/MonitorManagerTest.php index c5cb8fc..6d0b893 100644 --- a/tests/MonitorManagerTest.php +++ b/tests/MonitorManagerTest.php @@ -2,25 +2,21 @@ declare(strict_types=1); -namespace Tests; - use ArangoClient\Prometheus\Prometheus; -class MonitorManagerTest extends TestCase -{ - public function testGetMetrics() - { - $result = $this->arangoClient->monitor()->getMetrics(); - $this->assertIsObject($result); - $this->assertEquals("gauge", $result->arangodb_agency_cache_callback_number->type); - $this->assertCount(1, $result->arangodb_agency_cache_callback_number->labels); - } +uses(Tests\TestCase::class); + +test('get metrics', function () { + $result = $this->arangoClient->monitor()->getMetrics(); + $this->assertIsObject($result); + $this->assertEquals("gauge", $result->arangodb_agency_cache_callback_number->type); + $this->assertCount(1, $result->arangodb_agency_cache_callback_number->labels); +}); - public function testSummaryMetric() - { - $prometheus = new Prometheus(); +test('summary metric', function () { + $prometheus = new Prometheus(); - $rawMetrics = '# HELP prometheus_rule_evaluation_duration_seconds The duration for a rule to execute. + $rawMetrics = '# HELP prometheus_rule_evaluation_duration_seconds The duration for a rule to execute. # TYPE prometheus_rule_evaluation_duration_seconds summary prometheus_rule_evaluation_duration_seconds{quantile="0.5"} 6.4853e-05 prometheus_rule_evaluation_duration_seconds{quantile="0.9"} 0.00010102 @@ -29,15 +25,14 @@ public function testSummaryMetric() prometheus_rule_evaluation_duration_seconds_count 1.112293682e+09'; - $result = $prometheus->parseText($rawMetrics); - $this->assertIsObject($result); - } + $result = $prometheus->parseText($rawMetrics); + $this->assertIsObject($result); +}); - public function testHistorgramParsing() - { - $prometheus = new Prometheus(); +test('historgram parsing', function () { + $prometheus = new Prometheus(); - $rawMetrics = '# HELP arangodb_aql_query_time Execution time histogram for all AQL queries [s] + $rawMetrics = '# HELP arangodb_aql_query_time Execution time histogram for all AQL queries [s] # TYPE arangodb_aql_query_time histogram arangodb_aql_query_time_bucket{role="SINGLE",le="0.000095"} 36 2211753600 arangodb_aql_query_time_bucket{role="SINGLE",le="0.000191"} 157 2211753601 @@ -63,22 +58,20 @@ public function testHistorgramParsing() arangodb_aql_query_time_sum{role="SINGLE"} 0.035180 '; - $result = $prometheus->parseText($rawMetrics); - $this->assertCount(20, $result->arangodb_aql_query_time->buckets); - $this->assertEquals(177, $result->arangodb_aql_query_time->count); - $this->assertEquals(0.03518, $result->arangodb_aql_query_time->sum); - } + $result = $prometheus->parseText($rawMetrics); + $this->assertCount(20, $result->arangodb_aql_query_time->buckets); + $this->assertEquals(177, $result->arangodb_aql_query_time->count); + $this->assertEquals(0.03518, $result->arangodb_aql_query_time->sum); +}); - public function testTimestampParsing() - { - $prometheus = new Prometheus(); +test('timestamp parsing', function () { + $prometheus = new Prometheus(); - $rawMetrics = '# HELP arangodb_aql_local_query_memory_limit_reached_total Number of local AQL query memory limit violations + $rawMetrics = '# HELP arangodb_aql_local_query_memory_limit_reached_total Number of local AQL query memory limit violations # TYPE arangodb_aql_local_query_memory_limit_reached_total counter arangodb_aql_local_query_memory_limit_reached_total{role="SINGLE"} 0 2211753600'; - $result = $prometheus->parseText($rawMetrics); - $this->assertIsObject($result); - $this->assertEquals(2211753600, $result->arangodb_aql_local_query_memory_limit_reached_total->timestamp); - } -} + $result = $prometheus->parseText($rawMetrics); + $this->assertIsObject($result); + $this->assertEquals(2211753600, $result->arangodb_aql_local_query_memory_limit_reached_total->timestamp); +}); diff --git a/tests/SchemaManagerAnalyzersTest.php b/tests/SchemaManagerAnalyzersTest.php index 790d605..debaa39 100644 --- a/tests/SchemaManagerAnalyzersTest.php +++ b/tests/SchemaManagerAnalyzersTest.php @@ -1,108 +1,90 @@ 'testAnalyzerBasics', - 'type' => 'identity', - ]; +uses(Tests\TestCase::class); - protected function setUp(): void - { - \Tests\TestCase::setUp(); +declare(strict_types=1); +beforeEach(function () { + \Tests\TestCase::setUp(); - if (!$this->schemaManager->hasAnalyzer($this->analyzer['name'])) { - $this->schemaManager->createAnalyzer($this->analyzer); - } + if (!$this->schemaManager->hasAnalyzer($this->analyzer['name'])) { + $this->schemaManager->createAnalyzer($this->analyzer); } +}); - protected function tearDown(): void - { - \Tests\TestCase::tearDown(); +afterEach(function () { + \Tests\TestCase::tearDown(); - if ($this->schemaManager->hasAnalyzer($this->analyzer['name'])) { - $this->schemaManager->deleteAnalyzer($this->analyzer['name']); - } + if ($this->schemaManager->hasAnalyzer($this->analyzer['name'])) { + $this->schemaManager->deleteAnalyzer($this->analyzer['name']); } +}); - public function testGetAnalyzers() - { - $analyzers = $this->schemaManager->getAnalyzers(); - $customAnalyzer = end($analyzers); +test('get analyzers', function () { + $analyzers = $this->schemaManager->getAnalyzers(); - $this->assertSame('arangodb_php_client__test::' . $this->analyzer['name'], $customAnalyzer->name); - } + $customAnalyzer = end($analyzers); - public function testGetAnalyzer() - { - $analyzer = $this->schemaManager->getAnalyzer($this->analyzer['name']); + $this->assertSame('arangodb_php_client__test::' . $this->analyzer['name'], $customAnalyzer->name); +}); - $this->assertSame('arangodb_php_client__test::' . $this->analyzer['name'], $analyzer->name); - $this->assertObjectHasProperty('type', $analyzer); - } +test('get analyzer', function () { + $analyzer = $this->schemaManager->getAnalyzer($this->analyzer['name']); - public function testGetAnalyzerWithFullName() - { - $analyzer = $this->schemaManager->getAnalyzer('arangodb_php_client__test::' . $this->analyzer['name']); + $this->assertSame('arangodb_php_client__test::' . $this->analyzer['name'], $analyzer->name); + $this->assertObjectHasProperty('type', $analyzer); +}); - $this->assertSame('arangodb_php_client__test::' . $this->analyzer['name'], $analyzer->name); - $this->assertObjectHasProperty('type', $analyzer); - } +test('get analyzer with full name', function () { + $analyzer = $this->schemaManager->getAnalyzer('arangodb_php_client__test::' . $this->analyzer['name']); + $this->assertSame('arangodb_php_client__test::' . $this->analyzer['name'], $analyzer->name); + $this->assertObjectHasProperty('type', $analyzer); +}); - public function testHasAnalyzer() - { - $result = $this->schemaManager->hasAnalyzer($this->analyzer['name']); - $this->assertTrue($result); +test('has analyzer', function () { + $result = $this->schemaManager->hasAnalyzer($this->analyzer['name']); + $this->assertTrue($result); - $result = $this->schemaManager->hasAnalyzer('someNoneExistingAnalyzer'); - $this->assertFalse($result); - } + $result = $this->schemaManager->hasAnalyzer('someNoneExistingAnalyzer'); + $this->assertFalse($result); +}); - public function testReplaceAnalyzer() - { - $newAnalyzerProps = [ - 'name' => 'newAnalyzer', - 'type' => 'identity', - ]; - ; - $newAnalyzer = $this->schemaManager->replaceAnalyzer($this->analyzer['name'], $newAnalyzerProps); +test('replace analyzer', function () { + $newAnalyzerProps = [ + 'name' => 'newAnalyzer', + 'type' => 'identity', + ]; + ; + $newAnalyzer = $this->schemaManager->replaceAnalyzer($this->analyzer['name'], $newAnalyzerProps); - $this->assertSame('arangodb_php_client__test::' . $this->analyzer['name'], $newAnalyzer->name); - } + $this->assertSame('arangodb_php_client__test::' . $this->analyzer['name'], $newAnalyzer->name); +}); - public function testCreateAndDeleteAnalyzer() - { - $analyzer = [ - 'name' => 'coolnewanalyzer', - 'type' => 'identity', - ]; - $created = $this->schemaManager->createAnalyzer($analyzer); - $this->assertObjectHasProperty('name', $created); - $this->assertSame('arangodb_php_client__test::' . $analyzer['name'], $created->name); - - $deleted = $this->schemaManager->deleteAnalyzer($analyzer['name']); - $this->assertTrue($deleted); - } +test('create and delete analyzer', function () { + $analyzer = [ + 'name' => 'coolnewanalyzer', + 'type' => 'identity', + ]; + $created = $this->schemaManager->createAnalyzer($analyzer); + $this->assertObjectHasProperty('name', $created); + $this->assertSame('arangodb_php_client__test::' . $analyzer['name'], $created->name); - public function testDeleteWithFullName() - { - $analyzer = [ - 'name' => 'coolnewanalyzer', - 'type' => 'identity', - ]; - $created = $this->schemaManager->createAnalyzer($analyzer); + $deleted = $this->schemaManager->deleteAnalyzer($analyzer['name']); + $this->assertTrue($deleted); +}); - $fullName = 'arangodb_php_client__test::' . $analyzer['name']; +test('delete with full name', function () { + $analyzer = [ + 'name' => 'coolnewanalyzer', + 'type' => 'identity', + ]; + $created = $this->schemaManager->createAnalyzer($analyzer); - $deleted = $this->schemaManager->deleteAnalyzer($fullName); + $fullName = 'arangodb_php_client__test::' . $analyzer['name']; - $hasAnalyzer = $this->schemaManager->hasAnalyzer($fullName); - $this->assertFalse($hasAnalyzer); - } + $deleted = $this->schemaManager->deleteAnalyzer($fullName); -} + $hasAnalyzer = $this->schemaManager->hasAnalyzer($fullName); + $this->assertFalse($hasAnalyzer); +}); diff --git a/tests/SchemaManagerCollectionsTest.php b/tests/SchemaManagerCollectionsTest.php index 055bd02..b067946 100644 --- a/tests/SchemaManagerCollectionsTest.php +++ b/tests/SchemaManagerCollectionsTest.php @@ -1,217 +1,198 @@ skipTestOnArangoVersions('3.8', '>='); + $result = $this->schemaManager->getCollections(); -class SchemaManagerCollectionsTest extends TestCase -{ - public function testGetCollectionsBeforeVersion38() - { - $this->skipTestOnArangoVersions('3.8', '>='); - $result = $this->schemaManager->getCollections(); + $this->assertLessThanOrEqual(count($result), 10); + $this->assertIsObject($result[0]); +}); - $this->assertLessThanOrEqual(count($result), 10); - $this->assertIsObject($result[0]); - } +test('get collections', function () { + $this->skipTestOnArangoVersions('3.8', '<'); + $result = $this->schemaManager->getCollections(); - public function testGetCollections() - { - $this->skipTestOnArangoVersions('3.8', '<'); - $result = $this->schemaManager->getCollections(); + $this->assertLessThanOrEqual(count($result), 8); + $this->assertIsObject($result[0]); +}); - $this->assertLessThanOrEqual(count($result), 8); - $this->assertIsObject($result[0]); - } +test('get collections without system', function () { + $result = $this->schemaManager->getCollections(true); - public function testGetCollectionsWithoutSystem() - { - $result = $this->schemaManager->getCollections(true); + $this->assertEmpty($result); +}); - $this->assertEmpty($result); - } +test('get collection', function () { + $collections = $this->schemaManager->getCollections(); - public function testGetCollection() - { - $collections = $this->schemaManager->getCollections(); + $result = $this->schemaManager->getCollection($collections[0]->name); - $result = $this->schemaManager->getCollection($collections[0]->name); + $this->assertIsObject($result); + $this->assertObjectHasProperty('name', $result); + $this->assertObjectHasProperty('isSystem', $result); +}); - $this->assertIsObject($result); - $this->assertObjectHasProperty('name', $result); - $this->assertObjectHasProperty('isSystem', $result); - } +test('has collection', function () { + $result = $this->schemaManager->hasCollection('_graphs'); + $this->assertTrue($result); - public function testHasCollection() - { - $result = $this->schemaManager->hasCollection('_graphs'); - $this->assertTrue($result); + $result = $this->schemaManager->hasCollection('someNoneExistingCollection'); + $this->assertFalse($result); +}); - $result = $this->schemaManager->hasCollection('someNoneExistingCollection'); - $this->assertFalse($result); - } +test('get collection properties', function () { + $collections = $this->schemaManager->getCollections(); - public function testGetCollectionProperties() - { - $collections = $this->schemaManager->getCollections(); + $result = $this->schemaManager->getCollectionProperties($collections[0]->name); - $result = $this->schemaManager->getCollectionProperties($collections[0]->name); + $this->assertIsObject($result); + $this->assertObjectHasProperty('name', $result); + $this->assertObjectHasProperty('isSystem', $result); + $this->assertObjectHasProperty('statusString', $result); + $this->assertObjectHasProperty('keyOptions', $result); +}); - $this->assertIsObject($result); - $this->assertObjectHasProperty('name', $result); - $this->assertObjectHasProperty('isSystem', $result); - $this->assertObjectHasProperty('statusString', $result); - $this->assertObjectHasProperty('keyOptions', $result); - } +test('get collection with document count', function () { + $collections = $this->schemaManager->getCollections(); - public function testGetCollectionWithDocumentCount() - { - $collections = $this->schemaManager->getCollections(); + $result = $this->schemaManager->getCollectionWithDocumentCount($collections[0]->name); - $result = $this->schemaManager->getCollectionWithDocumentCount($collections[0]->name); + $this->assertObjectHasProperty('name', $result); + $this->assertObjectHasProperty('isSystem', $result); + $this->assertObjectHasProperty('statusString', $result); + $this->assertObjectHasProperty('keyOptions', $result); + $this->assertObjectHasProperty('count', $result); + $this->assertIsNumeric($result->count); +}); - $this->assertObjectHasProperty('name', $result); - $this->assertObjectHasProperty('isSystem', $result); - $this->assertObjectHasProperty('statusString', $result); - $this->assertObjectHasProperty('keyOptions', $result); - $this->assertObjectHasProperty('count', $result); - $this->assertIsNumeric($result->count); - } - - public function testGetCollectionDocumentCount() - { - $collections = $this->schemaManager->getCollections(); - - $result = $this->schemaManager->getCollectionDocumentCount($collections[0]->name); +test('get collection document count', function () { + $collections = $this->schemaManager->getCollections(); - $this->assertIsNumeric($result); - } - - public function testGetCollectionStatistics() - { - $collections = $this->schemaManager->getCollections(); + $result = $this->schemaManager->getCollectionDocumentCount($collections[0]->name); - $result = $this->schemaManager->getCollectionStatistics($collections[0]->name); + $this->assertIsNumeric($result); +}); - $this->assertObjectHasProperty('figures', $result); - } +test('get collection statistics', function () { + $collections = $this->schemaManager->getCollections(); - public function testGetCollectionStatisticsWithDetails() - { - $collections = $this->schemaManager->getCollections(); + $result = $this->schemaManager->getCollectionStatistics($collections[0]->name); - $result = $this->schemaManager->getCollectionStatistics($collections[0]->name, true); + $this->assertObjectHasProperty('figures', $result); +}); - $this->assertObjectHasProperty('figures', $result); - } +test('get collection statistics with details', function () { + $collections = $this->schemaManager->getCollections(); - public function testUpdateCollection() - { - $collection = 'users'; - $config = []; + $result = $this->schemaManager->getCollectionStatistics($collections[0]->name, true); - if (!$this->schemaManager->hasCollection($collection)) { - $this->schemaManager->createCollection($collection, $config); - } + $this->assertObjectHasProperty('figures', $result); +}); - $newConfig = ['waitForSync' => true]; - $result = $this->schemaManager->updateCollection($collection, $newConfig); - $this->assertTrue($result->waitForSync); +test('update collection', function () { + $collection = 'users'; + $config = []; - $this->schemaManager->deleteCollection($collection); + if (!$this->schemaManager->hasCollection($collection)) { + $this->schemaManager->createCollection($collection, $config); } - public function testRenameCollection() - { - $collection = 'users'; - $newName = 'characters'; - $config = []; + $newConfig = ['waitForSync' => true]; + $result = $this->schemaManager->updateCollection($collection, $newConfig); + $this->assertTrue($result->waitForSync); - if (!$this->schemaManager->hasCollection($collection)) { - $this->schemaManager->createCollection($collection, $config); - } - if ($this->schemaManager->hasCollection($newName)) { - $this->schemaManager->deleteCollection($newName); - } + $this->schemaManager->deleteCollection($collection); +}); - $result = $this->schemaManager->renameCollection($collection, $newName); - $this->assertSame($newName, $result->name); +test('rename collection', function () { + $collection = 'users'; + $newName = 'characters'; + $config = []; + if (!$this->schemaManager->hasCollection($collection)) { + $this->schemaManager->createCollection($collection, $config); + } + if ($this->schemaManager->hasCollection($newName)) { $this->schemaManager->deleteCollection($newName); } - public function testTruncateCollection() - { - $collection = 'users'; - if (!$this->schemaManager->hasCollection($collection)) { - $this->schemaManager->createCollection($collection); - } - $this->assertSame(0, $this->schemaManager->getCollectionWithDocumentCount($collection)->count); - $query = 'FOR i IN 1..10 - INSERT { - _key: CONCAT("test", i), - name: "test", - foobar: true - } INTO ' . $collection . ' OPTIONS { ignoreErrors: true }'; - $statement = $this->arangoClient->prepare($query); - $statement->execute(); - - $this->assertSame(0, count($statement->fetchAll())); - - $this->schemaManager->truncateCollection($collection); - - $this->assertSame(0, $this->schemaManager->getCollectionWithDocumentCount($collection)->count); - $this->schemaManager->deleteCollection($collection); + $result = $this->schemaManager->renameCollection($collection, $newName); + $this->assertSame($newName, $result->name); + + $this->schemaManager->deleteCollection($newName); +}); + +test('truncate collection', function () { + $collection = 'users'; + if (!$this->schemaManager->hasCollection($collection)) { + $this->schemaManager->createCollection($collection); } + $this->assertSame(0, $this->schemaManager->getCollectionWithDocumentCount($collection)->count); + $query = 'FOR i IN 1..10 + INSERT { + _key: CONCAT("test", i), + name: "test", + foobar: true + } INTO ' . $collection . ' OPTIONS { ignoreErrors: true }'; + $statement = $this->arangoClient->prepare($query); + $statement->execute(); - public function testCreateAndDeleteCollection() - { - $collection = 'users'; - $options = []; + $this->assertSame(0, count($statement->fetchAll())); - if (!$this->schemaManager->hasCollection($collection)) { - $result = $this->schemaManager->createCollection($collection, $options); - $this->assertEquals($collection, $result->name); - } + $this->schemaManager->truncateCollection($collection); - $result = $this->schemaManager->deleteCollection($collection); - $this->assertTrue($result); - $this->assertFalse($this->schemaManager->hasCollection($collection)); - } + $this->assertSame(0, $this->schemaManager->getCollectionWithDocumentCount($collection)->count); + $this->schemaManager->deleteCollection($collection); +}); - public function testCreateCollectionWithOptions() - { - $collection = 'users'; - $options = ['waitForSync' => true]; +test('create and delete collection', function () { + $collection = 'users'; + $options = []; - if (!$this->schemaManager->hasCollection($collection)) { - $result = $this->schemaManager->createCollection($collection, $options, 1, 1); - } + if (!$this->schemaManager->hasCollection($collection)) { + $result = $this->schemaManager->createCollection($collection, $options); + $this->assertEquals($collection, $result->name); + } - $collectionProperties = $this->schemaManager->getCollectionProperties('users'); - $this->assertTrue($collectionProperties->waitForSync); + $result = $this->schemaManager->deleteCollection($collection); + $this->assertTrue($result); + $this->assertFalse($this->schemaManager->hasCollection($collection)); +}); - // $waitForSyncReplication & $enforceReplicationFactor are not listed in the properties, so the lack of - // of an exception somewhat tests these options... +test('create collection with options', function () { + $collection = 'users'; + $options = ['waitForSync' => true]; - $result = $this->schemaManager->deleteCollection($collection); - $this->assertTrue($result); - $this->assertFalse($this->schemaManager->hasCollection($collection)); + if (!$this->schemaManager->hasCollection($collection)) { + $result = $this->schemaManager->createCollection($collection, $options, 1, 1); } - public function testCreateEdgeCollection() - { - $collection = 'relationships'; + $collectionProperties = $this->schemaManager->getCollectionProperties('users'); + $this->assertTrue($collectionProperties->waitForSync); - if ($this->schemaManager->hasCollection($collection)) { - $this->schemaManager->deleteCollection($collection); - } + // $waitForSyncReplication & $enforceReplicationFactor are not listed in the properties, so the lack of + // of an exception somewhat tests these options... - $result = $this->schemaManager->createEdgeCollection($collection); + $result = $this->schemaManager->deleteCollection($collection); + $this->assertTrue($result); + $this->assertFalse($this->schemaManager->hasCollection($collection)); +}); - $this->assertEquals($collection, $result->name); - $this->assertSame(3, $result->type); +test('create edge collection', function () { + $collection = 'relationships'; + if ($this->schemaManager->hasCollection($collection)) { $this->schemaManager->deleteCollection($collection); } -} + + $result = $this->schemaManager->createEdgeCollection($collection); + + $this->assertEquals($collection, $result->name); + $this->assertSame(3, $result->type); + + $this->schemaManager->deleteCollection($collection); +}); diff --git a/tests/SchemaManagerDatabasesTest.php b/tests/SchemaManagerDatabasesTest.php index 2184c24..1cae8b4 100644 --- a/tests/SchemaManagerDatabasesTest.php +++ b/tests/SchemaManagerDatabasesTest.php @@ -1,59 +1,50 @@ arangoClient->setDatabase('_system'); - $result = $this->schemaManager->getCurrentDatabase(); +test('get database', function () { + $this->arangoClient->setDatabase('_system'); + $result = $this->schemaManager->getCurrentDatabase(); - $this->assertSame('1', $result->id); - $this->assertSame('_system', $result->name); - $this->assertSame(true, $result->isSystem); - $this->assertSame('none', $result->path); - } + $this->assertSame('1', $result->id); + $this->assertSame('_system', $result->name); + $this->assertSame(true, $result->isSystem); + $this->assertSame('none', $result->path); +}); - public function testGetDatabases() - { - $result = $this->schemaManager->getDatabases(); +test('get databases', function () { + $result = $this->schemaManager->getDatabases(); - $this->assertLessThanOrEqual(count($result), 2); - foreach ($result as $database) { - $this->assertIsString($database); - } + $this->assertLessThanOrEqual(count($result), 2); + foreach ($result as $database) { + $this->assertIsString($database); } +}); - public function testCreateAndDeleteDatabase() - { - $database = 'arangodb_php_client_database__test'; - $existingDatabases = $this->schemaManager->getDatabases(); +test('create and delete database', function () { + $database = 'arangodb_php_client_database__test'; + $existingDatabases = $this->schemaManager->getDatabases(); - if (!in_array($database, $existingDatabases)) { - $result = $this->schemaManager->createDatabase($database); - $this->assertTrue($result); - } - - $result = $this->schemaManager->deleteDatabase($database); + if (!in_array($database, $existingDatabases)) { + $result = $this->schemaManager->createDatabase($database); $this->assertTrue($result); - $existingDatabases = $this->schemaManager->getDatabases(); - $this->assertNotContains($database, $existingDatabases); } - public function testHasDatabase() - { - $check = $this->schemaManager->hasDatabase('someNoneExistingDatabase'); - $this->assertFalse($check); + $result = $this->schemaManager->deleteDatabase($database); + $this->assertTrue($result); + $existingDatabases = $this->schemaManager->getDatabases(); + $this->assertNotContains($database, $existingDatabases); +}); - $check = $this->schemaManager->hasDatabase($this->testDatabaseName); - $this->assertTrue($check); - } -} +test('has database', function () { + $check = $this->schemaManager->hasDatabase('someNoneExistingDatabase'); + $this->assertFalse($check); + + $check = $this->schemaManager->hasDatabase($this->testDatabaseName); + $this->assertTrue($check); +}); diff --git a/tests/SchemaManagerGraphsTest.php b/tests/SchemaManagerGraphsTest.php index 21b569c..78fbd26 100644 --- a/tests/SchemaManagerGraphsTest.php +++ b/tests/SchemaManagerGraphsTest.php @@ -1,25 +1,99 @@ schemaManager->createGraph('locations', [], true); + $this->assertSame('_graphs/locations', $result->_id); -class SchemaManagerGraphsTest extends TestCase -{ - public function testCreateAndDeleteGraph() - { - $result = $this->schemaManager->createGraph('locations', [], true); - $this->assertSame('_graphs/locations', $result->_id); + $result = $this->schemaManager->deleteGraph('locations'); + $this->assertTrue($result); +}); - $result = $this->schemaManager->deleteGraph('locations'); - $this->assertTrue($result); +test('create graph with edges', function () { + if (!$this->schemaManager->hasCollection('characters')) { + $this->schemaManager->createCollection('characters'); } + $result = $this->schemaManager->createGraph( + 'relations', + [ + 'edgeDefinitions' => [ + [ + 'collection' => 'children', + 'from' => ['characters'], + 'to' => ['characters'], + ], + ], + 'orphanCollections' => [ + 'orphanVertices', + ], + ], + true, + ); + $this->assertEquals(1, is_countable($result->edgeDefinitions) ? count($result->edgeDefinitions) : 0); + $this->assertEquals($result->_id, '_graphs/relations'); + + $this->schemaManager->deleteGraph('relations'); + $this->schemaManager->deleteCollection('children'); + $this->schemaManager->deleteCollection('characters'); + $this->schemaManager->deleteCollection('orphanVertices'); +}); + +test('get graphs no results', function () { + $result = $this->schemaManager->getGraphs(); + + $this->assertLessThanOrEqual(0, count($result)); +}); + +test('get graphs with results', function () { + if (!$this->schemaManager->hasGraph('characters')) { + $this->schemaManager->createGraph('characters'); + } + if (!$this->schemaManager->hasGraph('locations')) { + $this->schemaManager->createGraph('locations'); + } + + $result = $this->schemaManager->getGraphs(); + + $this->assertEquals(2, count($result)); + $this->assertEquals('characters', $result[0]->_key); + $this->assertEquals('locations', $result[1]->_key); + + $this->schemaManager->deleteGraph('characters'); + $this->schemaManager->deleteGraph('locations'); +}); + +test('has graph', function () { + if (!$this->schemaManager->hasGraph('locations')) { + $this->schemaManager->createGraph('locations'); + } + $result = $this->schemaManager->hasGraph('locations'); + $this->assertTrue($result); + + $this->schemaManager->deleteGraph('locations'); + $result = $this->schemaManager->hasGraph('locations'); + $this->assertFalse($result); +}); + +test('get graph', function () { + if (!$this->schemaManager->hasGraph('locations')) { + $this->schemaManager->createGraph('locations'); + } + + $result = $this->schemaManager->getGraph('locations'); + + $this->assertEquals('locations', $result->_key); - public function testCreateGraphWithEdges() - { - if (!$this->schemaManager->hasCollection('characters')) { - $this->schemaManager->createCollection('characters'); - } + $this->schemaManager->deleteGraph('locations'); +}); + +test('get graph vertices', function () { + if (!$this->schemaManager->hasCollection('characters')) { + $this->schemaManager->createCollection('characters'); + } + if (!$this->schemaManager->hasGraph('relations')) { $result = $this->schemaManager->createGraph( 'relations', [ @@ -36,314 +110,224 @@ public function testCreateGraphWithEdges() ], true, ); - $this->assertEquals(1, is_countable($result->edgeDefinitions) ? count($result->edgeDefinitions) : 0); - $this->assertEquals($result->_id, '_graphs/relations'); - - $this->schemaManager->deleteGraph('relations'); - $this->schemaManager->deleteCollection('children'); - $this->schemaManager->deleteCollection('characters'); - $this->schemaManager->deleteCollection('orphanVertices'); } - public function testGetGraphsNoResults() - { - $result = $this->schemaManager->getGraphs(); - - $this->assertLessThanOrEqual(0, count($result)); - } + $results = $this->schemaManager->getGraphVertices('relations'); - public function testGetGraphsWithResults() - { - if (!$this->schemaManager->hasGraph('characters')) { - $this->schemaManager->createGraph('characters'); - } - if (!$this->schemaManager->hasGraph('locations')) { - $this->schemaManager->createGraph('locations'); - } + $this->assertEquals(2, count($results)); + $this->assertEquals($results[0], 'characters'); + $this->assertEquals($results[1], 'orphanVertices'); - $result = $this->schemaManager->getGraphs(); + $this->schemaManager->deleteGraph('relations'); + $this->schemaManager->deleteCollection('children'); + $this->schemaManager->deleteCollection('characters'); + $this->schemaManager->deleteCollection('orphanVertices'); +}); - $this->assertEquals(2, count($result)); - $this->assertEquals('characters', $result[0]->_key); - $this->assertEquals('locations', $result[1]->_key); - - $this->schemaManager->deleteGraph('characters'); - $this->schemaManager->deleteGraph('locations'); - } - - public function testHasGraph() - { - if (!$this->schemaManager->hasGraph('locations')) { - $this->schemaManager->createGraph('locations'); - } - $result = $this->schemaManager->hasGraph('locations'); - $this->assertTrue($result); - - $this->schemaManager->deleteGraph('locations'); - $result = $this->schemaManager->hasGraph('locations'); - $this->assertFalse($result); - } - - public function testGetGraph() - { - if (!$this->schemaManager->hasGraph('locations')) { - $this->schemaManager->createGraph('locations'); - } - - $result = $this->schemaManager->getGraph('locations'); - - $this->assertEquals('locations', $result->_key); - - $this->schemaManager->deleteGraph('locations'); - } - - public function testGetGraphVertices() - { - if (!$this->schemaManager->hasCollection('characters')) { - $this->schemaManager->createCollection('characters'); - } - if (!$this->schemaManager->hasGraph('relations')) { - $result = $this->schemaManager->createGraph( - 'relations', - [ - 'edgeDefinitions' => [ - [ - 'collection' => 'children', - 'from' => ['characters'], - 'to' => ['characters'], - ], - ], - 'orphanCollections' => [ - 'orphanVertices', +test('add graph vertex', function () { + if (!$this->schemaManager->hasGraph('relations')) { + $this->schemaManager->createGraph( + 'relations', + [ + 'edgeDefinitions' => [ + [ + 'collection' => 'children', + 'from' => ['characters'], + 'to' => ['characters'], ], ], - true, - ); - } + 'orphanCollections' => [ + 'orphanVertices', + ], + ], + false, + ); + } + $newVertex = 'houses'; - $results = $this->schemaManager->getGraphVertices('relations'); + $result = $this->schemaManager->addGraphVertex('relations', $newVertex); - $this->assertEquals(2, count($results)); - $this->assertEquals($results[0], 'characters'); - $this->assertEquals($results[1], 'orphanVertices'); + $this->assertContains('orphanVertices', $result->orphanCollections); + $this->assertContains($newVertex, $result->orphanCollections); - $this->schemaManager->deleteGraph('relations'); - $this->schemaManager->deleteCollection('children'); - $this->schemaManager->deleteCollection('characters'); - $this->schemaManager->deleteCollection('orphanVertices'); - } + $this->schemaManager->deleteGraph('relations'); + $this->schemaManager->deleteCollection('children'); + $this->schemaManager->deleteCollection('characters'); + $this->schemaManager->deleteCollection('orphanVertices'); + $this->schemaManager->deleteCollection($newVertex); +}); - public function testAddGraphVertex() - { - if (!$this->schemaManager->hasGraph('relations')) { - $this->schemaManager->createGraph( - 'relations', - [ - 'edgeDefinitions' => [ - [ - 'collection' => 'children', - 'from' => ['characters'], - 'to' => ['characters'], - ], - ], - 'orphanCollections' => [ - 'orphanVertices', +test('remove graph vertex', function () { + if (!$this->schemaManager->hasGraph('relations')) { + $this->schemaManager->createGraph( + 'relations', + [ + 'edgeDefinitions' => [ + [ + 'collection' => 'children', + 'from' => ['characters'], + 'to' => ['characters'], ], ], - false, - ); - } - $newVertex = 'houses'; - - $result = $this->schemaManager->addGraphVertex('relations', $newVertex); - - $this->assertContains('orphanVertices', $result->orphanCollections); - $this->assertContains($newVertex, $result->orphanCollections); - - $this->schemaManager->deleteGraph('relations'); - $this->schemaManager->deleteCollection('children'); - $this->schemaManager->deleteCollection('characters'); - $this->schemaManager->deleteCollection('orphanVertices'); - $this->schemaManager->deleteCollection($newVertex); + 'orphanCollections' => [ + 'orphanVertices', + ], + ], + false, + ); } - public function testRemoveGraphVertex() - { - if (!$this->schemaManager->hasGraph('relations')) { - $this->schemaManager->createGraph( - 'relations', - [ - 'edgeDefinitions' => [ - [ - 'collection' => 'children', - 'from' => ['characters'], - 'to' => ['characters'], - ], - ], - 'orphanCollections' => [ - 'orphanVertices', - ], - ], - false, - ); - } + $result = $this->schemaManager->removeGraphVertex('relations', 'orphanVertices', true); - $result = $this->schemaManager->removeGraphVertex('relations', 'orphanVertices', true); + $this->assertNotContains('orphanVertices', $result->orphanCollections); - $this->assertNotContains('orphanVertices', $result->orphanCollections); + $checkDropped = $this->schemaManager->hasCollection('orphanVertices'); + $this->assertFalse($checkDropped); - $checkDropped = $this->schemaManager->hasCollection('orphanVertices'); - $this->assertFalse($checkDropped); + $this->schemaManager->deleteGraph('relations'); + $this->schemaManager->deleteCollection('children'); + $this->schemaManager->deleteCollection('characters'); +}); - $this->schemaManager->deleteGraph('relations'); - $this->schemaManager->deleteCollection('children'); - $this->schemaManager->deleteCollection('characters'); +test('get graph edges', function () { + if (!$this->schemaManager->hasCollection('characters')) { + $this->schemaManager->createCollection('characters'); } - - public function testGetGraphEdges() - { - if (!$this->schemaManager->hasCollection('characters')) { - $this->schemaManager->createCollection('characters'); - } - if (!$this->schemaManager->hasGraph('relations')) { - $result = $this->schemaManager->createGraph( - 'relations', - [ - 'edgeDefinitions' => [ - [ - 'collection' => 'children', - 'from' => ['characters'], - 'to' => ['characters'], - ], - ], - 'orphanCollections' => [ - 'orphanVertices', + if (!$this->schemaManager->hasGraph('relations')) { + $result = $this->schemaManager->createGraph( + 'relations', + [ + 'edgeDefinitions' => [ + [ + 'collection' => 'children', + 'from' => ['characters'], + 'to' => ['characters'], ], ], - true, - ); - } + 'orphanCollections' => [ + 'orphanVertices', + ], + ], + true, + ); + } - $results = $this->schemaManager->getGraphEdges('relations'); + $results = $this->schemaManager->getGraphEdges('relations'); - $this->assertEquals(1, count($results)); - $this->assertEquals($results[0], 'children'); + $this->assertEquals(1, count($results)); + $this->assertEquals($results[0], 'children'); - $this->schemaManager->deleteGraph('relations'); - $this->schemaManager->deleteCollection('children'); - $this->schemaManager->deleteCollection('characters'); - $this->schemaManager->deleteCollection('orphanVertices'); - } + $this->schemaManager->deleteGraph('relations'); + $this->schemaManager->deleteCollection('children'); + $this->schemaManager->deleteCollection('characters'); + $this->schemaManager->deleteCollection('orphanVertices'); +}); - public function testAddGraphEdge() - { - if (!$this->schemaManager->hasGraph('relations')) { - $this->schemaManager->createGraph( - 'relations', - [ - 'edgeDefinitions' => [ - [ - 'collection' => 'children', - 'from' => ['characters'], - 'to' => ['characters'], - ], +test('add graph edge', function () { + if (!$this->schemaManager->hasGraph('relations')) { + $this->schemaManager->createGraph( + 'relations', + [ + 'edgeDefinitions' => [ + [ + 'collection' => 'children', + 'from' => ['characters'], + 'to' => ['characters'], ], ], - false, - ); - } - $newEdge = [ - 'collection' => 'vassals', - 'from' => ['characters'], - 'to' => ['houses'], - ]; - - $result = $this->schemaManager->addGraphEdge('relations', $newEdge); - - $this->assertEquals($newEdge['collection'], $result->edgeDefinitions[1]->collection); - - $this->schemaManager->deleteGraph('relations'); - $this->schemaManager->deleteCollection('children'); - $this->schemaManager->deleteCollection('characters'); - $this->schemaManager->deleteCollection('vassals'); - $this->schemaManager->deleteCollection('houses'); + ], + false, + ); } - - public function testReplaceGraphEdge() - { - if (!$this->schemaManager->hasGraph('relations')) { - $this->schemaManager->createGraph( - 'relations', - [ - 'edgeDefinitions' => [ - [ - 'collection' => 'children', - 'from' => ['characters'], - 'to' => ['characters'], - ], + $newEdge = [ + 'collection' => 'vassals', + 'from' => ['characters'], + 'to' => ['houses'], + ]; + + $result = $this->schemaManager->addGraphEdge('relations', $newEdge); + + $this->assertEquals($newEdge['collection'], $result->edgeDefinitions[1]->collection); + + $this->schemaManager->deleteGraph('relations'); + $this->schemaManager->deleteCollection('children'); + $this->schemaManager->deleteCollection('characters'); + $this->schemaManager->deleteCollection('vassals'); + $this->schemaManager->deleteCollection('houses'); +}); + +test('replace graph edge', function () { + if (!$this->schemaManager->hasGraph('relations')) { + $this->schemaManager->createGraph( + 'relations', + [ + 'edgeDefinitions' => [ + [ + 'collection' => 'children', + 'from' => ['characters'], + 'to' => ['characters'], ], ], - ); - } - - $newEdge = [ - 'collection' => 'children', - 'from' => ['houses'], - 'to' => ['houses'], - ]; - - $result = $this->schemaManager->replaceGraphEdge( - 'relations', - 'children', - $newEdge, - false, - true, + ], ); - - $this->assertEquals($newEdge['collection'], $result->edgeDefinitions[0]->collection); - - $this->schemaManager->deleteGraph('relations'); - $this->schemaManager->deleteCollection('children'); - $this->schemaManager->deleteCollection('houses'); - $this->schemaManager->deleteCollection('characters'); } - public function testRemoveGraphEdge() - { - if (!$this->schemaManager->hasGraph('relations')) { - $this->schemaManager->createGraph( - 'relations', - [ - 'edgeDefinitions' => [ - [ - 'collection' => 'children', - 'from' => ['characters'], - 'to' => ['characters'], - ], - [ - 'collection' => 'vassals', - 'from' => ['houses'], - 'to' => ['houses'], - ], + $newEdge = [ + 'collection' => 'children', + 'from' => ['houses'], + 'to' => ['houses'], + ]; + + $result = $this->schemaManager->replaceGraphEdge( + 'relations', + 'children', + $newEdge, + false, + true, + ); + + $this->assertEquals($newEdge['collection'], $result->edgeDefinitions[0]->collection); + + $this->schemaManager->deleteGraph('relations'); + $this->schemaManager->deleteCollection('children'); + $this->schemaManager->deleteCollection('houses'); + $this->schemaManager->deleteCollection('characters'); +}); + +test('remove graph edge', function () { + if (!$this->schemaManager->hasGraph('relations')) { + $this->schemaManager->createGraph( + 'relations', + [ + 'edgeDefinitions' => [ + [ + 'collection' => 'children', + 'from' => ['characters'], + 'to' => ['characters'], + ], + [ + 'collection' => 'vassals', + 'from' => ['houses'], + 'to' => ['houses'], ], ], - ); - } - - $result = $this->schemaManager->removeGraphEdge( - 'relations', - 'children', - true, - true, + ], ); - - $this->assertEquals(1, is_countable($result->edgeDefinitions) ? count($result->edgeDefinitions) : 0); - $this->assertEquals('vassals', $result->edgeDefinitions[0]->collection); - - $this->schemaManager->deleteGraph('relations'); - $this->schemaManager->deleteCollection('children'); - $this->schemaManager->deleteCollection('houses'); - $this->schemaManager->deleteCollection('characters'); - $this->schemaManager->deleteCollection('vassals'); } -} + + $result = $this->schemaManager->removeGraphEdge( + 'relations', + 'children', + true, + true, + ); + + $this->assertEquals(1, is_countable($result->edgeDefinitions) ? count($result->edgeDefinitions) : 0); + $this->assertEquals('vassals', $result->edgeDefinitions[0]->collection); + + $this->schemaManager->deleteGraph('relations'); + $this->schemaManager->deleteCollection('children'); + $this->schemaManager->deleteCollection('houses'); + $this->schemaManager->deleteCollection('characters'); + $this->schemaManager->deleteCollection('vassals'); +}); diff --git a/tests/SchemaManagerIndexesTest.php b/tests/SchemaManagerIndexesTest.php index 6bb94c4..6990c54 100644 --- a/tests/SchemaManagerIndexesTest.php +++ b/tests/SchemaManagerIndexesTest.php @@ -1,95 +1,79 @@ schemaManager->hasCollection($this->collection)) { - $this->schemaManager->createCollection($this->collection); - } - } +uses(Tests\TestCase::class); - protected function tearDown(): void - { - parent::tearDown(); - - if ($this->schemaManager->hasCollection($this->collection)) { - $this->schemaManager->deleteCollection($this->collection); - } - } - - public function testGetIndexes() - { - $indexes = $this->schemaManager->getIndexes($this->collection); - - $this->assertIsObject($indexes[0]); - $this->assertObjectHasProperty('name', $indexes[0]); - $this->assertSame('primary', $indexes[0]->name); - } - - public function testGetIndex() - { - $indexes = $this->schemaManager->getIndexes($this->collection); - $indexId = $indexes[0]->id; - - $index = $this->schemaManager->getIndex($indexId); - - $this->assertObjectHasProperty('name', $index); - $this->assertObjectHasProperty('id', $index); - $this->assertObjectHasProperty('fields', $index); - } - - public function testGetIndexByName() - { - $indexes = $this->schemaManager->getIndexes($this->collection); - $indexName = $indexes[0]->name; - - $index = $this->schemaManager->getIndexByName($this->collection, $indexName); - - $this->assertSame($indexName, $index->name); - } - - public function testCreateIndex() - { - $index = [ - 'name' => 'email_persistent_unique', - 'type' => 'persistent', - 'fields' => ['profile.email'], - 'unique' => true, - 'sparse' => false, - ]; - $created = $this->schemaManager->createIndex($this->collection, $index); - $result = $this->schemaManager->getIndexByName($this->collection, 'email_persistent_unique'); - - $this->assertSame($index['name'], $result->name); - $this->assertSame($index['fields'][0], $result->fields[0]); - $this->assertSame($index['unique'], $result->unique); - $this->assertSame($index['sparse'], $result->sparse); +declare(strict_types=1); +beforeEach(function () { + if (!$this->schemaManager->hasCollection($this->collection)) { + $this->schemaManager->createCollection($this->collection); } +}); - public function testDeleteIndex() - { - $index = [ - 'name' => 'email_persistent_unique', - 'type' => 'persistent', - 'fields' => ['profile.email'], - 'unique' => true, - 'sparse' => false, - ]; - $created = $this->schemaManager->createIndex($this->collection, $index); - $found = $this->schemaManager->getIndexByName($this->collection, 'email_persistent_unique'); - - $deleted = $this->schemaManager->deleteIndex($found->id); - $this->assertEquals($created->id, $deleted->id); - $searchForDeleted = $this->schemaManager->getIndexByName($this->collection, 'email_persistent_unique'); - $this->assertFalse($searchForDeleted); +afterEach(function () { + if ($this->schemaManager->hasCollection($this->collection)) { + $this->schemaManager->deleteCollection($this->collection); } -} +}); + + +test('get indexes', function () { + $indexes = $this->schemaManager->getIndexes($this->collection); + + $this->assertIsObject($indexes[0]); + $this->assertObjectHasProperty('name', $indexes[0]); + $this->assertSame('primary', $indexes[0]->name); +}); + +test('get index', function () { + $indexes = $this->schemaManager->getIndexes($this->collection); + $indexId = $indexes[0]->id; + + $index = $this->schemaManager->getIndex($indexId); + + $this->assertObjectHasProperty('name', $index); + $this->assertObjectHasProperty('id', $index); + $this->assertObjectHasProperty('fields', $index); +}); + +test('get index by name', function () { + $indexes = $this->schemaManager->getIndexes($this->collection); + $indexName = $indexes[0]->name; + + $index = $this->schemaManager->getIndexByName($this->collection, $indexName); + + $this->assertSame($indexName, $index->name); +}); + +test('create index', function () { + $index = [ + 'name' => 'email_persistent_unique', + 'type' => 'persistent', + 'fields' => ['profile.email'], + 'unique' => true, + 'sparse' => false, + ]; + $created = $this->schemaManager->createIndex($this->collection, $index); + $result = $this->schemaManager->getIndexByName($this->collection, 'email_persistent_unique'); + + $this->assertSame($index['name'], $result->name); + $this->assertSame($index['fields'][0], $result->fields[0]); + $this->assertSame($index['unique'], $result->unique); + $this->assertSame($index['sparse'], $result->sparse); +}); + +test('delete index', function () { + $index = [ + 'name' => 'email_persistent_unique', + 'type' => 'persistent', + 'fields' => ['profile.email'], + 'unique' => true, + 'sparse' => false, + ]; + $created = $this->schemaManager->createIndex($this->collection, $index); + $found = $this->schemaManager->getIndexByName($this->collection, 'email_persistent_unique'); + + $deleted = $this->schemaManager->deleteIndex($found->id); + $this->assertEquals($created->id, $deleted->id); + $searchForDeleted = $this->schemaManager->getIndexByName($this->collection, 'email_persistent_unique'); + $this->assertFalse($searchForDeleted); +}); diff --git a/tests/SchemaManagerUsersTest.php b/tests/SchemaManagerUsersTest.php index 311fbd7..8fdb8ed 100644 --- a/tests/SchemaManagerUsersTest.php +++ b/tests/SchemaManagerUsersTest.php @@ -1,157 +1,136 @@ $this->userName, - 'password' => 'yee random pw', - ]; - - if (!$this->schemaManager->hasUser($this->userName)) { - $this->schemaManager->createUser($user); - } - } - - protected function tearDown(): void - { - parent::tearDown(); - - if ($this->schemaManager->hasUser($this->userName)) { - $this->schemaManager->deleteUser($this->userName); - } - } +uses(Tests\TestCase::class); - public function testGetUser() - { - $name = 'root'; - $user = $this->schemaManager->getUser($name); - - $this->assertSame($name, $user->user); +declare(strict_types=1); +beforeEach(function () { + $user = [ + 'user' => $this->userName, + 'password' => 'yee random pw', + ]; + + if (!$this->schemaManager->hasUser($this->userName)) { + $this->schemaManager->createUser($user); } +}); - public function testGetUsers() - { - $users = $this->schemaManager->getUsers(); - $this->assertIsArray($users); - $this->assertObjectHasProperty('user', $users[0]); +afterEach(function () { + if ($this->schemaManager->hasUser($this->userName)) { + $this->schemaManager->deleteUser($this->userName); } - - public function testHasUser() - { - $result = $this->schemaManager->hasUser('root'); - $this->assertTrue($result); - - $result = $this->schemaManager->hasUser('nonExistingUser'); - $this->assertFalse($result); - } - - public function testCreateAndDeleteUser() - { - $user = [ - 'user' => 'admin', - 'passwd' => 'highly secretive password', - 'active' => true, - 'extra' => [ - 'profile' => [ - 'name' => 'Billy Butcher', - ], +}); + + +test('get user', function () { + $name = 'root'; + $user = $this->schemaManager->getUser($name); + + $this->assertSame($name, $user->user); +}); + +test('get users', function () { + $users = $this->schemaManager->getUsers(); + $this->assertIsArray($users); + $this->assertObjectHasProperty('user', $users[0]); +}); + +test('has user', function () { + $result = $this->schemaManager->hasUser('root'); + $this->assertTrue($result); + + $result = $this->schemaManager->hasUser('nonExistingUser'); + $this->assertFalse($result); +}); + +test('create and delete user', function () { + $user = [ + 'user' => 'admin', + 'passwd' => 'highly secretive password', + 'active' => true, + 'extra' => [ + 'profile' => [ + 'name' => 'Billy Butcher', ], - ]; - if ($this->schemaManager->hasUser($user['user'])) { - $this->schemaManager->deleteUser($user['user']); - } - - $created = $this->schemaManager->createUser($user); - $this->assertSame($user['user'], $created->user); - + ], + ]; + if ($this->schemaManager->hasUser($user['user'])) { $this->schemaManager->deleteUser($user['user']); - $checkDeleted = $this->schemaManager->hasUser($user['user']); - $this->assertFalse($checkDeleted); } - public function testUpdateUser() - { - $newUserData = [ - 'user' => $this->userName, - 'active' => false, - ]; - $updated = $this->schemaManager->updateUser($this->userName, $newUserData); + $created = $this->schemaManager->createUser($user); + $this->assertSame($user['user'], $created->user); - $this->assertSame($newUserData['user'], $updated->user); - } + $this->schemaManager->deleteUser($user['user']); + $checkDeleted = $this->schemaManager->hasUser($user['user']); + $this->assertFalse($checkDeleted); +}); - public function testReplaceUser() - { - $newUserData = [ - 'user' => 'newUserName', - 'active' => false, - ]; - $replaced = $this->schemaManager->replaceUser($this->userName, $newUserData); +test('update user', function () { + $newUserData = [ + 'user' => $this->userName, + 'active' => false, + ]; + $updated = $this->schemaManager->updateUser($this->userName, $newUserData); - $this->assertSame($this->userName, $replaced->user); - } + $this->assertSame($newUserData['user'], $updated->user); +}); - public function testGetDatabaseAccessLevel() - { - $accessLevel = $this->schemaManager->getDatabaseAccessLevel('root', '_system'); +test('replace user', function () { + $newUserData = [ + 'user' => 'newUserName', + 'active' => false, + ]; + $replaced = $this->schemaManager->replaceUser($this->userName, $newUserData); - $this->assertSame('rw', $accessLevel); - } + $this->assertSame($this->userName, $replaced->user); +}); - public function testSetDatabaseAccessLevel() - { - $this->setUpAccessTest(); - $grant = 'rw'; +test('get database access level', function () { + $accessLevel = $this->schemaManager->getDatabaseAccessLevel('root', '_system'); - $results = $this->schemaManager->setDatabaseAccessLevel($this->userName, $this->accessDatabase, $grant); - $accessLevel = $this->schemaManager->getDatabaseAccessLevel($this->userName, $this->accessDatabase); + $this->assertSame('rw', $accessLevel); +}); - $this->assertObjectHasProperty($this->accessDatabase, $results); - $this->assertSame($grant, $results->{$this->accessDatabase}); - $this->assertSame($grant, $accessLevel); +test('set database access level', function () { + setUpAccessTest(); + $grant = 'rw'; - $this->tearDownAccessTest(); - } + $results = $this->schemaManager->setDatabaseAccessLevel($this->userName, $this->accessDatabase, $grant); + $accessLevel = $this->schemaManager->getDatabaseAccessLevel($this->userName, $this->accessDatabase); - public function testClearDatabaseAccessLevel() - { - $this->setUpAccessTest(); - $grant = 'rw'; + $this->assertObjectHasProperty($this->accessDatabase, $results); + $this->assertSame($grant, $results->{$this->accessDatabase}); + $this->assertSame($grant, $accessLevel); - $this->schemaManager->setDatabaseAccessLevel($this->userName, $this->accessDatabase, $grant); - $accessLevel = $this->schemaManager->getDatabaseAccessLevel($this->userName, $this->accessDatabase); - $this->assertSame($grant, $accessLevel); + tearDownAccessTest(); +}); - $result = $this->schemaManager->clearDatabaseAccessLevel($this->userName, $this->accessDatabase); - $accessLevel = $this->schemaManager->getDatabaseAccessLevel($this->userName, $this->accessDatabase); +test('clear database access level', function () { + setUpAccessTest(); + $grant = 'rw'; - $this->assertTrue($result); - $this->assertSame('none', $accessLevel); + $this->schemaManager->setDatabaseAccessLevel($this->userName, $this->accessDatabase, $grant); + $accessLevel = $this->schemaManager->getDatabaseAccessLevel($this->userName, $this->accessDatabase); + $this->assertSame($grant, $accessLevel); - $this->tearDownAccessTest(); - } + $result = $this->schemaManager->clearDatabaseAccessLevel($this->userName, $this->accessDatabase); + $accessLevel = $this->schemaManager->getDatabaseAccessLevel($this->userName, $this->accessDatabase); - protected function setUpAccessTest() - { - if (!$this->schemaManager->hasDatabase($this->accessDatabase)) { - $this->schemaManager->createDatabase($this->accessDatabase); - } - } + $this->assertTrue($result); + $this->assertSame('none', $accessLevel); - protected function tearDownAccessTest() - { - $this->schemaManager->deleteDatabase($this->accessDatabase); + tearDownAccessTest(); +}); + +// Helpers +function setUpAccessTest() +{ + if (!test()->schemaManager->hasDatabase(test()->accessDatabase)) { + test()->schemaManager->createDatabase(test()->accessDatabase); } } + +function tearDownAccessTest() +{ + test()->schemaManager->deleteDatabase(test()->accessDatabase); +} diff --git a/tests/SchemaManagerViewsTest.php b/tests/SchemaManagerViewsTest.php index 5519c5a..21314ce 100644 --- a/tests/SchemaManagerViewsTest.php +++ b/tests/SchemaManagerViewsTest.php @@ -1,112 +1,90 @@ 'testViewBasics', - 'type' => 'arangosearch', - ]; - - protected function setUp(): void - { - parent::setUp(); +declare(strict_types=1); +beforeEach(function () { + if (!$this->schemaManager->hasView($this->view['name'])) { + $this->schemaManager->createView($this->view); + } +}); - if (!$this->schemaManager->hasView($this->view['name'])) { - $this->schemaManager->createView($this->view); - } +afterEach(function () { + if ($this->schemaManager->hasView($this->view['name'])) { + $this->schemaManager->deleteView($this->view['name']); } +}); - protected function tearDown(): void - { - parent::tearDown(); - if ($this->schemaManager->hasView($this->view['name'])) { - $this->schemaManager->deleteView($this->view['name']); - } - } +test('get views', function () { + $views = $this->schemaManager->getViews(); - public function testGetViews() - { - $views = $this->schemaManager->getViews(); + $this->assertSame($this->view['name'], $views[0]->name); +}); - $this->assertSame($this->view['name'], $views[0]->name); - } +test('get view', function () { + $view = $this->schemaManager->getView($this->view['name']); - public function testGetView() - { - $view = $this->schemaManager->getView($this->view['name']); + $this->assertSame($this->view['name'], $view->name); + $this->assertObjectHasProperty('type', $view); + $this->assertObjectHasProperty('links', $view); +}); - $this->assertSame($this->view['name'], $view->name); - $this->assertObjectHasProperty('type', $view); - $this->assertObjectHasProperty('links', $view); - } +test('get view properties', function () { + $view = $this->schemaManager->getViewProperties($this->view['name']); - public function testGetViewProperties() - { - $view = $this->schemaManager->getViewProperties($this->view['name']); + $this->assertSame($this->view['name'], $view->name); + $this->assertObjectHasProperty('type', $view); + $this->assertObjectHasProperty('links', $view); +}); - $this->assertSame($this->view['name'], $view->name); - $this->assertObjectHasProperty('type', $view); - $this->assertObjectHasProperty('links', $view); - } +test('has view', function () { + $result = $this->schemaManager->hasView($this->view['name']); + $this->assertTrue($result); - public function testHasView() - { - $result = $this->schemaManager->hasView($this->view['name']); - $this->assertTrue($result); + $result = $this->schemaManager->hasView('someNoneExistingView'); + $this->assertFalse($result); +}); - $result = $this->schemaManager->hasView('someNoneExistingView'); - $this->assertFalse($result); - } +test('rename view', function () { + $newName = 'newName'; + $result = $this->schemaManager->renameView($this->view['name'], $newName); + $this->assertSame($newName, $result->name); - public function testRenameView() - { - $newName = 'newName'; - $result = $this->schemaManager->renameView($this->view['name'], $newName); - $this->assertSame($newName, $result->name); + $this->schemaManager->deleteView($newName); +}); - $this->schemaManager->deleteView($newName); - } +test('update view', function () { + $newViewProps = [ + 'cleanupIntervalStep' => 3, + 'primarySort' => 'email', + ]; + $result = $this->schemaManager->updateView($this->view['name'], $newViewProps); - public function testUpdateView() - { - $newViewProps = [ - 'cleanupIntervalStep' => 3, - 'primarySort' => 'email', - ]; - $result = $this->schemaManager->updateView($this->view['name'], $newViewProps); + $this->assertSame(3, $result->cleanupIntervalStep); +}); - $this->assertSame(3, $result->cleanupIntervalStep); - } +test('replace view', function () { + $newViewProps = [ + 'primarySort' => [[ + 'field' => 'email', + 'direction' => 'desc', + ]], + ]; + $newView = $this->schemaManager->replaceView($this->view['name'], $newViewProps); - public function testReplaceView() - { - $newViewProps = [ - 'primarySort' => [[ - 'field' => 'email', - 'direction' => 'desc', - ]], - ]; - $newView = $this->schemaManager->replaceView($this->view['name'], $newViewProps); - - $this->assertSame($newViewProps['primarySort'][0]['field'], $newView->primarySort[0]->field); - $this->assertFalse($newView->primarySort[0]->asc); - } + $this->assertSame($newViewProps['primarySort'][0]['field'], $newView->primarySort[0]->field); + $this->assertFalse($newView->primarySort[0]->asc); +}); - public function testCreateAndDeleteView() - { - $view = [ - 'name' => 'coolnewview', - ]; - $created = $this->schemaManager->createView($view); - $this->assertObjectHasProperty('name', $created); - $this->assertSame($view['name'], $created->name); - - $deleted = $this->schemaManager->deleteView($view['name']); - $this->assertTrue($deleted); - } -} +test('create and delete view', function () { + $view = [ + 'name' => 'coolnewview', + ]; + $created = $this->schemaManager->createView($view); + $this->assertObjectHasProperty('name', $created); + $this->assertSame($view['name'], $created->name); + + $deleted = $this->schemaManager->deleteView($view['name']); + $this->assertTrue($deleted); +}); diff --git a/tests/StatementTest.php b/tests/StatementTest.php index e58cf0c..fb9d407 100644 --- a/tests/StatementTest.php +++ b/tests/StatementTest.php @@ -2,162 +2,139 @@ declare(strict_types=1); -namespace Tests; - use Traversable; -class StatementTest extends TestCase -{ - protected Traversable $statement; - - protected string $collection = 'users'; - - protected function setUp(): void - { - parent::setUp(); - - if (!$this->schemaManager->hasCollection($this->collection)) { - $this->schemaManager->createCollection($this->collection); - } - $query = 'FOR doc IN ' . $this->collection . ' RETURN doc'; - - $this->statement = $this->arangoClient->prepare($query); +uses(Tests\TestCase::class); +beforeEach(function () { + if (!$this->schemaManager->hasCollection($this->collection)) { + $this->schemaManager->createCollection($this->collection); } + $query = 'FOR doc IN ' . $this->collection . ' RETURN doc'; - protected function tearDown(): void - { - parent::tearDown(); + $this->statement = $this->arangoClient->prepare($query); +}); - if ($this->schemaManager->hasCollection($this->collection)) { - $this->schemaManager->deleteCollection($this->collection); - } +afterEach(function () { + if ($this->schemaManager->hasCollection($this->collection)) { + $this->schemaManager->deleteCollection($this->collection); } +}); - protected function generateTestDocuments(): void - { - $query = 'FOR i IN 1..10 - INSERT { - _key: CONCAT("test", i), - name: "test", - foobar: true - } INTO ' . $this->collection . ' OPTIONS { ignoreErrors: true }'; - $statement = $this->arangoClient->prepare($query); +test('set and get query', function () { + $query = 'FOR doc IN ' . $this->collection . ' LIMIT 1 RETURN doc'; - $statement->execute(); - } + $statement = $this->statement->setQuery($query); - public function testSetAndGetQuery() - { - $query = 'FOR doc IN ' . $this->collection . ' LIMIT 1 RETURN doc'; + $this->assertSame($query, $statement->getQuery()); +}); - $statement = $this->statement->setQuery($query); +test('explain', function () { + $explanation = $this->statement->explain(); - $this->assertSame($query, $statement->getQuery()); - } + $this->assertObjectHasProperty('plan', $explanation); +}); - public function testExplain() - { - $explanation = $this->statement->explain(); +test('parse', function () { + $parsed = $this->statement->parse(); - $this->assertObjectHasProperty('plan', $explanation); - } + $this->assertObjectHasProperty('ast', $parsed); +}); - public function testParse() - { - $parsed = $this->statement->parse(); +test('profile', function () { + $profile = $this->statement->profile(); + $this->assertObjectHasProperty('stats', $profile); + $this->assertObjectHasProperty('profile', $profile); +}); - $this->assertObjectHasProperty('ast', $parsed); - } +test('profile mode two', function () { + $profile = $this->statement->profile(2); - public function testProfile() - { - $profile = $this->statement->profile(); - $this->assertObjectHasProperty('stats', $profile); - $this->assertObjectHasProperty('profile', $profile); - } + $this->assertObjectHasProperty('stats', $profile); + $this->assertObjectHasProperty('profile', $profile); + $this->assertObjectHasProperty('plan', $profile); +}); - public function testProfileModeTwo() - { - $profile = $this->statement->profile(2); +test('get count', function () { + $query = 'FOR doc IN ' . $this->collection . ' RETURN doc'; + $options = ['count' => true]; + $statement = $this->arangoClient->prepare($query, [], $options); + $statement->execute(); - $this->assertObjectHasProperty('stats', $profile); - $this->assertObjectHasProperty('profile', $profile); - $this->assertObjectHasProperty('plan', $profile); - } + $this->assertSame(0, $statement->getCount()); +}); - public function testGetCount() - { - $query = 'FOR doc IN ' . $this->collection . ' RETURN doc'; - $options = ['count' => true]; - $statement = $this->arangoClient->prepare($query, [], $options); - $statement->execute(); +test('get count not set', function () { + $this->statement->execute(); - $this->assertSame(0, $statement->getCount()); - } + $this->assertNull($this->statement->getCount()); +}); - public function testGetCountNotSet() - { - $this->statement->execute(); +test('fetch all', function () { + generateTestDocuments(); - $this->assertNull($this->statement->getCount()); - } + $query = 'FOR doc IN ' . $this->collection . ' RETURN doc'; + $this->statement->setQuery($query); + $executed = $this->statement->execute(); + $this->assertTrue($executed); - public function testFetchAll() - { - $this->generateTestDocuments(); + $results = $this->statement->fetchAll(); + $this->assertEquals(10, is_countable($results) ? count($results) : 0); + $this->assertSame('test1', $results[0]->_key); +}); - $query = 'FOR doc IN ' . $this->collection . ' RETURN doc'; - $this->statement->setQuery($query); - $executed = $this->statement->execute(); - $this->assertTrue($executed); +test('results greater than batch size', function () { + generateTestDocuments(); - $results = $this->statement->fetchAll(); - $this->assertEquals(10, is_countable($results) ? count($results) : 0); - $this->assertSame('test1', $results[0]->_key); - } + // Retrieve data in batches of 2 + $query = 'FOR doc IN ' . $this->collection . ' RETURN doc'; + $options = ['batchSize' => 2]; + $statement = $this->arangoClient->prepare($query, [], $options); + $executed = $statement->execute(); + $this->assertTrue($executed); + $results = $statement->fetchAll(); - public function testResultsGreaterThanBatchSize() - { - $this->generateTestDocuments(); + $this->assertEquals(10, count($results)); + $this->assertSame('test1', $results[0]->_key); +}); - // Retrieve data in batches of 2 - $query = 'FOR doc IN ' . $this->collection . ' RETURN doc'; - $options = ['batchSize' => 2]; - $statement = $this->arangoClient->prepare($query, [], $options); - $executed = $statement->execute(); - $this->assertTrue($executed); - $results = $statement->fetchAll(); +test('statement is iterable', function () { + generateTestDocuments(); + $this->statement->execute(); - $this->assertEquals(10, count($results)); - $this->assertSame('test1', $results[0]->_key); + $count = 0; + foreach ($this->statement as $document) { + $this->assertObjectHasProperty('foobar', $document); + $count++; } + $this->assertEquals(10, $count); +}); - public function testStatementIsIterable() - { - $this->generateTestDocuments(); - $this->statement->execute(); - - $count = 0; - foreach ($this->statement as $document) { - $this->assertObjectHasProperty('foobar', $document); - $count++; - } - $this->assertEquals(10, $count); - } +test('get writes executed', function () { + $query = 'FOR i IN 1..10 + INSERT { + _key: CONCAT("test", i), + name: "test", + foobar: true + } INTO ' . $this->collection . ' OPTIONS { ignoreErrors: true }'; - public function testGetWritesExecuted(): void - { - $query = 'FOR i IN 1..10 - INSERT { - _key: CONCAT("test", i), - name: "test", - foobar: true - } INTO ' . $this->collection . ' OPTIONS { ignoreErrors: true }'; + $statement = $this->arangoClient->prepare($query); + $statement->execute(); - $statement = $this->arangoClient->prepare($query); - $statement->execute(); + $this->assertSame(10, $statement->getWritesExecuted()); +}); - $this->assertSame(10, $statement->getWritesExecuted()); - } +// Helpers +function generateTestDocuments(): void +{ + $query = 'FOR i IN 1..10 + INSERT { + _key: CONCAT("test", i), + name: "test", + foobar: true + } INTO ' . test()->collection . ' OPTIONS { ignoreErrors: true }'; + + $statement = test()->arangoClient->prepare($query); + + $statement->execute(); } diff --git a/tests/SupportsTransactionsTest.php b/tests/SupportsTransactionsTest.php index 8a87889..6afba32 100644 --- a/tests/SupportsTransactionsTest.php +++ b/tests/SupportsTransactionsTest.php @@ -2,120 +2,108 @@ declare(strict_types=1); -namespace Tests; - use ArangoClient\Transactions\TransactionManager; -class SupportsTransactionsTest extends TestCase -{ - protected function setUp(): void - { - parent::setUp(); - } +uses(Tests\TestCase::class); +beforeEach(function () { +}); - public function testTransactions() - { - $transactionManager = $this->arangoClient->transactions(); - $this->assertInstanceOf(TransactionManager::class, $transactionManager); - } - public function testBeginTransaction() - { - $transactionId = $this->arangoClient->beginTransaction(); - $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); - $this->assertSame($transactionId, $runningTransactions[0]->id); +test('transactions', function () { + $transactionManager = $this->arangoClient->transactions(); + $this->assertInstanceOf(TransactionManager::class, $transactionManager); +}); - $this->arangoClient->abort(); - } +test('begin transaction', function () { + $transactionId = $this->arangoClient->beginTransaction(); + $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); + $this->assertSame($transactionId, $runningTransactions[0]->id); - public function testBegin() - { - $transactionId = $this->arangoClient->begin(); - $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); - $this->assertSame($transactionId, $runningTransactions[0]->id); + $this->arangoClient->abort(); +}); - $this->arangoClient->abort(); - } +test('begin', function () { + $transactionId = $this->arangoClient->begin(); + $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); + $this->assertSame($transactionId, $runningTransactions[0]->id); - public function testAbort() - { - $transactionId = $this->arangoClient->beginTransaction(); - $aborted = $this->arangoClient->abort(); - $this->assertTrue($aborted); + $this->arangoClient->abort(); +}); - $transactionsListedInManager = $this->arangoClient->transactions()->getTransactions(); - $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); +test('abort', function () { + $transactionId = $this->arangoClient->beginTransaction(); + $aborted = $this->arangoClient->abort(); + $this->assertTrue($aborted); - $this->assertArrayNotHasKey($transactionId, $transactionsListedInManager); - $this->assertFalse(array_search($transactionId, array_column($runningTransactions, 'id'))); - } + $transactionsListedInManager = $this->arangoClient->transactions()->getTransactions(); + $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); - public function testRollBack() - { - $transactionId = $this->arangoClient->beginTransaction(); - $aborted = $this->arangoClient->rollBack(); - $this->assertTrue($aborted); + $this->assertArrayNotHasKey($transactionId, $transactionsListedInManager); + $this->assertFalse(array_search($transactionId, array_column($runningTransactions, 'id'))); +}); - $transactionsListedInManager = $this->arangoClient->transactions()->getTransactions(); - $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); +test('roll back', function () { + $transactionId = $this->arangoClient->beginTransaction(); + $aborted = $this->arangoClient->rollBack(); + $this->assertTrue($aborted); - $this->assertArrayNotHasKey($transactionId, $transactionsListedInManager); - $this->assertFalse(array_search($transactionId, array_column($runningTransactions, 'id'))); - } + $transactionsListedInManager = $this->arangoClient->transactions()->getTransactions(); + $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); + + $this->assertArrayNotHasKey($transactionId, $transactionsListedInManager); + $this->assertFalse(array_search($transactionId, array_column($runningTransactions, 'id'))); +}); - public function testCommit() - { - if (!$this->arangoClient->schema()->hasCollection('Users')) { - $this->arangoClient->schema()->createCollection('Users'); - } - if (!$this->arangoClient->schema()->hasCollection('Customers')) { - $this->arangoClient->schema()->createCollection('Customers'); - } - - $collections = [ - 'write' => [ - 'Users', - 'Customers', - ], - ]; - - $this->arangoClient->beginTransaction($collections); - - $insertQuery = 'FOR i IN 1..10 - INSERT { - _key: CONCAT("test", i), - name: "test", - foobar: true - } INTO Users OPTIONS { ignoreErrors: true }'; - $insertStatement = $this->arangoClient->prepare($insertQuery); - $insertStatement->execute(); - - $getQuery = 'for user in Users RETURN user'; - $getStatement = $this->arangoClient->prepare($getQuery); - $getStatement->execute(); - - $this->assertEquals(10, count($getStatement->fetchAll())); - - $this->arangoClient->commit(); - - $getQuery = 'for user in Users RETURN user'; - $getStatement = $this->arangoClient->prepare($getQuery); - $getStatement->execute(); - - $this->assertEquals(10, count($getStatement->fetchAll())); - - $this->arangoClient->schema()->deleteCollection('Users'); - $this->arangoClient->schema()->deleteCollection('Customers'); +test('commit', function () { + if (!$this->arangoClient->schema()->hasCollection('Users')) { + $this->arangoClient->schema()->createCollection('Users'); + } + if (!$this->arangoClient->schema()->hasCollection('Customers')) { + $this->arangoClient->schema()->createCollection('Customers'); } - public function testTransactionManagerSetterAndGetter() - { - $oldTransactionManager = $this->arangoClient->getTransactionManager(); - $newTransactionManager = new TransactionManager($this->arangoClient); - $this->arangoClient->setTransactionManager($newTransactionManager); - $retrievedNewTransactionManager = $this->arangoClient->getTransactionManager(); + $collections = [ + 'write' => [ + 'Users', + 'Customers', + ], + ]; - $this->assertNull($oldTransactionManager); - $this->assertEquals(spl_object_id($newTransactionManager), spl_object_id($retrievedNewTransactionManager)); - } -} + $this->arangoClient->beginTransaction($collections); + + $insertQuery = 'FOR i IN 1..10 + INSERT { + _key: CONCAT("test", i), + name: "test", + foobar: true + } INTO Users OPTIONS { ignoreErrors: true }'; + $insertStatement = $this->arangoClient->prepare($insertQuery); + $insertStatement->execute(); + + $getQuery = 'for user in Users RETURN user'; + $getStatement = $this->arangoClient->prepare($getQuery); + $getStatement->execute(); + + $this->assertEquals(10, count($getStatement->fetchAll())); + + $this->arangoClient->commit(); + + $getQuery = 'for user in Users RETURN user'; + $getStatement = $this->arangoClient->prepare($getQuery); + $getStatement->execute(); + + $this->assertEquals(10, count($getStatement->fetchAll())); + + $this->arangoClient->schema()->deleteCollection('Users'); + $this->arangoClient->schema()->deleteCollection('Customers'); +}); + +test('transaction manager setter and getter', function () { + $oldTransactionManager = $this->arangoClient->getTransactionManager(); + $newTransactionManager = new TransactionManager($this->arangoClient); + $this->arangoClient->setTransactionManager($newTransactionManager); + $retrievedNewTransactionManager = $this->arangoClient->getTransactionManager(); + + $this->assertNull($oldTransactionManager); + $this->assertEquals(spl_object_id($newTransactionManager), spl_object_id($retrievedNewTransactionManager)); +}); diff --git a/tests/TransactionManagerTest.php b/tests/TransactionManagerTest.php index bc7c269..1ebe77c 100644 --- a/tests/TransactionManagerTest.php +++ b/tests/TransactionManagerTest.php @@ -2,165 +2,147 @@ declare(strict_types=1); -namespace Tests; - use ArangoClient\Transactions\TransactionManager; -class TransactionManagerTest extends TestCase -{ - protected TransactionManager $transactionManager; +uses(Tests\TestCase::class); +beforeEach(function () { + $this->transactionManager = new TransactionManager($this->arangoClient); +}); - protected function setUp(): void - { - parent::setUp(); - $this->transactionManager = new TransactionManager($this->arangoClient); - } +test('begin', function () { + $transactionId = $this->transactionManager->begin(); + $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); + $this->assertSame($transactionId, $runningTransactions[0]->id); - public function testBegin() - { - $transactionId = $this->transactionManager->begin(); - $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); - $this->assertSame($transactionId, $runningTransactions[0]->id); + $this->transactionManager->abort(); +}); - $this->transactionManager->abort(); - } +test('get transactions', function () { + $transactions = []; + $begunTransactions = $this->transactionManager->getTransactions(); + $this->assertEmpty($begunTransactions); - public function testGetTransactions() - { - $transactions = []; - $begunTransactions = $this->transactionManager->getTransactions(); - $this->assertEmpty($begunTransactions); + $id = $this->transactionManager->begin(); + $transactions[$id] = $id; + $id = $this->transactionManager->begin(); + $transactions[$id] = $id; - $id = $this->transactionManager->begin(); - $transactions[$id] = $id; - $id = $this->transactionManager->begin(); - $transactions[$id] = $id; + $begunTransactions = $this->transactionManager->getTransactions(); - $begunTransactions = $this->transactionManager->getTransactions(); + $this->assertSame($transactions, $begunTransactions); +}); - $this->assertSame($transactions, $begunTransactions); - } +test('get transaction', function () { + $transactions = []; + $transactions[] = $this->transactionManager->begin(); + $transactions[] = $this->transactionManager->begin(); - public function testGetTransaction() - { - $transactions = []; - $transactions[] = $this->transactionManager->begin(); - $transactions[] = $this->transactionManager->begin(); + $lastTransaction = $this->transactionManager->getTransaction(); - $lastTransaction = $this->transactionManager->getTransaction(); + $this->assertSame($transactions[1], $lastTransaction); +}); - $this->assertSame($transactions[1], $lastTransaction); - } +test('get transaction before begin', function () { + $this->expectExceptionCode(404); + $this->transactionManager->getTransaction(); +}); - public function testGetTransactionBeforeBegin() - { - $this->expectExceptionCode(404); - $this->transactionManager->getTransaction(); +test('begin multiple transactions', function () { + $transactions = []; + $transactions[] = $this->transactionManager->begin(); + $transactions[] = $this->transactionManager->begin(); + + $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); + $transactionsListedInManager = $this->transactionManager->getTransactions(); + + foreach ($transactions as $key => $id) { + $this->assertContains($id, $transactionsListedInManager); + $this->assertNotFalse(array_search($id, array_column($runningTransactions, 'id'))); } + $this->assertEquals(count($transactions), count($transactionsListedInManager)); - public function testBeginMultipleTransactions() - { - $transactions = []; - $transactions[] = $this->transactionManager->begin(); - $transactions[] = $this->transactionManager->begin(); + $this->transactionManager->abortRunningTransactions(); +}); - $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); - $transactionsListedInManager = $this->transactionManager->getTransactions(); +test('abort', function () { + $transactionId = $this->transactionManager->begin(); + $aborted = $this->transactionManager->abort(); + $this->assertTrue($aborted); - foreach ($transactions as $key => $id) { - $this->assertContains($id, $transactionsListedInManager); - $this->assertNotFalse(array_search($id, array_column($runningTransactions, 'id'))); - } - $this->assertEquals(count($transactions), count($transactionsListedInManager)); + $transactionsListedInManager = $this->transactionManager->getTransactions(); + $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); - $this->transactionManager->abortRunningTransactions(); - } + $this->assertArrayNotHasKey($transactionId, $transactionsListedInManager); + $this->assertFalse(array_search($transactionId, array_column($runningTransactions, 'id'))); +}); - public function testAbort() - { - $transactionId = $this->transactionManager->begin(); - $aborted = $this->transactionManager->abort(); - $this->assertTrue($aborted); +test('abort before commit', function () { + $this->expectExceptionCode(404); + $this->transactionManager->abort(); +}); - $transactionsListedInManager = $this->transactionManager->getTransactions(); - $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); +test('abort wrong id', function () { + $this->expectExceptionCode(404); + $this->transactionManager->abort('nonExistingTransaction'); +}); - $this->assertArrayNotHasKey($transactionId, $transactionsListedInManager); - $this->assertFalse(array_search($transactionId, array_column($runningTransactions, 'id'))); - } +test('abort running transactions', function () { + $transactions = []; + $transactions[] = $this->transactionManager->begin(); + $transactions[] = $this->transactionManager->begin(); - public function testAbortBeforeCommit() - { - $this->expectExceptionCode(404); - $this->transactionManager->abort(); + $this->transactionManager->abortRunningTransactions(); + + $transactionsListedInManager = $this->transactionManager->getTransactions(); + $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); + + $this->assertEmpty($transactionsListedInManager); + foreach ($transactions as $id) { + $this->assertFalse(array_search($id, array_column($runningTransactions, 'id'))); } +}); - public function testAbortWrongId() - { - $this->expectExceptionCode(404); - $this->transactionManager->abort('nonExistingTransaction'); +test('commit', function () { + if (!$this->arangoClient->schema()->hasCollection('Users')) { + $this->arangoClient->schema()->createCollection('Users'); + } + if (!$this->arangoClient->schema()->hasCollection('Customers')) { + $this->arangoClient->schema()->createCollection('Customers'); } - public function testAbortRunningTransactions() - { - $transactions = []; - $transactions[] = $this->transactionManager->begin(); - $transactions[] = $this->transactionManager->begin(); + $collections = [ + 'write' => [ + 'Users', + 'Customers', + ], + ]; - $this->transactionManager->abortRunningTransactions(); + $this->transactionManager->begin($collections); - $transactionsListedInManager = $this->transactionManager->getTransactions(); - $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); + $insertQuery = 'FOR i IN 1..10 + INSERT { + _key: CONCAT("test", i), + name: "test", + foobar: true + } INTO Users OPTIONS { ignoreErrors: true }'; + $insertStatement = $this->arangoClient->prepare($insertQuery); + $insertStatement->execute(); - $this->assertEmpty($transactionsListedInManager); - foreach ($transactions as $id) { - $this->assertFalse(array_search($id, array_column($runningTransactions, 'id'))); - } - } + $getQuery = 'for user in Users RETURN user'; + $getStatement = $this->arangoClient->prepare($getQuery); + $getStatement->execute(); - public function testCommit() - { - if (!$this->arangoClient->schema()->hasCollection('Users')) { - $this->arangoClient->schema()->createCollection('Users'); - } - if (!$this->arangoClient->schema()->hasCollection('Customers')) { - $this->arangoClient->schema()->createCollection('Customers'); - } - - $collections = [ - 'write' => [ - 'Users', - 'Customers', - ], - ]; - - $this->transactionManager->begin($collections); - - $insertQuery = 'FOR i IN 1..10 - INSERT { - _key: CONCAT("test", i), - name: "test", - foobar: true - } INTO Users OPTIONS { ignoreErrors: true }'; - $insertStatement = $this->arangoClient->prepare($insertQuery); - $insertStatement->execute(); - - $getQuery = 'for user in Users RETURN user'; - $getStatement = $this->arangoClient->prepare($getQuery); - $getStatement->execute(); - - $this->assertEquals(10, count($getStatement->fetchAll())); - - $this->transactionManager->commit(); - - $getQuery = 'for user in Users RETURN user'; - $getStatement = $this->arangoClient->prepare($getQuery); - $getStatement->execute(); - - $this->assertEquals(10, count($getStatement->fetchAll())); - - $this->arangoClient->schema()->deleteCollection('Users'); - $this->arangoClient->schema()->deleteCollection('Customers'); - } -} + $this->assertEquals(10, count($getStatement->fetchAll())); + + $this->transactionManager->commit(); + + $getQuery = 'for user in Users RETURN user'; + $getStatement = $this->arangoClient->prepare($getQuery); + $getStatement->execute(); + + $this->assertEquals(10, count($getStatement->fetchAll())); + + $this->arangoClient->schema()->deleteCollection('Users'); + $this->arangoClient->schema()->deleteCollection('Customers'); +}); From 6b955e49f340ddcc9fa42e841c2e5a1ffd61f803 Mon Sep 17 00:00:00 2001 From: Shift Date: Mon, 7 Oct 2024 22:26:21 +0000 Subject: [PATCH 4/8] Remove non-compound imports --- tests/ArangoClientTest.php | 2 -- tests/StatementTest.php | 1 - 2 files changed, 3 deletions(-) diff --git a/tests/ArangoClientTest.php b/tests/ArangoClientTest.php index bb8da98..1ea388b 100644 --- a/tests/ArangoClientTest.php +++ b/tests/ArangoClientTest.php @@ -11,8 +11,6 @@ use GuzzleHttp\HandlerStack; use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Response; -use Mockery; -use stdClass; uses(Tests\TestCase::class); diff --git a/tests/StatementTest.php b/tests/StatementTest.php index fb9d407..e111a68 100644 --- a/tests/StatementTest.php +++ b/tests/StatementTest.php @@ -2,7 +2,6 @@ declare(strict_types=1); -use Traversable; uses(Tests\TestCase::class); beforeEach(function () { From 4c2a126dc5412f0222d12f7d64528b44b2f9e609 Mon Sep 17 00:00:00 2001 From: Shift Date: Mon, 7 Oct 2024 22:26:21 +0000 Subject: [PATCH 5/8] Adopt expectation API --- tests/AdminManagerTest.php | 14 ++++---- tests/ArangoClientTest.php | 42 +++++++++++----------- tests/MonitorManagerTest.php | 18 +++++----- tests/SchemaManagerAnalyzersTest.php | 18 +++++----- tests/SchemaManagerCollectionsTest.php | 48 +++++++++++++------------- tests/SchemaManagerDatabasesTest.php | 20 +++++------ tests/SchemaManagerGraphsTest.php | 46 ++++++++++++------------ tests/SchemaManagerIndexesTest.php | 18 +++++----- tests/SchemaManagerUsersTest.php | 28 +++++++-------- tests/SchemaManagerViewsTest.php | 22 ++++++------ tests/StatementTest.php | 22 ++++++------ tests/SupportsTransactionsTest.php | 22 ++++++------ tests/TransactionManagerTest.php | 24 ++++++------- 13 files changed, 171 insertions(+), 171 deletions(-) diff --git a/tests/AdminManagerTest.php b/tests/AdminManagerTest.php index 303f4fa..4b30d37 100644 --- a/tests/AdminManagerTest.php +++ b/tests/AdminManagerTest.php @@ -13,20 +13,20 @@ test('get version', function () { $result = $this->adminManager->getVersion(); - $this->assertSame('arango', $result->server); - $this->assertSame('community', $result->license); - $this->assertIsString($result->version); + expect($result->server)->toBe('arango'); + expect($result->license)->toBe('community'); + expect($result->version)->toBeString(); }); test('get version with details', function () { $result = $this->adminManager->getVersion(true); - $this->assertSame('arango', $result->server); - $this->assertSame('community', $result->license); - $this->assertIsString($result->version); + expect($result->server)->toBe('arango'); + expect($result->license)->toBe('community'); + expect($result->version)->toBeString(); }); test('get running transactions', function () { $transactions = $this->adminManager->getRunningTransactions(); - $this->assertEmpty($transactions); + expect($transactions)->toBeEmpty(); }); diff --git a/tests/ArangoClientTest.php b/tests/ArangoClientTest.php index 1ea388b..1cb7df6 100644 --- a/tests/ArangoClientTest.php +++ b/tests/ArangoClientTest.php @@ -30,7 +30,7 @@ ]; $config = $this->arangoClient->getConfig(); - $this->assertSame($defaultConfig, $config); + expect($config)->toBe($defaultConfig); }); test('get config with endpoint without host port', function () { @@ -40,7 +40,7 @@ ]; $returnedConfig = $this->arangoClient->getConfig(); - $this->assertSame($config['endpoint'], $returnedConfig['endpoint']); + expect($returnedConfig['endpoint'])->toBe($config['endpoint']); }); test('client with host port config', function () { @@ -52,7 +52,7 @@ $client = new ArangoClient($config); $retrievedConfig = $client->getConfig(); - $this->assertEquals('http://127.0.0.1:1234', $retrievedConfig['endpoint']); + expect($retrievedConfig['endpoint'])->toEqual('http://127.0.0.1:1234'); }); test('config with alien properties', function () { @@ -77,32 +77,32 @@ $this->arangoClient->setHttpClient($newClient); $retrievedClient = $this->arangoClient->getHttpClient(); - $this->assertInstanceOf(Client::class, $oldClient); - $this->assertEquals($newClient::class, $retrievedClient::class); + expect($oldClient)->toBeInstanceOf(Client::class); + expect($retrievedClient::class)->toEqual($newClient::class); }); test('request', function () { $result = $this->arangoClient->request('get', '/_api/version', []); - $this->assertSame('arango', $result->server); - $this->assertSame('community', $result->license); - $this->assertIsString($result->version); + expect($result->server)->toBe('arango'); + expect($result->license)->toBe('community'); + expect($result->version)->toBeString(); }); test('get user', function () { $user = $this->arangoClient->getUser(); - $this->assertSame('root', $user); + expect($user)->toBe('root'); }); test('set and get database name', function () { $database = $this->arangoClient->getDatabase(); - $this->assertSame($this->testDatabaseName, $database); + expect($database)->toBe($this->testDatabaseName); $newDatabaseName = 'ArangoClientDB'; $this->arangoClient->setDatabase($newDatabaseName); $database = $this->arangoClient->getDatabase(); - $this->assertSame($newDatabaseName, $database); + expect($database)->toBe($newDatabaseName); }); test('database name is used in requests', function () { @@ -126,7 +126,7 @@ $this->arangoClient->request('get', $uri, ['handler' => $handlerStack]); foreach ($container as $transaction) { - $this->assertSame('/_db/' . $database . $uri, $transaction['request']->getUri()->getPath()); + expect($transaction['request']->getUri()->getPath())->toBe('/_db/' . $database . $uri); } $this->arangoClient->schema()->deleteDatabase($database); @@ -134,7 +134,7 @@ test('schema', function () { $result = $this->arangoClient->schema(); - $this->assertInstanceOf(SchemaManager::class, $result); + expect($result)->toBeInstanceOf(SchemaManager::class); $database = $this->arangoClient->schema()->getCurrentDatabase(); @@ -143,7 +143,7 @@ test('admin', function () { $result = $this->arangoClient->admin(); - $this->assertInstanceOf(AdminManager::class, $result); + expect($result)->toBeInstanceOf(AdminManager::class); $version = $this->arangoClient->admin()->getVersion(); @@ -153,7 +153,7 @@ test('prepare', function () { $statement = $this->arangoClient->prepare('FOR doc IN users RETURN doc'); - $this->assertInstanceOf(Statement::class, $statement); + expect($statement)->toBeInstanceOf(Statement::class); }); test('connection protocol version', function () { @@ -165,7 +165,7 @@ $options['version'] = 2; $response = $this->arangoClient->debugRequest('get', $uri, $options); - $this->assertEquals(2, $response->getProtocolVersion()); + expect($response->getProtocolVersion())->toEqual(2); }); test('connection protocol version with default setting', function () { @@ -179,25 +179,25 @@ $options['version'] = 2; $response = $this->arangoClient->debugRequest('get', $uri, $options); - $this->assertEquals(2, $response->getProtocolVersion()); + expect($response->getProtocolVersion())->toEqual(2); }); test('json encode', function () { $results = $this->arangoClient->jsonEncode([]); - $this->assertSame('{}', $results); + expect($results)->toBe('{}'); }); test('json encode empty array', function () { $results = $this->arangoClient->jsonEncode([]); - $this->assertSame('{}', $results); + expect($results)->toBe('{}'); }); test('json encode empty string', function () { $results = $this->arangoClient->jsonEncode(''); - $this->assertSame('""', $results); + expect($results)->toBe('""'); }); test('json encode invalid data', function () { @@ -230,7 +230,7 @@ $statement->execute(); $users = $statement->fetchAll(); - $this->assertEquals($insertResult[0], $users[0]); + expect($users[0])->toEqual($insertResult[0]); $this->schemaManager->deleteCollection($collection); }); diff --git a/tests/MonitorManagerTest.php b/tests/MonitorManagerTest.php index 6d0b893..70c2cba 100644 --- a/tests/MonitorManagerTest.php +++ b/tests/MonitorManagerTest.php @@ -8,9 +8,9 @@ test('get metrics', function () { $result = $this->arangoClient->monitor()->getMetrics(); - $this->assertIsObject($result); - $this->assertEquals("gauge", $result->arangodb_agency_cache_callback_number->type); - $this->assertCount(1, $result->arangodb_agency_cache_callback_number->labels); + expect($result)->toBeObject(); + expect($result->arangodb_agency_cache_callback_number->type)->toEqual("gauge"); + expect($result->arangodb_agency_cache_callback_number->labels)->toHaveCount(1); }); test('summary metric', function () { @@ -26,7 +26,7 @@ $result = $prometheus->parseText($rawMetrics); - $this->assertIsObject($result); + expect($result)->toBeObject(); }); test('historgram parsing', function () { @@ -59,9 +59,9 @@ '; $result = $prometheus->parseText($rawMetrics); - $this->assertCount(20, $result->arangodb_aql_query_time->buckets); - $this->assertEquals(177, $result->arangodb_aql_query_time->count); - $this->assertEquals(0.03518, $result->arangodb_aql_query_time->sum); + expect($result->arangodb_aql_query_time->buckets)->toHaveCount(20); + expect($result->arangodb_aql_query_time->count)->toEqual(177); + expect($result->arangodb_aql_query_time->sum)->toEqual(0.03518); }); test('timestamp parsing', function () { @@ -72,6 +72,6 @@ arangodb_aql_local_query_memory_limit_reached_total{role="SINGLE"} 0 2211753600'; $result = $prometheus->parseText($rawMetrics); - $this->assertIsObject($result); - $this->assertEquals(2211753600, $result->arangodb_aql_local_query_memory_limit_reached_total->timestamp); + expect($result)->toBeObject(); + expect($result->arangodb_aql_local_query_memory_limit_reached_total->timestamp)->toEqual(2211753600); }); diff --git a/tests/SchemaManagerAnalyzersTest.php b/tests/SchemaManagerAnalyzersTest.php index debaa39..1158246 100644 --- a/tests/SchemaManagerAnalyzersTest.php +++ b/tests/SchemaManagerAnalyzersTest.php @@ -25,29 +25,29 @@ $customAnalyzer = end($analyzers); - $this->assertSame('arangodb_php_client__test::' . $this->analyzer['name'], $customAnalyzer->name); + expect($customAnalyzer->name)->toBe('arangodb_php_client__test::' . $this->analyzer['name']); }); test('get analyzer', function () { $analyzer = $this->schemaManager->getAnalyzer($this->analyzer['name']); - $this->assertSame('arangodb_php_client__test::' . $this->analyzer['name'], $analyzer->name); + expect($analyzer->name)->toBe('arangodb_php_client__test::' . $this->analyzer['name']); $this->assertObjectHasProperty('type', $analyzer); }); test('get analyzer with full name', function () { $analyzer = $this->schemaManager->getAnalyzer('arangodb_php_client__test::' . $this->analyzer['name']); - $this->assertSame('arangodb_php_client__test::' . $this->analyzer['name'], $analyzer->name); + expect($analyzer->name)->toBe('arangodb_php_client__test::' . $this->analyzer['name']); $this->assertObjectHasProperty('type', $analyzer); }); test('has analyzer', function () { $result = $this->schemaManager->hasAnalyzer($this->analyzer['name']); - $this->assertTrue($result); + expect($result)->toBeTrue(); $result = $this->schemaManager->hasAnalyzer('someNoneExistingAnalyzer'); - $this->assertFalse($result); + expect($result)->toBeFalse(); }); test('replace analyzer', function () { @@ -58,7 +58,7 @@ ; $newAnalyzer = $this->schemaManager->replaceAnalyzer($this->analyzer['name'], $newAnalyzerProps); - $this->assertSame('arangodb_php_client__test::' . $this->analyzer['name'], $newAnalyzer->name); + expect($newAnalyzer->name)->toBe('arangodb_php_client__test::' . $this->analyzer['name']); }); test('create and delete analyzer', function () { @@ -68,10 +68,10 @@ ]; $created = $this->schemaManager->createAnalyzer($analyzer); $this->assertObjectHasProperty('name', $created); - $this->assertSame('arangodb_php_client__test::' . $analyzer['name'], $created->name); + expect($created->name)->toBe('arangodb_php_client__test::' . $analyzer['name']); $deleted = $this->schemaManager->deleteAnalyzer($analyzer['name']); - $this->assertTrue($deleted); + expect($deleted)->toBeTrue(); }); test('delete with full name', function () { @@ -86,5 +86,5 @@ $deleted = $this->schemaManager->deleteAnalyzer($fullName); $hasAnalyzer = $this->schemaManager->hasAnalyzer($fullName); - $this->assertFalse($hasAnalyzer); + expect($hasAnalyzer)->toBeFalse(); }); diff --git a/tests/SchemaManagerCollectionsTest.php b/tests/SchemaManagerCollectionsTest.php index b067946..65d98d9 100644 --- a/tests/SchemaManagerCollectionsTest.php +++ b/tests/SchemaManagerCollectionsTest.php @@ -8,22 +8,22 @@ $this->skipTestOnArangoVersions('3.8', '>='); $result = $this->schemaManager->getCollections(); - $this->assertLessThanOrEqual(count($result), 10); - $this->assertIsObject($result[0]); + expect(10)->toBeLessThanOrEqual(count($result)); + expect($result[0])->toBeObject(); }); test('get collections', function () { $this->skipTestOnArangoVersions('3.8', '<'); $result = $this->schemaManager->getCollections(); - $this->assertLessThanOrEqual(count($result), 8); - $this->assertIsObject($result[0]); + expect(8)->toBeLessThanOrEqual(count($result)); + expect($result[0])->toBeObject(); }); test('get collections without system', function () { $result = $this->schemaManager->getCollections(true); - $this->assertEmpty($result); + expect($result)->toBeEmpty(); }); test('get collection', function () { @@ -31,17 +31,17 @@ $result = $this->schemaManager->getCollection($collections[0]->name); - $this->assertIsObject($result); + expect($result)->toBeObject(); $this->assertObjectHasProperty('name', $result); $this->assertObjectHasProperty('isSystem', $result); }); test('has collection', function () { $result = $this->schemaManager->hasCollection('_graphs'); - $this->assertTrue($result); + expect($result)->toBeTrue(); $result = $this->schemaManager->hasCollection('someNoneExistingCollection'); - $this->assertFalse($result); + expect($result)->toBeFalse(); }); test('get collection properties', function () { @@ -49,7 +49,7 @@ $result = $this->schemaManager->getCollectionProperties($collections[0]->name); - $this->assertIsObject($result); + expect($result)->toBeObject(); $this->assertObjectHasProperty('name', $result); $this->assertObjectHasProperty('isSystem', $result); $this->assertObjectHasProperty('statusString', $result); @@ -66,7 +66,7 @@ $this->assertObjectHasProperty('statusString', $result); $this->assertObjectHasProperty('keyOptions', $result); $this->assertObjectHasProperty('count', $result); - $this->assertIsNumeric($result->count); + expect($result->count)->toBeNumeric(); }); test('get collection document count', function () { @@ -74,7 +74,7 @@ $result = $this->schemaManager->getCollectionDocumentCount($collections[0]->name); - $this->assertIsNumeric($result); + expect($result)->toBeNumeric(); }); test('get collection statistics', function () { @@ -103,7 +103,7 @@ $newConfig = ['waitForSync' => true]; $result = $this->schemaManager->updateCollection($collection, $newConfig); - $this->assertTrue($result->waitForSync); + expect($result->waitForSync)->toBeTrue(); $this->schemaManager->deleteCollection($collection); }); @@ -121,7 +121,7 @@ } $result = $this->schemaManager->renameCollection($collection, $newName); - $this->assertSame($newName, $result->name); + expect($result->name)->toBe($newName); $this->schemaManager->deleteCollection($newName); }); @@ -131,7 +131,7 @@ if (!$this->schemaManager->hasCollection($collection)) { $this->schemaManager->createCollection($collection); } - $this->assertSame(0, $this->schemaManager->getCollectionWithDocumentCount($collection)->count); + expect($this->schemaManager->getCollectionWithDocumentCount($collection)->count)->toBe(0); $query = 'FOR i IN 1..10 INSERT { _key: CONCAT("test", i), @@ -141,11 +141,11 @@ $statement = $this->arangoClient->prepare($query); $statement->execute(); - $this->assertSame(0, count($statement->fetchAll())); + expect(count($statement->fetchAll()))->toBe(0); $this->schemaManager->truncateCollection($collection); - $this->assertSame(0, $this->schemaManager->getCollectionWithDocumentCount($collection)->count); + expect($this->schemaManager->getCollectionWithDocumentCount($collection)->count)->toBe(0); $this->schemaManager->deleteCollection($collection); }); @@ -155,12 +155,12 @@ if (!$this->schemaManager->hasCollection($collection)) { $result = $this->schemaManager->createCollection($collection, $options); - $this->assertEquals($collection, $result->name); + expect($result->name)->toEqual($collection); } $result = $this->schemaManager->deleteCollection($collection); - $this->assertTrue($result); - $this->assertFalse($this->schemaManager->hasCollection($collection)); + expect($result)->toBeTrue(); + expect($this->schemaManager->hasCollection($collection))->toBeFalse(); }); test('create collection with options', function () { @@ -172,14 +172,14 @@ } $collectionProperties = $this->schemaManager->getCollectionProperties('users'); - $this->assertTrue($collectionProperties->waitForSync); + expect($collectionProperties->waitForSync)->toBeTrue(); // $waitForSyncReplication & $enforceReplicationFactor are not listed in the properties, so the lack of // of an exception somewhat tests these options... $result = $this->schemaManager->deleteCollection($collection); - $this->assertTrue($result); - $this->assertFalse($this->schemaManager->hasCollection($collection)); + expect($result)->toBeTrue(); + expect($this->schemaManager->hasCollection($collection))->toBeFalse(); }); test('create edge collection', function () { @@ -191,8 +191,8 @@ $result = $this->schemaManager->createEdgeCollection($collection); - $this->assertEquals($collection, $result->name); - $this->assertSame(3, $result->type); + expect($result->name)->toEqual($collection); + expect($result->type)->toBe(3); $this->schemaManager->deleteCollection($collection); }); diff --git a/tests/SchemaManagerDatabasesTest.php b/tests/SchemaManagerDatabasesTest.php index 1cae8b4..3628bb3 100644 --- a/tests/SchemaManagerDatabasesTest.php +++ b/tests/SchemaManagerDatabasesTest.php @@ -11,18 +11,18 @@ $this->arangoClient->setDatabase('_system'); $result = $this->schemaManager->getCurrentDatabase(); - $this->assertSame('1', $result->id); - $this->assertSame('_system', $result->name); - $this->assertSame(true, $result->isSystem); - $this->assertSame('none', $result->path); + expect($result->id)->toBe('1'); + expect($result->name)->toBe('_system'); + expect($result->isSystem)->toBe(true); + expect($result->path)->toBe('none'); }); test('get databases', function () { $result = $this->schemaManager->getDatabases(); - $this->assertLessThanOrEqual(count($result), 2); + expect(2)->toBeLessThanOrEqual(count($result)); foreach ($result as $database) { - $this->assertIsString($database); + expect($database)->toBeString(); } }); @@ -32,19 +32,19 @@ if (!in_array($database, $existingDatabases)) { $result = $this->schemaManager->createDatabase($database); - $this->assertTrue($result); + expect($result)->toBeTrue(); } $result = $this->schemaManager->deleteDatabase($database); - $this->assertTrue($result); + expect($result)->toBeTrue(); $existingDatabases = $this->schemaManager->getDatabases(); $this->assertNotContains($database, $existingDatabases); }); test('has database', function () { $check = $this->schemaManager->hasDatabase('someNoneExistingDatabase'); - $this->assertFalse($check); + expect($check)->toBeFalse(); $check = $this->schemaManager->hasDatabase($this->testDatabaseName); - $this->assertTrue($check); + expect($check)->toBeTrue(); }); diff --git a/tests/SchemaManagerGraphsTest.php b/tests/SchemaManagerGraphsTest.php index 78fbd26..62f022d 100644 --- a/tests/SchemaManagerGraphsTest.php +++ b/tests/SchemaManagerGraphsTest.php @@ -6,10 +6,10 @@ test('create and delete graph', function () { $result = $this->schemaManager->createGraph('locations', [], true); - $this->assertSame('_graphs/locations', $result->_id); + expect($result->_id)->toBe('_graphs/locations'); $result = $this->schemaManager->deleteGraph('locations'); - $this->assertTrue($result); + expect($result)->toBeTrue(); }); test('create graph with edges', function () { @@ -32,8 +32,8 @@ ], true, ); - $this->assertEquals(1, is_countable($result->edgeDefinitions) ? count($result->edgeDefinitions) : 0); - $this->assertEquals($result->_id, '_graphs/relations'); + expect(is_countable($result->edgeDefinitions) ? count($result->edgeDefinitions) : 0)->toEqual(1); + expect('_graphs/relations')->toEqual($result->_id); $this->schemaManager->deleteGraph('relations'); $this->schemaManager->deleteCollection('children'); @@ -44,7 +44,7 @@ test('get graphs no results', function () { $result = $this->schemaManager->getGraphs(); - $this->assertLessThanOrEqual(0, count($result)); + expect(count($result))->toBeLessThanOrEqual(0); }); test('get graphs with results', function () { @@ -57,9 +57,9 @@ $result = $this->schemaManager->getGraphs(); - $this->assertEquals(2, count($result)); - $this->assertEquals('characters', $result[0]->_key); - $this->assertEquals('locations', $result[1]->_key); + expect(count($result))->toEqual(2); + expect($result[0]->_key)->toEqual('characters'); + expect($result[1]->_key)->toEqual('locations'); $this->schemaManager->deleteGraph('characters'); $this->schemaManager->deleteGraph('locations'); @@ -70,11 +70,11 @@ $this->schemaManager->createGraph('locations'); } $result = $this->schemaManager->hasGraph('locations'); - $this->assertTrue($result); + expect($result)->toBeTrue(); $this->schemaManager->deleteGraph('locations'); $result = $this->schemaManager->hasGraph('locations'); - $this->assertFalse($result); + expect($result)->toBeFalse(); }); test('get graph', function () { @@ -84,7 +84,7 @@ $result = $this->schemaManager->getGraph('locations'); - $this->assertEquals('locations', $result->_key); + expect($result->_key)->toEqual('locations'); $this->schemaManager->deleteGraph('locations'); }); @@ -114,9 +114,9 @@ $results = $this->schemaManager->getGraphVertices('relations'); - $this->assertEquals(2, count($results)); - $this->assertEquals($results[0], 'characters'); - $this->assertEquals($results[1], 'orphanVertices'); + expect(count($results))->toEqual(2); + expect('characters')->toEqual($results[0]); + expect('orphanVertices')->toEqual($results[1]); $this->schemaManager->deleteGraph('relations'); $this->schemaManager->deleteCollection('children'); @@ -147,8 +147,8 @@ $result = $this->schemaManager->addGraphVertex('relations', $newVertex); - $this->assertContains('orphanVertices', $result->orphanCollections); - $this->assertContains($newVertex, $result->orphanCollections); + expect($result->orphanCollections)->toContain('orphanVertices'); + expect($result->orphanCollections)->toContain($newVertex); $this->schemaManager->deleteGraph('relations'); $this->schemaManager->deleteCollection('children'); @@ -182,7 +182,7 @@ $this->assertNotContains('orphanVertices', $result->orphanCollections); $checkDropped = $this->schemaManager->hasCollection('orphanVertices'); - $this->assertFalse($checkDropped); + expect($checkDropped)->toBeFalse(); $this->schemaManager->deleteGraph('relations'); $this->schemaManager->deleteCollection('children'); @@ -214,8 +214,8 @@ $results = $this->schemaManager->getGraphEdges('relations'); - $this->assertEquals(1, count($results)); - $this->assertEquals($results[0], 'children'); + expect(count($results))->toEqual(1); + expect('children')->toEqual($results[0]); $this->schemaManager->deleteGraph('relations'); $this->schemaManager->deleteCollection('children'); @@ -247,7 +247,7 @@ $result = $this->schemaManager->addGraphEdge('relations', $newEdge); - $this->assertEquals($newEdge['collection'], $result->edgeDefinitions[1]->collection); + expect($result->edgeDefinitions[1]->collection)->toEqual($newEdge['collection']); $this->schemaManager->deleteGraph('relations'); $this->schemaManager->deleteCollection('children'); @@ -286,7 +286,7 @@ true, ); - $this->assertEquals($newEdge['collection'], $result->edgeDefinitions[0]->collection); + expect($result->edgeDefinitions[0]->collection)->toEqual($newEdge['collection']); $this->schemaManager->deleteGraph('relations'); $this->schemaManager->deleteCollection('children'); @@ -322,8 +322,8 @@ true, ); - $this->assertEquals(1, is_countable($result->edgeDefinitions) ? count($result->edgeDefinitions) : 0); - $this->assertEquals('vassals', $result->edgeDefinitions[0]->collection); + expect(is_countable($result->edgeDefinitions) ? count($result->edgeDefinitions) : 0)->toEqual(1); + expect($result->edgeDefinitions[0]->collection)->toEqual('vassals'); $this->schemaManager->deleteGraph('relations'); $this->schemaManager->deleteCollection('children'); diff --git a/tests/SchemaManagerIndexesTest.php b/tests/SchemaManagerIndexesTest.php index 6990c54..37845c9 100644 --- a/tests/SchemaManagerIndexesTest.php +++ b/tests/SchemaManagerIndexesTest.php @@ -19,9 +19,9 @@ test('get indexes', function () { $indexes = $this->schemaManager->getIndexes($this->collection); - $this->assertIsObject($indexes[0]); + expect($indexes[0])->toBeObject(); $this->assertObjectHasProperty('name', $indexes[0]); - $this->assertSame('primary', $indexes[0]->name); + expect($indexes[0]->name)->toBe('primary'); }); test('get index', function () { @@ -41,7 +41,7 @@ $index = $this->schemaManager->getIndexByName($this->collection, $indexName); - $this->assertSame($indexName, $index->name); + expect($index->name)->toBe($indexName); }); test('create index', function () { @@ -55,10 +55,10 @@ $created = $this->schemaManager->createIndex($this->collection, $index); $result = $this->schemaManager->getIndexByName($this->collection, 'email_persistent_unique'); - $this->assertSame($index['name'], $result->name); - $this->assertSame($index['fields'][0], $result->fields[0]); - $this->assertSame($index['unique'], $result->unique); - $this->assertSame($index['sparse'], $result->sparse); + expect($result->name)->toBe($index['name']); + expect($result->fields[0])->toBe($index['fields'][0]); + expect($result->unique)->toBe($index['unique']); + expect($result->sparse)->toBe($index['sparse']); }); test('delete index', function () { @@ -73,7 +73,7 @@ $found = $this->schemaManager->getIndexByName($this->collection, 'email_persistent_unique'); $deleted = $this->schemaManager->deleteIndex($found->id); - $this->assertEquals($created->id, $deleted->id); + expect($deleted->id)->toEqual($created->id); $searchForDeleted = $this->schemaManager->getIndexByName($this->collection, 'email_persistent_unique'); - $this->assertFalse($searchForDeleted); + expect($searchForDeleted)->toBeFalse(); }); diff --git a/tests/SchemaManagerUsersTest.php b/tests/SchemaManagerUsersTest.php index 8fdb8ed..b9e5c69 100644 --- a/tests/SchemaManagerUsersTest.php +++ b/tests/SchemaManagerUsersTest.php @@ -25,21 +25,21 @@ $name = 'root'; $user = $this->schemaManager->getUser($name); - $this->assertSame($name, $user->user); + expect($user->user)->toBe($name); }); test('get users', function () { $users = $this->schemaManager->getUsers(); - $this->assertIsArray($users); + expect($users)->toBeArray(); $this->assertObjectHasProperty('user', $users[0]); }); test('has user', function () { $result = $this->schemaManager->hasUser('root'); - $this->assertTrue($result); + expect($result)->toBeTrue(); $result = $this->schemaManager->hasUser('nonExistingUser'); - $this->assertFalse($result); + expect($result)->toBeFalse(); }); test('create and delete user', function () { @@ -58,11 +58,11 @@ } $created = $this->schemaManager->createUser($user); - $this->assertSame($user['user'], $created->user); + expect($created->user)->toBe($user['user']); $this->schemaManager->deleteUser($user['user']); $checkDeleted = $this->schemaManager->hasUser($user['user']); - $this->assertFalse($checkDeleted); + expect($checkDeleted)->toBeFalse(); }); test('update user', function () { @@ -72,7 +72,7 @@ ]; $updated = $this->schemaManager->updateUser($this->userName, $newUserData); - $this->assertSame($newUserData['user'], $updated->user); + expect($updated->user)->toBe($newUserData['user']); }); test('replace user', function () { @@ -82,13 +82,13 @@ ]; $replaced = $this->schemaManager->replaceUser($this->userName, $newUserData); - $this->assertSame($this->userName, $replaced->user); + expect($replaced->user)->toBe($this->userName); }); test('get database access level', function () { $accessLevel = $this->schemaManager->getDatabaseAccessLevel('root', '_system'); - $this->assertSame('rw', $accessLevel); + expect($accessLevel)->toBe('rw'); }); test('set database access level', function () { @@ -99,8 +99,8 @@ $accessLevel = $this->schemaManager->getDatabaseAccessLevel($this->userName, $this->accessDatabase); $this->assertObjectHasProperty($this->accessDatabase, $results); - $this->assertSame($grant, $results->{$this->accessDatabase}); - $this->assertSame($grant, $accessLevel); + expect($results->{$this->accessDatabase})->toBe($grant); + expect($accessLevel)->toBe($grant); tearDownAccessTest(); }); @@ -111,13 +111,13 @@ $this->schemaManager->setDatabaseAccessLevel($this->userName, $this->accessDatabase, $grant); $accessLevel = $this->schemaManager->getDatabaseAccessLevel($this->userName, $this->accessDatabase); - $this->assertSame($grant, $accessLevel); + expect($accessLevel)->toBe($grant); $result = $this->schemaManager->clearDatabaseAccessLevel($this->userName, $this->accessDatabase); $accessLevel = $this->schemaManager->getDatabaseAccessLevel($this->userName, $this->accessDatabase); - $this->assertTrue($result); - $this->assertSame('none', $accessLevel); + expect($result)->toBeTrue(); + expect($accessLevel)->toBe('none'); tearDownAccessTest(); }); diff --git a/tests/SchemaManagerViewsTest.php b/tests/SchemaManagerViewsTest.php index 21314ce..68c1e0b 100644 --- a/tests/SchemaManagerViewsTest.php +++ b/tests/SchemaManagerViewsTest.php @@ -19,13 +19,13 @@ test('get views', function () { $views = $this->schemaManager->getViews(); - $this->assertSame($this->view['name'], $views[0]->name); + expect($views[0]->name)->toBe($this->view['name']); }); test('get view', function () { $view = $this->schemaManager->getView($this->view['name']); - $this->assertSame($this->view['name'], $view->name); + expect($view->name)->toBe($this->view['name']); $this->assertObjectHasProperty('type', $view); $this->assertObjectHasProperty('links', $view); }); @@ -33,23 +33,23 @@ test('get view properties', function () { $view = $this->schemaManager->getViewProperties($this->view['name']); - $this->assertSame($this->view['name'], $view->name); + expect($view->name)->toBe($this->view['name']); $this->assertObjectHasProperty('type', $view); $this->assertObjectHasProperty('links', $view); }); test('has view', function () { $result = $this->schemaManager->hasView($this->view['name']); - $this->assertTrue($result); + expect($result)->toBeTrue(); $result = $this->schemaManager->hasView('someNoneExistingView'); - $this->assertFalse($result); + expect($result)->toBeFalse(); }); test('rename view', function () { $newName = 'newName'; $result = $this->schemaManager->renameView($this->view['name'], $newName); - $this->assertSame($newName, $result->name); + expect($result->name)->toBe($newName); $this->schemaManager->deleteView($newName); }); @@ -61,7 +61,7 @@ ]; $result = $this->schemaManager->updateView($this->view['name'], $newViewProps); - $this->assertSame(3, $result->cleanupIntervalStep); + expect($result->cleanupIntervalStep)->toBe(3); }); test('replace view', function () { @@ -73,8 +73,8 @@ ]; $newView = $this->schemaManager->replaceView($this->view['name'], $newViewProps); - $this->assertSame($newViewProps['primarySort'][0]['field'], $newView->primarySort[0]->field); - $this->assertFalse($newView->primarySort[0]->asc); + expect($newView->primarySort[0]->field)->toBe($newViewProps['primarySort'][0]['field']); + expect($newView->primarySort[0]->asc)->toBeFalse(); }); test('create and delete view', function () { @@ -83,8 +83,8 @@ ]; $created = $this->schemaManager->createView($view); $this->assertObjectHasProperty('name', $created); - $this->assertSame($view['name'], $created->name); + expect($created->name)->toBe($view['name']); $deleted = $this->schemaManager->deleteView($view['name']); - $this->assertTrue($deleted); + expect($deleted)->toBeTrue(); }); diff --git a/tests/StatementTest.php b/tests/StatementTest.php index e111a68..7dad2cf 100644 --- a/tests/StatementTest.php +++ b/tests/StatementTest.php @@ -25,7 +25,7 @@ $statement = $this->statement->setQuery($query); - $this->assertSame($query, $statement->getQuery()); + expect($statement->getQuery())->toBe($query); }); test('explain', function () { @@ -60,13 +60,13 @@ $statement = $this->arangoClient->prepare($query, [], $options); $statement->execute(); - $this->assertSame(0, $statement->getCount()); + expect($statement->getCount())->toBe(0); }); test('get count not set', function () { $this->statement->execute(); - $this->assertNull($this->statement->getCount()); + expect($this->statement->getCount())->toBeNull(); }); test('fetch all', function () { @@ -75,11 +75,11 @@ $query = 'FOR doc IN ' . $this->collection . ' RETURN doc'; $this->statement->setQuery($query); $executed = $this->statement->execute(); - $this->assertTrue($executed); + expect($executed)->toBeTrue(); $results = $this->statement->fetchAll(); - $this->assertEquals(10, is_countable($results) ? count($results) : 0); - $this->assertSame('test1', $results[0]->_key); + expect(is_countable($results) ? count($results) : 0)->toEqual(10); + expect($results[0]->_key)->toBe('test1'); }); test('results greater than batch size', function () { @@ -90,11 +90,11 @@ $options = ['batchSize' => 2]; $statement = $this->arangoClient->prepare($query, [], $options); $executed = $statement->execute(); - $this->assertTrue($executed); + expect($executed)->toBeTrue(); $results = $statement->fetchAll(); - $this->assertEquals(10, count($results)); - $this->assertSame('test1', $results[0]->_key); + expect(count($results))->toEqual(10); + expect($results[0]->_key)->toBe('test1'); }); test('statement is iterable', function () { @@ -106,7 +106,7 @@ $this->assertObjectHasProperty('foobar', $document); $count++; } - $this->assertEquals(10, $count); + expect($count)->toEqual(10); }); test('get writes executed', function () { @@ -120,7 +120,7 @@ $statement = $this->arangoClient->prepare($query); $statement->execute(); - $this->assertSame(10, $statement->getWritesExecuted()); + expect($statement->getWritesExecuted())->toBe(10); }); // Helpers diff --git a/tests/SupportsTransactionsTest.php b/tests/SupportsTransactionsTest.php index 6afba32..809cde1 100644 --- a/tests/SupportsTransactionsTest.php +++ b/tests/SupportsTransactionsTest.php @@ -11,13 +11,13 @@ test('transactions', function () { $transactionManager = $this->arangoClient->transactions(); - $this->assertInstanceOf(TransactionManager::class, $transactionManager); + expect($transactionManager)->toBeInstanceOf(TransactionManager::class); }); test('begin transaction', function () { $transactionId = $this->arangoClient->beginTransaction(); $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); - $this->assertSame($transactionId, $runningTransactions[0]->id); + expect($runningTransactions[0]->id)->toBe($transactionId); $this->arangoClient->abort(); }); @@ -25,7 +25,7 @@ test('begin', function () { $transactionId = $this->arangoClient->begin(); $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); - $this->assertSame($transactionId, $runningTransactions[0]->id); + expect($runningTransactions[0]->id)->toBe($transactionId); $this->arangoClient->abort(); }); @@ -33,25 +33,25 @@ test('abort', function () { $transactionId = $this->arangoClient->beginTransaction(); $aborted = $this->arangoClient->abort(); - $this->assertTrue($aborted); + expect($aborted)->toBeTrue(); $transactionsListedInManager = $this->arangoClient->transactions()->getTransactions(); $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); $this->assertArrayNotHasKey($transactionId, $transactionsListedInManager); - $this->assertFalse(array_search($transactionId, array_column($runningTransactions, 'id'))); + expect(array_search($transactionId, array_column($runningTransactions, 'id')))->toBeFalse(); }); test('roll back', function () { $transactionId = $this->arangoClient->beginTransaction(); $aborted = $this->arangoClient->rollBack(); - $this->assertTrue($aborted); + expect($aborted)->toBeTrue(); $transactionsListedInManager = $this->arangoClient->transactions()->getTransactions(); $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); $this->assertArrayNotHasKey($transactionId, $transactionsListedInManager); - $this->assertFalse(array_search($transactionId, array_column($runningTransactions, 'id'))); + expect(array_search($transactionId, array_column($runningTransactions, 'id')))->toBeFalse(); }); test('commit', function () { @@ -84,7 +84,7 @@ $getStatement = $this->arangoClient->prepare($getQuery); $getStatement->execute(); - $this->assertEquals(10, count($getStatement->fetchAll())); + expect(count($getStatement->fetchAll()))->toEqual(10); $this->arangoClient->commit(); @@ -92,7 +92,7 @@ $getStatement = $this->arangoClient->prepare($getQuery); $getStatement->execute(); - $this->assertEquals(10, count($getStatement->fetchAll())); + expect(count($getStatement->fetchAll()))->toEqual(10); $this->arangoClient->schema()->deleteCollection('Users'); $this->arangoClient->schema()->deleteCollection('Customers'); @@ -104,6 +104,6 @@ $this->arangoClient->setTransactionManager($newTransactionManager); $retrievedNewTransactionManager = $this->arangoClient->getTransactionManager(); - $this->assertNull($oldTransactionManager); - $this->assertEquals(spl_object_id($newTransactionManager), spl_object_id($retrievedNewTransactionManager)); + expect($oldTransactionManager)->toBeNull(); + expect(spl_object_id($retrievedNewTransactionManager))->toEqual(spl_object_id($newTransactionManager)); }); diff --git a/tests/TransactionManagerTest.php b/tests/TransactionManagerTest.php index 1ebe77c..0199bb8 100644 --- a/tests/TransactionManagerTest.php +++ b/tests/TransactionManagerTest.php @@ -13,7 +13,7 @@ test('begin', function () { $transactionId = $this->transactionManager->begin(); $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); - $this->assertSame($transactionId, $runningTransactions[0]->id); + expect($runningTransactions[0]->id)->toBe($transactionId); $this->transactionManager->abort(); }); @@ -21,7 +21,7 @@ test('get transactions', function () { $transactions = []; $begunTransactions = $this->transactionManager->getTransactions(); - $this->assertEmpty($begunTransactions); + expect($begunTransactions)->toBeEmpty(); $id = $this->transactionManager->begin(); $transactions[$id] = $id; @@ -30,7 +30,7 @@ $begunTransactions = $this->transactionManager->getTransactions(); - $this->assertSame($transactions, $begunTransactions); + expect($begunTransactions)->toBe($transactions); }); test('get transaction', function () { @@ -40,7 +40,7 @@ $lastTransaction = $this->transactionManager->getTransaction(); - $this->assertSame($transactions[1], $lastTransaction); + expect($lastTransaction)->toBe($transactions[1]); }); test('get transaction before begin', function () { @@ -57,10 +57,10 @@ $transactionsListedInManager = $this->transactionManager->getTransactions(); foreach ($transactions as $key => $id) { - $this->assertContains($id, $transactionsListedInManager); + expect($transactionsListedInManager)->toContain($id); $this->assertNotFalse(array_search($id, array_column($runningTransactions, 'id'))); } - $this->assertEquals(count($transactions), count($transactionsListedInManager)); + expect(count($transactionsListedInManager))->toEqual(count($transactions)); $this->transactionManager->abortRunningTransactions(); }); @@ -68,13 +68,13 @@ test('abort', function () { $transactionId = $this->transactionManager->begin(); $aborted = $this->transactionManager->abort(); - $this->assertTrue($aborted); + expect($aborted)->toBeTrue(); $transactionsListedInManager = $this->transactionManager->getTransactions(); $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); $this->assertArrayNotHasKey($transactionId, $transactionsListedInManager); - $this->assertFalse(array_search($transactionId, array_column($runningTransactions, 'id'))); + expect(array_search($transactionId, array_column($runningTransactions, 'id')))->toBeFalse(); }); test('abort before commit', function () { @@ -97,9 +97,9 @@ $transactionsListedInManager = $this->transactionManager->getTransactions(); $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); - $this->assertEmpty($transactionsListedInManager); + expect($transactionsListedInManager)->toBeEmpty(); foreach ($transactions as $id) { - $this->assertFalse(array_search($id, array_column($runningTransactions, 'id'))); + expect(array_search($id, array_column($runningTransactions, 'id')))->toBeFalse(); } }); @@ -133,7 +133,7 @@ $getStatement = $this->arangoClient->prepare($getQuery); $getStatement->execute(); - $this->assertEquals(10, count($getStatement->fetchAll())); + expect(count($getStatement->fetchAll()))->toEqual(10); $this->transactionManager->commit(); @@ -141,7 +141,7 @@ $getStatement = $this->arangoClient->prepare($getQuery); $getStatement->execute(); - $this->assertEquals(10, count($getStatement->fetchAll())); + expect(count($getStatement->fetchAll()))->toEqual(10); $this->arangoClient->schema()->deleteCollection('Users'); $this->arangoClient->schema()->deleteCollection('Customers'); From 705e96999aa4d7ba7074a823f0293b5ae9b1508f Mon Sep 17 00:00:00 2001 From: Shift Date: Mon, 7 Oct 2024 22:26:22 +0000 Subject: [PATCH 6/8] Optimize uses --- tests/Pest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Pest.php b/tests/Pest.php index fc70c72..f9ac1d7 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,5 +1,6 @@ Date: Mon, 7 Oct 2024 22:26:22 +0000 Subject: [PATCH 7/8] Use Pest test runner --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2b9f5d3..20a0461 100644 --- a/composer.json +++ b/composer.json @@ -45,7 +45,7 @@ }, "scripts": { "analyse": "vendor/bin/phpstan analyse", - "test": "vendor/bin/phpunit", + "test": "vendor/bin/pest", "test:coverage": "vendor/bin/phpunit --coverage-clover clover.xml --whitelist src", "style": "vendor/bin/pint" }, From 52fc3c12b6fe5520e186f8c8081bad6400e8317c Mon Sep 17 00:00:00 2001 From: Bas Date: Tue, 8 Oct 2024 13:16:53 +0200 Subject: [PATCH 8/8] Migrated tests from Phpunit to Pest --- .github/workflows/run-tests.yml | 2 +- composer.json | 7 ++- phpunit.xml | 2 +- tests/AdminManagerTest.php | 2 + tests/ArangoClientTest.php | 11 ----- tests/ExceptionsTest.php | 4 +- tests/MonitorManagerTest.php | 1 - tests/Pest.php | 11 +++++ tests/SchemaManagerAnalyzersTest.php | 12 ++--- tests/SchemaManagerCollectionsTest.php | 22 ++++----- tests/SchemaManagerDatabasesTest.php | 6 +-- tests/SchemaManagerGraphsTest.php | 4 +- tests/SchemaManagerIndexesTest.php | 3 +- tests/SchemaManagerUsersTest.php | 24 +++------- tests/SchemaManagerViewsTest.php | 3 +- tests/StatementTest.php | 24 ++-------- ...onsTest.php => StreamTransactionsTest.php} | 6 +-- tests/TestCase.php | 46 ++++++++++++++++++- 18 files changed, 101 insertions(+), 89 deletions(-) rename tests/{SupportsTransactionsTest.php => StreamTransactionsTest.php} (97%) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 3e88eb8..646bf37 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -13,7 +13,7 @@ jobs: matrix: os: [ubuntu-latest] arangodb: ["3.10", 3.11, 3.12] - php: [8.1, 8.2, 8.3] + php: [8.2, 8.3] stability: [prefer-stable] name: P${{ matrix.php }} - A${{ matrix.arangodb }} - ${{ matrix.stability }} diff --git a/composer.json b/composer.json index 20a0461..e5b86ca 100644 --- a/composer.json +++ b/composer.json @@ -46,10 +46,13 @@ "scripts": { "analyse": "vendor/bin/phpstan analyse", "test": "vendor/bin/pest", - "test:coverage": "vendor/bin/phpunit --coverage-clover clover.xml --whitelist src", + "test:coverage": "vendor/bin/pest --coverage-clover clover.xml", "style": "vendor/bin/pint" }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true + } } } diff --git a/phpunit.xml b/phpunit.xml index 3e91546..e12801e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -16,6 +16,6 @@ - + diff --git a/tests/AdminManagerTest.php b/tests/AdminManagerTest.php index 4b30d37..8d00a17 100644 --- a/tests/AdminManagerTest.php +++ b/tests/AdminManagerTest.php @@ -5,6 +5,7 @@ use ArangoClient\Admin\AdminManager; uses(Tests\TestCase::class); + beforeEach(function () { $this->adminManager = new AdminManager($this->arangoClient); }); @@ -28,5 +29,6 @@ test('get running transactions', function () { $transactions = $this->adminManager->getRunningTransactions(); + expect($transactions)->toBeEmpty(); }); diff --git a/tests/ArangoClientTest.php b/tests/ArangoClientTest.php index 1cb7df6..0187280 100644 --- a/tests/ArangoClientTest.php +++ b/tests/ArangoClientTest.php @@ -234,14 +234,3 @@ $this->schemaManager->deleteCollection($collection); }); - -// Helpers -function checkHttp2Support() -{ - // First assert that CURL supports http2! - if (!curl_version()['features'] || CURL_VERSION_HTTP2 === 0) { - test()->markTestSkipped('The installed version of CURL does not support the HTTP2 protocol.'); - } - // HTTP/2 is only supported by ArangoDB 3.7 and up. - test()->skipTestOnArangoVersions('3.7'); -} diff --git a/tests/ExceptionsTest.php b/tests/ExceptionsTest.php index 3292201..668aca1 100644 --- a/tests/ExceptionsTest.php +++ b/tests/ExceptionsTest.php @@ -1,9 +1,9 @@ schemaManager->hasDatabase($database)) { diff --git a/tests/MonitorManagerTest.php b/tests/MonitorManagerTest.php index 70c2cba..4f941db 100644 --- a/tests/MonitorManagerTest.php +++ b/tests/MonitorManagerTest.php @@ -72,6 +72,5 @@ arangodb_aql_local_query_memory_limit_reached_total{role="SINGLE"} 0 2211753600'; $result = $prometheus->parseText($rawMetrics); - expect($result)->toBeObject(); expect($result->arangodb_aql_local_query_memory_limit_reached_total->timestamp)->toEqual(2211753600); }); diff --git a/tests/Pest.php b/tests/Pest.php index f9ac1d7..ee45215 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -39,3 +39,14 @@ */ /** @link https://pestphp.com/docs/custom-helpers */ + +// Helpers +function checkHttp2Support() +{ + // First assert that CURL supports http2! + if (!curl_version()['features'] || CURL_VERSION_HTTP2 === 0) { + test()->markTestSkipped('The installed version of CURL does not support the HTTP2 protocol.'); + } + // HTTP/2 is only supported by ArangoDB 3.7 and up. + test()->skipTestOnArangoVersions('3.7'); +} diff --git a/tests/SchemaManagerAnalyzersTest.php b/tests/SchemaManagerAnalyzersTest.php index 1158246..0195370 100644 --- a/tests/SchemaManagerAnalyzersTest.php +++ b/tests/SchemaManagerAnalyzersTest.php @@ -1,25 +1,21 @@ schemaManager->hasAnalyzer($this->analyzer['name'])) { $this->schemaManager->createAnalyzer($this->analyzer); } }); afterEach(function () { - \Tests\TestCase::tearDown(); - if ($this->schemaManager->hasAnalyzer($this->analyzer['name'])) { $this->schemaManager->deleteAnalyzer($this->analyzer['name']); } }); - test('get analyzers', function () { $analyzers = $this->schemaManager->getAnalyzers(); @@ -79,11 +75,11 @@ 'name' => 'coolnewanalyzer', 'type' => 'identity', ]; - $created = $this->schemaManager->createAnalyzer($analyzer); + $this->schemaManager->createAnalyzer($analyzer); $fullName = 'arangodb_php_client__test::' . $analyzer['name']; - $deleted = $this->schemaManager->deleteAnalyzer($fullName); + $this->schemaManager->deleteAnalyzer($fullName); $hasAnalyzer = $this->schemaManager->hasAnalyzer($fullName); expect($hasAnalyzer)->toBeFalse(); diff --git a/tests/SchemaManagerCollectionsTest.php b/tests/SchemaManagerCollectionsTest.php index 65d98d9..f913ddd 100644 --- a/tests/SchemaManagerCollectionsTest.php +++ b/tests/SchemaManagerCollectionsTest.php @@ -1,19 +1,10 @@ skipTestOnArangoVersions('3.8', '>='); - $result = $this->schemaManager->getCollections(); - - expect(10)->toBeLessThanOrEqual(count($result)); - expect($result[0])->toBeObject(); -}); +uses(Tests\TestCase::class); test('get collections', function () { - $this->skipTestOnArangoVersions('3.8', '<'); $result = $this->schemaManager->getCollections(); expect(8)->toBeLessThanOrEqual(count($result)); @@ -32,8 +23,7 @@ $result = $this->schemaManager->getCollection($collections[0]->name); expect($result)->toBeObject(); - $this->assertObjectHasProperty('name', $result); - $this->assertObjectHasProperty('isSystem', $result); + expect((array) $result)->toHaveKeys(['globallyUniqueId', 'isSystem', 'status', 'type', 'name', 'id']); }); test('has collection', function () { @@ -131,7 +121,9 @@ if (!$this->schemaManager->hasCollection($collection)) { $this->schemaManager->createCollection($collection); } + expect($this->schemaManager->getCollectionWithDocumentCount($collection)->count)->toBe(0); + $query = 'FOR i IN 1..10 INSERT { _key: CONCAT("test", i), @@ -146,7 +138,11 @@ $this->schemaManager->truncateCollection($collection); expect($this->schemaManager->getCollectionWithDocumentCount($collection)->count)->toBe(0); - $this->schemaManager->deleteCollection($collection); + + if ($this->schemaManager->hasCollection($collection)) { + $this->schemaManager->deleteCollection($collection); + } + }); test('create and delete collection', function () { diff --git a/tests/SchemaManagerDatabasesTest.php b/tests/SchemaManagerDatabasesTest.php index 3628bb3..8525c32 100644 --- a/tests/SchemaManagerDatabasesTest.php +++ b/tests/SchemaManagerDatabasesTest.php @@ -1,10 +1,10 @@ schemaManager->createGraph('locations', [], true); expect($result->_id)->toBe('_graphs/locations'); diff --git a/tests/SchemaManagerIndexesTest.php b/tests/SchemaManagerIndexesTest.php index 37845c9..5061391 100644 --- a/tests/SchemaManagerIndexesTest.php +++ b/tests/SchemaManagerIndexesTest.php @@ -1,8 +1,9 @@ schemaManager->hasCollection($this->collection)) { $this->schemaManager->createCollection($this->collection); diff --git a/tests/SchemaManagerUsersTest.php b/tests/SchemaManagerUsersTest.php index b9e5c69..8c10af9 100644 --- a/tests/SchemaManagerUsersTest.php +++ b/tests/SchemaManagerUsersTest.php @@ -1,8 +1,9 @@ $this->userName, @@ -92,7 +93,7 @@ }); test('set database access level', function () { - setUpAccessTest(); + $this->setUpAccessTest(); $grant = 'rw'; $results = $this->schemaManager->setDatabaseAccessLevel($this->userName, $this->accessDatabase, $grant); @@ -102,11 +103,11 @@ expect($results->{$this->accessDatabase})->toBe($grant); expect($accessLevel)->toBe($grant); - tearDownAccessTest(); + $this->tearDownAccessTest(); }); test('clear database access level', function () { - setUpAccessTest(); + $this->setUpAccessTest(); $grant = 'rw'; $this->schemaManager->setDatabaseAccessLevel($this->userName, $this->accessDatabase, $grant); @@ -119,18 +120,5 @@ expect($result)->toBeTrue(); expect($accessLevel)->toBe('none'); - tearDownAccessTest(); + $this->tearDownAccessTest(); }); - -// Helpers -function setUpAccessTest() -{ - if (!test()->schemaManager->hasDatabase(test()->accessDatabase)) { - test()->schemaManager->createDatabase(test()->accessDatabase); - } -} - -function tearDownAccessTest() -{ - test()->schemaManager->deleteDatabase(test()->accessDatabase); -} diff --git a/tests/SchemaManagerViewsTest.php b/tests/SchemaManagerViewsTest.php index 68c1e0b..5ed96ad 100644 --- a/tests/SchemaManagerViewsTest.php +++ b/tests/SchemaManagerViewsTest.php @@ -1,8 +1,9 @@ schemaManager->hasView($this->view['name'])) { $this->schemaManager->createView($this->view); diff --git a/tests/StatementTest.php b/tests/StatementTest.php index 7dad2cf..8a6ed9e 100644 --- a/tests/StatementTest.php +++ b/tests/StatementTest.php @@ -2,8 +2,8 @@ declare(strict_types=1); - uses(Tests\TestCase::class); + beforeEach(function () { if (!$this->schemaManager->hasCollection($this->collection)) { $this->schemaManager->createCollection($this->collection); @@ -19,7 +19,6 @@ } }); - test('set and get query', function () { $query = 'FOR doc IN ' . $this->collection . ' LIMIT 1 RETURN doc'; @@ -70,7 +69,7 @@ }); test('fetch all', function () { - generateTestDocuments(); + $this->generateTestDocuments(); $query = 'FOR doc IN ' . $this->collection . ' RETURN doc'; $this->statement->setQuery($query); @@ -83,7 +82,7 @@ }); test('results greater than batch size', function () { - generateTestDocuments(); + $this->generateTestDocuments(); // Retrieve data in batches of 2 $query = 'FOR doc IN ' . $this->collection . ' RETURN doc'; @@ -98,7 +97,7 @@ }); test('statement is iterable', function () { - generateTestDocuments(); + $this->generateTestDocuments(); $this->statement->execute(); $count = 0; @@ -122,18 +121,3 @@ expect($statement->getWritesExecuted())->toBe(10); }); - -// Helpers -function generateTestDocuments(): void -{ - $query = 'FOR i IN 1..10 - INSERT { - _key: CONCAT("test", i), - name: "test", - foobar: true - } INTO ' . test()->collection . ' OPTIONS { ignoreErrors: true }'; - - $statement = test()->arangoClient->prepare($query); - - $statement->execute(); -} diff --git a/tests/SupportsTransactionsTest.php b/tests/StreamTransactionsTest.php similarity index 97% rename from tests/SupportsTransactionsTest.php rename to tests/StreamTransactionsTest.php index 809cde1..f225b20 100644 --- a/tests/SupportsTransactionsTest.php +++ b/tests/StreamTransactionsTest.php @@ -5,9 +5,6 @@ use ArangoClient\Transactions\TransactionManager; uses(Tests\TestCase::class); -beforeEach(function () { -}); - test('transactions', function () { $transactionManager = $this->arangoClient->transactions(); @@ -25,7 +22,8 @@ test('begin', function () { $transactionId = $this->arangoClient->begin(); $runningTransactions = $this->arangoClient->admin()->getRunningTransactions(); - expect($runningTransactions[0]->id)->toBe($transactionId); + $latestTransaction = end($runningTransactions); + expect($latestTransaction->id)->toBe($transactionId); $this->arangoClient->abort(); }); diff --git a/tests/TestCase.php b/tests/TestCase.php index 4ae930a..57be3d3 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -22,6 +22,24 @@ abstract class TestCase extends PhpUnitTestCase protected string $testDatabaseName = 'arangodb_php_client__test'; + protected string $accessDatabase = 'arangodb_php_client_access__test'; + + protected array $analyzer = [ + 'name' => 'testAnalyzerBasics', + 'type' => 'identity', + ]; + + protected string $collection = 'users'; + + protected \Traversable $statement; + + protected string $userName = 'kimiko'; + + protected array $view = [ + 'name' => 'testViewBasics', + 'type' => 'arangosearch', + ]; + protected function setUp(): void { $this->arangoClient = new ArangoClient([ @@ -44,7 +62,33 @@ protected function createTestDatabase() } } - protected function skipTestOnArangoVersions(string $version, string $operator = '<') + public function setUpAccessTest() + { + if (!$this->schemaManager->hasDatabase($this->accessDatabase)) { + $this->schemaManager->createDatabase($this->accessDatabase); + } + } + + public function tearDownAccessTest() + { + $this->schemaManager->deleteDatabase($this->accessDatabase); + } + + public function generateTestDocuments(): void + { + $query = 'FOR i IN 1..10 + INSERT { + _key: CONCAT("test", i), + name: "test", + foobar: true + } INTO ' . $this->collection . ' OPTIONS { ignoreErrors: true }'; + + $statement = $this->arangoClient->prepare($query); + + $statement->execute(); + } + + public function skipTestOnArangoVersions(string $version, string $operator = '<') { if (version_compare(getenv('ARANGODB_VERSION'), $version, $operator)) { $this->markTestSkipped('This test does not support ArangoDB versions before ' . $version);