Skip to content

Commit fad6bc7

Browse files
tests: create default test database
1 parent c9a2a98 commit fad6bc7

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/DatabaseClient.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ public function listDatabases(): array
4141
return (array) $this->connector->request('get', '/_api/database');
4242
}
4343

44+
/**
45+
* @param $database
46+
* @return bool
47+
* @throws Exceptions\ArangoDbException
48+
* @throws GuzzleException
49+
*/
50+
public function exists($database): bool
51+
{
52+
$databaseList = $this->listDatabases();
53+
return in_array($database, $databaseList);
54+
}
55+
4456
/**
4557
* @return array<mixed>
4658
*

tests/DatabaseClientTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testListMyDatabases()
5252

5353
public function testCreateAndDeleteDatabase()
5454
{
55-
$database = 'test__arangodb_php_client_database';
55+
$database = 'arangodb_php_client_database__test';
5656
$existingDatabases = $this->client->listDatabases();
5757

5858
if (! in_array($database, $existingDatabases)) {
@@ -64,6 +64,14 @@ public function testCreateAndDeleteDatabase()
6464
$this->assertTrue($result);
6565
$existingDatabases = $this->client->listDatabases();
6666
$this->assertNotContains($database, $existingDatabases);
67+
}
68+
69+
public function testDatabaseExists()
70+
{
71+
$check = $this->databaseClient->exists('someNoneExistingDatabase');
72+
$this->assertFalse($check);
6773

74+
$check = $this->databaseClient->exists($this->testDatabaseName);
75+
$this->assertTrue($check);
6876
}
6977
}

tests/TestCase.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,34 @@
55
namespace Tests;
66

77
use ArangoClient\Connector;
8+
use ArangoClient\DatabaseClient;
89
use PHPUnit\Framework\TestCase as PhpUnitTestCase;
910

1011
abstract class TestCase extends PhpUnitTestCase
1112
{
12-
protected $connector;
13+
protected Connector $connector;
14+
15+
protected DatabaseClient $databaseClient;
16+
17+
protected string $testDatabaseName = 'arangodb_php_client_database__test';
1318

1419
protected function setUp(): void
1520
{
1621
$this->connector = new Connector();
22+
23+
$this->databaseClient = new DatabaseClient($this->connector);
24+
25+
$this->createTestDatabase();
26+
}
27+
28+
/**
29+
* @throws \ArangoClient\Exceptions\ArangoDbException
30+
* @throws \GuzzleHttp\Exception\GuzzleException
31+
*/
32+
protected function createTestDatabase()
33+
{
34+
if(! $this->databaseClient->exists($this->testDatabaseName)) {
35+
$this->databaseClient->create($this->testDatabaseName);
36+
}
1737
}
1838
}

0 commit comments

Comments
 (0)