summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-09-16 00:58:25 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-09-16 14:53:33 +0200
commitedeab5e75ffa02b075c151ca03ea1038f61e4396 (patch)
tree66dd92ed7bc0ac47683fc84769613fabfb0c0080 /tests
parentd36de2d26f5af76d5d4f34f8620694c6d0368983 (diff)
Added Database class as a replacement for Db, fixed naming
Diffstat (limited to 'tests')
-rw-r--r--tests/Unit/Database/DatabaseServiceProviderTest.php48
-rw-r--r--tests/Unit/Database/DatabaseTest.php146
-rw-r--r--tests/Unit/Database/Migration/MigrateTest.php14
-rw-r--r--tests/Unit/Database/Migration/MigrationServiceProviderTest.php17
4 files changed, 205 insertions, 20 deletions
diff --git a/tests/Unit/Database/DatabaseServiceProviderTest.php b/tests/Unit/Database/DatabaseServiceProviderTest.php
index 8f7898cd..0f259036 100644
--- a/tests/Unit/Database/DatabaseServiceProviderTest.php
+++ b/tests/Unit/Database/DatabaseServiceProviderTest.php
@@ -3,7 +3,9 @@
namespace Engelsystem\Test\Unit\Database;
use Engelsystem\Config\Config;
+use Engelsystem\Database\Database;
use Engelsystem\Database\DatabaseServiceProvider;
+use Engelsystem\Database\Db;
use Engelsystem\Test\Unit\ServiceProviderTest;
use Exception;
use Illuminate\Database\Capsule\Manager as CapsuleManager;
@@ -18,9 +20,29 @@ class DatabaseServiceProviderTest extends ServiceProviderTest
*/
public function testRegister()
{
- list($app, $dbManager) = $this->prepare(['driver' => 'sqlite', 'database' => ':memory:']);
+ /** @var Application|MockObject $app */
+ /** @var CapsuleManager|MockObject $dbManager */
+ /** @var PDO|MockObject $pdo */
+ /** @var Database|MockObject $database */
+ /** @var Connection|MockObject $connection */
+ list($app, $dbManager, $pdo, $database, $connection) = $this->prepare(
+ [
+ 'driver' => 'sqlite',
+ 'database' => ':memory:'
+ ]
+ );
- $this->setExpects($app, 'instance', ['db', $dbManager]);
+ $app->expects($this->exactly(7))
+ ->method('instance')
+ ->withConsecutive(
+ [CapsuleManager::class, $dbManager],
+ [Db::class, $dbManager],
+ [Connection::class, $connection],
+ [Database::class, $database],
+ ['db', $database],
+ ['db.pdo', $pdo],
+ ['db.connection', $connection]
+ );
$serviceProvider = new DatabaseServiceProvider($app);
$serviceProvider->register();
@@ -64,13 +86,31 @@ class DatabaseServiceProviderTest extends ServiceProviderTest
$connection = $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->getMock();
+ /** @var PDO|MockObject $pdo */
+ $pdo = $this->getMockBuilder(PDO::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ /** @var Database|MockObject $database */
+ $database = $this->getMockBuilder(Database::class)
+ ->disableOriginalConstructor()
+ ->getMock();
$app = $this->getApp(['get', 'make', 'instance']);
$this->setExpects($app, 'get', ['config'], $config);
- $this->setExpects($app, 'make', [CapsuleManager::class], $dbManager);
$this->setExpects($config, 'get', ['database'], $dbConfigData, $this->atLeastOnce());
+ $app->expects($this->atLeastOnce())
+ ->method('make')
+ ->withConsecutive(
+ [CapsuleManager::class],
+ [Database::class]
+ )
+ ->willReturn(
+ $dbManager,
+ $database
+ );
+
$this->setExpects($dbManager, 'setAsGlobal');
$this->setExpects($dbManager, 'bootEloquent');
@@ -86,6 +126,6 @@ class DatabaseServiceProviderTest extends ServiceProviderTest
});
$this->setExpects($dbManager, 'getConnection', [], $connection, $this->atLeastOnce());
- return [$app, $dbManager];
+ return [$app, $dbManager, $pdo, $database, $connection];
}
}
diff --git a/tests/Unit/Database/DatabaseTest.php b/tests/Unit/Database/DatabaseTest.php
new file mode 100644
index 00000000..91d6a6c7
--- /dev/null
+++ b/tests/Unit/Database/DatabaseTest.php
@@ -0,0 +1,146 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Database;
+
+use Engelsystem\Database\Database;
+use Illuminate\Database\Capsule\Manager as CapsuleManager;
+use Illuminate\Database\Connection as DatabaseConnection;
+use PDO;
+use PHPUnit\Framework\TestCase;
+use PHPUnit_Framework_MockObject_MockObject as MockObject;
+
+class DatabaseTest extends TestCase
+{
+ /** @var DatabaseConnection */
+ protected $connection;
+
+ /**
+ * @covers \Engelsystem\Database\Database::__construct()
+ * @covers \Engelsystem\Database\Database::getPdo()
+ * @covers \Engelsystem\Database\Database::getConnection()
+ */
+ public function testInit()
+ {
+ /** @var Pdo|MockObject $pdo */
+ $pdo = $this->getMockBuilder(Pdo::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ /** @var DatabaseConnection|MockObject $databaseConnection */
+ $databaseConnection = $this->getMockBuilder(DatabaseConnection::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $databaseConnection->expects($this->atLeastOnce())
+ ->method('getPdo')
+ ->willReturn($pdo);
+
+ $db = new Database($databaseConnection);
+
+ $this->assertEquals($databaseConnection, $db->getConnection());
+ $this->assertEquals($pdo, $db->getPdo());
+ $this->assertInstanceOf(PDO::class, $db->getPdo());
+ }
+
+ /**
+ * @covers \Engelsystem\Database\Database::select()
+ */
+ public function testSelect()
+ {
+ $db = new Database($this->connection);
+
+ $return = $db->select('SELECT * FROM test_data');
+ $this->assertTrue(count($return) > 3);
+
+ $return = $db->select('SELECT * FROM test_data WHERE id = ?', [2]);
+ $this->assertCount(1, $return);
+ }
+
+ /**
+ * @covers \Engelsystem\Database\Database::selectOne()
+ */
+ public function testSelectOne()
+ {
+ $db = new Database($this->connection);
+
+ $return = $db->selectOne('SELECT * FROM test_data');
+ $this->assertEquals('Foo', $return->data);
+
+ $return = $db->selectOne('SELECT * FROM test_data WHERE id = -1');
+ $this->assertEmpty($return);
+
+ $return = $db->selectOne('SELECT * FROM test_data WHERE id = ?', [3]);
+ $this->assertTrue(!is_array($return));
+ }
+
+ /**
+ * @covers \Engelsystem\Database\Database::insert()
+ */
+ public function testInsert()
+ {
+ $db = new Database($this->connection);
+
+ $result = $db->insert("INSERT INTO test_data (id, data) VALUES (5, 'Some random text'), (6, 'another text')");
+ $this->assertTrue($result);
+ }
+
+ /**
+ * @covers \Engelsystem\Database\Database::update()
+ */
+ public function testUpdate()
+ {
+ $db = new Database($this->connection);
+
+ $count = $db->update("UPDATE test_data SET data='NOPE' WHERE data LIKE '%Replaceme%'");
+ $this->assertEquals(3, $count);
+
+ $count = $db->update("UPDATE test_data SET data=? WHERE data LIKE '%NOPE%'", ['Some random text!']);
+ $this->assertEquals(3, $count);
+ }
+
+ /**
+ * @covers \Engelsystem\Database\Database::delete()
+ */
+ public function testDelete()
+ {
+ $db = new Database($this->connection);
+
+ $count = $db->delete('DELETE FROM test_data WHERE id=1');
+ $this->assertEquals(1, $count);
+
+ $count = $db->delete('DELETE FROM test_data WHERE data LIKE ?', ['%Replaceme%']);
+ $this->assertEquals(3, $count);
+ }
+
+ /**
+ * Setup in memory database
+ */
+ protected function setUp()
+ {
+ $dbManager = new CapsuleManager();
+ $dbManager->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
+
+ $connection = $dbManager->getConnection();
+ $this->connection = $connection;
+
+ $connection->getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ $connection->statement(
+ '
+ CREATE TABLE test_data(
+ id INT PRIMARY KEY NOT NULL,
+ data TEXT NOT NULL
+ );
+ ');
+ $connection->statement('CREATE UNIQUE INDEX test_data_id_uindex ON test_data (id);');
+ $connection->insert("
+ INSERT INTO test_data (id, data)
+ VALUES
+ (1, 'Foo'),
+ (2, 'Bar'),
+ (3, 'Batz'),
+ (4, 'Lorem ipsum dolor sit'),
+ (10, 'Replaceme ipsum dolor sit amet'),
+ (11, 'Lorem Replaceme dolor sit amet'),
+ (12, 'Lorem ipsum Replaceme sit amet')
+ ;");
+ }
+}
diff --git a/tests/Unit/Database/Migration/MigrateTest.php b/tests/Unit/Database/Migration/MigrateTest.php
index c88ad777..2adbed41 100644
--- a/tests/Unit/Database/Migration/MigrateTest.php
+++ b/tests/Unit/Database/Migration/MigrateTest.php
@@ -120,12 +120,12 @@ class MigrateTest extends TestCase
$dbManager->bootEloquent();
$db = $dbManager->getConnection();
$db->useDefaultSchemaGrammar();
- $scheme = $db->getSchemaBuilder();
+ $schema = $db->getSchemaBuilder();
- $app->instance('scheme', $scheme);
- $app->bind(SchemaBuilder::class, 'scheme');
+ $app->instance('schema', $schema);
+ $app->bind(SchemaBuilder::class, 'schema');
- $migration = new Migrate($scheme, $app);
+ $migration = new Migrate($schema, $app);
$messages = [];
$migration->setOutput(function ($msg) use (&$messages) {
@@ -134,7 +134,7 @@ class MigrateTest extends TestCase
$migration->run(__DIR__ . '/Stub', Migrate::UP);
- $this->assertTrue($scheme->hasTable('migrations'));
+ $this->assertTrue($schema->hasTable('migrations'));
$migrations = $db->table('migrations')->get();
$this->assertCount(3, $migrations);
@@ -143,7 +143,7 @@ class MigrateTest extends TestCase
$this->assertTrue($migrations->contains('migration', '2017_12_24_053300_another_stuff'));
$this->assertTrue($migrations->contains('migration', '2022_12_22_221222_add_some_feature'));
- $this->assertTrue($scheme->hasTable('lorem_ipsum'));
+ $this->assertTrue($schema->hasTable('lorem_ipsum'));
$migration->run(__DIR__ . '/Stub', Migrate::DOWN, true);
@@ -155,6 +155,6 @@ class MigrateTest extends TestCase
$migrations = $db->table('migrations')->get();
$this->assertCount(0, $migrations);
- $this->assertFalse($scheme->hasTable('lorem_ipsum'));
+ $this->assertFalse($schema->hasTable('lorem_ipsum'));
}
}
diff --git a/tests/Unit/Database/Migration/MigrationServiceProviderTest.php b/tests/Unit/Database/Migration/MigrationServiceProviderTest.php
index a99cdebe..593da5c5 100644
--- a/tests/Unit/Database/Migration/MigrationServiceProviderTest.php
+++ b/tests/Unit/Database/Migration/MigrationServiceProviderTest.php
@@ -2,11 +2,10 @@
namespace Engelsystem\Test\Unit\Database\Migration;
-use Engelsystem\Database\Db;
+use Engelsystem\Database\Database;
use Engelsystem\Database\Migration\Migrate;
use Engelsystem\Database\Migration\MigrationServiceProvider;
use Engelsystem\Test\Unit\ServiceProviderTest;
-use Illuminate\Database\Capsule\Manager as CapsuleManager;
use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Builder as SchemaBuilder;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
@@ -22,8 +21,8 @@ class MigrationServiceProviderTest extends ServiceProviderTest
$migration = $this->getMockBuilder(Migrate::class)
->disableOriginalConstructor()
->getMock();
- /** @var MockObject|CapsuleManager $dbManager */
- $dbManager = $this->getMockBuilder(CapsuleManager::class)
+ /** @var Database|MockObject $database */
+ $database = $this->getMockBuilder(Database::class)
->disableOriginalConstructor()
->getMock();
/** @var MockObject|Connection $dbConnection */
@@ -35,19 +34,19 @@ class MigrationServiceProviderTest extends ServiceProviderTest
->disableOriginalConstructor()
->getMock();
- $app = $this->getApp(['make', 'instance', 'bind']);
+ $app = $this->getApp(['make', 'instance', 'bind', 'get']);
$app->expects($this->atLeastOnce())
->method('instance')
- ->withConsecutive(['db.scheme'], ['db.migration'])
+ ->withConsecutive(['db.schema'], ['db.migration'])
->willReturnOnConsecutiveCalls($schemaBuilder, $migration);
- $this->setExpects($app, 'bind', [SchemaBuilder::class, 'db.scheme']);
+ $this->setExpects($app, 'bind', [SchemaBuilder::class, 'db.schema']);
$this->setExpects($app, 'make', [Migrate::class], $migration);
+ $this->setExpects($app, 'get', [Database::class], $database);
$this->setExpects($dbConnection, 'getSchemaBuilder', null, $schemaBuilder);
- $this->setExpects($dbManager, 'getConnection', null, $dbConnection);
- Db::setDbManager($dbManager);
+ $this->setExpects($database, 'getConnection', null, $dbConnection);
$serviceProvider = new MigrationServiceProvider($app);
$serviceProvider->register();