summaryrefslogtreecommitdiff
path: root/tests/Unit
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-08-20 20:58:51 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-08-20 21:07:57 +0200
commitd6c8f1a61475fefa9594141aaf12a28d220bdaf8 (patch)
tree905051fdb1307f947c3a3a7be240609f8bc00e59 /tests/Unit
parentbf6efe532c8f2de84e95b090911280a9b1b61ce8 (diff)
parent2f41b9e4418def9b69cf237312bc592364585025 (diff)
Merge branch 'master' to 'rebuild-database'
Diffstat (limited to 'tests/Unit')
-rw-r--r--tests/Unit/Database/DatabaseServiceProviderTest.php80
-rw-r--r--tests/Unit/Database/DbTest.php125
-rw-r--r--tests/Unit/Database/Migration/MigrateTest.php160
-rw-r--r--tests/Unit/Database/Migration/MigrationServiceProviderTest.php55
-rw-r--r--tests/Unit/Database/Migration/MigrationTest.php24
-rw-r--r--tests/Unit/Database/Migration/Stub/2001_04_11_123456_create_lorem_ipsum_table.php27
-rw-r--r--tests/Unit/Database/Migration/Stub/2017_12_24_053300_another_stuff.php22
-rw-r--r--tests/Unit/Database/Migration/Stub/2022_12_22_221222_add_some_feature.php22
8 files changed, 418 insertions, 97 deletions
diff --git a/tests/Unit/Database/DatabaseServiceProviderTest.php b/tests/Unit/Database/DatabaseServiceProviderTest.php
index 61848c35..8f7898cd 100644
--- a/tests/Unit/Database/DatabaseServiceProviderTest.php
+++ b/tests/Unit/Database/DatabaseServiceProviderTest.php
@@ -6,32 +6,86 @@ use Engelsystem\Config\Config;
use Engelsystem\Database\DatabaseServiceProvider;
use Engelsystem\Test\Unit\ServiceProviderTest;
use Exception;
-use PHPUnit_Framework_MockObject_MockObject;
+use Illuminate\Database\Capsule\Manager as CapsuleManager;
+use Illuminate\Database\Connection;
+use PDOException;
+use PHPUnit_Framework_MockObject_MockObject as MockObject;
class DatabaseServiceProviderTest extends ServiceProviderTest
{
/**
* @covers \Engelsystem\Database\DatabaseServiceProvider::register()
- * @covers \Engelsystem\Database\DatabaseServiceProvider::exitOnError()
*/
public function testRegister()
{
- /** @var PHPUnit_Framework_MockObject_MockObject|Config $config */
- $config = $this->getMockBuilder(Config::class)
- ->getMock();
+ list($app, $dbManager) = $this->prepare(['driver' => 'sqlite', 'database' => ':memory:']);
- $app = $this->getApp(['get']);
+ $this->setExpects($app, 'instance', ['db', $dbManager]);
+
+ $serviceProvider = new DatabaseServiceProvider($app);
+ $serviceProvider->register();
+ }
+
+ /**
+ * @covers \Engelsystem\Database\DatabaseServiceProvider::register()
+ * @covers \Engelsystem\Database\DatabaseServiceProvider::exitOnError()
+ */
+ public function testRegisterError()
+ {
+ list($app) = $this->prepare([
+ 'host' => 'localhost',
+ 'database' => 'database',
+ 'username' => 'user',
+ 'password' => 'password',
+ ], true);
- $this->setExpects($app, 'get', ['config'], $config);
- $this->setExpects($config, 'get', ['database'], [
- 'host' => 'localhost',
- 'db' => 'database',
- 'user' => 'user',
- 'pw' => 'password',
- ], $this->atLeastOnce());
$this->expectException(Exception::class);
$serviceProvider = new DatabaseServiceProvider($app);
$serviceProvider->register();
}
+
+ /**
+ * Prepare some mocks
+ *
+ * @param array $dbConfigData
+ * @param bool $getPdoThrowException
+ * @return array
+ */
+ protected function prepare($dbConfigData, $getPdoThrowException = false)
+ {
+ /** @var MockObject|Config $config */
+ $config = $this->getMockBuilder(Config::class)
+ ->getMock();
+ /** @var MockObject|CapsuleManager $config */
+ $dbManager = $this->getMockBuilder(CapsuleManager::class)
+ ->getMock();
+ /** @var MockObject|Connection $connection */
+ $connection = $this->getMockBuilder(Connection::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());
+
+ $this->setExpects($dbManager, 'setAsGlobal');
+ $this->setExpects($dbManager, 'bootEloquent');
+
+ $this->setExpects($connection, 'useDefaultSchemaGrammar');
+ $connection->expects($this->once())
+ ->method('getPdo')
+ ->willReturnCallback(function () use ($getPdoThrowException) {
+ if ($getPdoThrowException) {
+ throw new PDOException();
+ }
+
+ return '';
+ });
+ $this->setExpects($dbManager, 'getConnection', [], $connection, $this->atLeastOnce());
+
+ return [$app, $dbManager];
+ }
}
diff --git a/tests/Unit/Database/DbTest.php b/tests/Unit/Database/DbTest.php
index 63607cad..ca6ac52c 100644
--- a/tests/Unit/Database/DbTest.php
+++ b/tests/Unit/Database/DbTest.php
@@ -3,45 +3,45 @@
namespace Engelsystem\Test\Unit\Database;
use Engelsystem\Database\Db;
+use Illuminate\Database\Capsule\Manager as CapsuleManager;
+use Illuminate\Database\Connection as DatabaseConnection;
use PDO;
-use PDOStatement;
use PHPUnit\Framework\TestCase;
-use ReflectionObject;
-use Throwable;
+use PHPUnit_Framework_MockObject_MockObject as MockObject;
class DbTest extends TestCase
{
/**
- * @covers \Engelsystem\Database\Db::connect()
+ * @covers \Engelsystem\Database\Db::setDbManager()
+ * @covers \Engelsystem\Database\Db::connection()
*/
- public function testConnect()
+ public function testSetDbManager()
{
- $result = Db::connect('mysql:host=localhost;dbname=someTestDatabaseThatDoesNotExist;charset=utf8');
- $this->assertFalse($result);
-
- $result = Db::connect('sqlite::memory:');
- $this->assertTrue($result);
- }
-
- /**
- * @covers \Engelsystem\Database\Db::query()
- */
- public function testQuery()
- {
- $stm = Db::query('SELECT * FROM test_data');
- $this->assertEquals('00000', $stm->errorCode());
-
- $stm = Db::query('SELECT * FROM test_data WHERE id = ?', [4]);
- $this->assertEquals('00000', $stm->errorCode());
- }
-
- /**
- * @covers \Engelsystem\Database\Db::unprepared()
- */
- public function testUnprepared()
- {
- $return = Db::unprepared('SELECT * FROM test_data WHERE id = 3');
- $this->assertTrue($return);
+ /** @var MockObject|Pdo $pdo */
+ $pdo = $this->getMockBuilder(Pdo::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ /** @var MockObject|CapsuleManager $dbManager */
+ $dbManager = $this->getMockBuilder(CapsuleManager::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ /** @var MockObject|DatabaseConnection $dbManager */
+ $databaseConnection = $this->getMockBuilder(DatabaseConnection::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $dbManager
+ ->expects($this->atLeastOnce())
+ ->method('getConnection')
+ ->willReturn($databaseConnection);
+ $databaseConnection
+ ->expects($this->atLeastOnce())
+ ->method('getPdo')
+ ->willReturn($pdo);
+
+ Db::setDbManager($dbManager);
+ $this->assertEquals($pdo, Db::getPdo());
+ $this->assertEquals($databaseConnection, Db::connection());
}
/**
@@ -77,11 +77,8 @@ class DbTest extends TestCase
*/
public function testInsert()
{
- $count = Db::insert("INSERT INTO test_data (id, data) VALUES (5, 'Some random text'), (6, 'another text')");
- $this->assertEquals(2, $count);
-
- $count = Db::insert('INSERT INTO test_data(id, data) VALUES (:id, :alias)', ['id' => 7, 'alias' => 'Blafoo']);
- $this->assertEquals(1, $count);
+ $result = Db::insert("INSERT INTO test_data (id, data) VALUES (5, 'Some random text'), (6, 'another text')");
+ $this->assertTrue($result);
}
/**
@@ -109,42 +106,6 @@ class DbTest extends TestCase
}
/**
- * @covers \Engelsystem\Database\Db::statement()
- */
- public function testStatement()
- {
- $return = Db::statement('SELECT * FROM test_data WHERE id = 3');
- $this->assertTrue($return);
-
- $return = Db::statement('SELECT * FROM test_data WHERE id = ?', [2]);
- $this->assertTrue($return);
- }
-
- /**
- * @covers \Engelsystem\Database\Db::getError()
- */
- public function testGetError()
- {
- try {
- Db::statement('foo');
- } catch (Throwable $e) {
- }
-
- $error = Db::getError();
- $this->assertTrue(is_array($error));
- $this->assertEquals('near "foo": syntax error', $error[2]);
-
- $db = new Db();
- $refObject = new ReflectionObject($db);
- $refProperty = $refObject->getProperty('stm');
- $refProperty->setAccessible(true);
- $refProperty->setValue(null, null);
-
- $error = Db::getError();
- $this->assertEquals([-1, null, null], $error);
- }
-
- /**
* @covers \Engelsystem\Database\Db::getPdo()
*/
public function testGetPdo()
@@ -154,29 +115,25 @@ class DbTest extends TestCase
}
/**
- * @covers \Engelsystem\Database\Db::getStm()
- */
- public function testGetStm()
- {
- $stm = Db::getStm();
- $this->assertInstanceOf(PDOStatement::class, $stm);
- }
-
- /**
* Setup in memory database
*/
protected function setUp()
{
- Db::connect('sqlite::memory:');
+ $dbManager = new CapsuleManager();
+ $dbManager->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
+ $dbManager->setAsGlobal();
+ $dbManager->bootEloquent();
+
+ Db::setDbManager($dbManager);
Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- Db::query(
+ Db::connection()->statement(
'
CREATE TABLE test_data(
id INT PRIMARY KEY NOT NULL,
data TEXT NOT NULL
);
');
- Db::query('CREATE UNIQUE INDEX test_data_id_uindex ON test_data (id);');
+ Db::connection()->statement('CREATE UNIQUE INDEX test_data_id_uindex ON test_data (id);');
Db::insert("
INSERT INTO test_data (id, data)
VALUES
diff --git a/tests/Unit/Database/Migration/MigrateTest.php b/tests/Unit/Database/Migration/MigrateTest.php
new file mode 100644
index 00000000..c88ad777
--- /dev/null
+++ b/tests/Unit/Database/Migration/MigrateTest.php
@@ -0,0 +1,160 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Database;
+
+use Engelsystem\Application;
+use Engelsystem\Database\Migration\Migrate;
+use Illuminate\Database\Capsule\Manager as CapsuleManager;
+use Illuminate\Database\Schema\Builder as SchemaBuilder;
+use Illuminate\Support\Collection;
+use Illuminate\Support\Str;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+
+class MigrateTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Database\Migration\Migrate::__construct
+ * @covers \Engelsystem\Database\Migration\Migrate::run
+ * @covers \Engelsystem\Database\Migration\Migrate::getMigrations
+ * @covers \Engelsystem\Database\Migration\Migrate::setOutput
+ */
+ public function testRun()
+ {
+ /** @var MockObject|Application $app */
+ $app = $this->getMockBuilder(Application::class)
+ ->setMethods(['instance'])
+ ->getMock();
+ /** @var MockObject|SchemaBuilder $builder */
+ $builder = $this->getMockBuilder(SchemaBuilder::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ /** @var MockObject|Migrate $migration */
+ $migration = $this->getMockBuilder(Migrate::class)
+ ->setConstructorArgs([$builder, $app])
+ ->setMethods(['initMigration', 'getMigrationFiles', 'getMigrated', 'migrate', 'setMigrated'])
+ ->getMock();
+
+ $migration->expects($this->atLeastOnce())
+ ->method('initMigration');
+ $migration->expects($this->atLeastOnce())
+ ->method('getMigrationFiles')
+ ->willReturn([
+ 'foo/1234_01_23_123456_init_foo.php',
+ 'foo/9876_03_22_210000_random_hack.php',
+ 'foo/4567_11_01_000000_do_stuff.php',
+ 'foo/9999_99_99_999999_another_foo.php',
+ ]);
+ $migration->expects($this->atLeastOnce())
+ ->method('getMigrated')
+ ->willReturn(new Collection([
+ ['id' => 1, 'migration' => '1234_01_23_123456_init_foo'],
+ ['id' => 2, 'migration' => '4567_11_01_000000_do_stuff'],
+ ]));
+ $migration->expects($this->atLeastOnce())
+ ->method('migrate')
+ ->withConsecutive(
+ ['foo/9876_03_22_210000_random_hack.php', '9876_03_22_210000_random_hack', Migrate::UP],
+ ['foo/9999_99_99_999999_another_foo.php', '9999_99_99_999999_another_foo', Migrate::UP],
+ ['foo/9876_03_22_210000_random_hack.php', '9876_03_22_210000_random_hack', Migrate::UP],
+ ['foo/4567_11_01_000000_do_stuff.php', '4567_11_01_000000_do_stuff', Migrate::DOWN]
+ );
+ $migration->expects($this->atLeastOnce())
+ ->method('setMigrated')
+ ->withConsecutive(
+ ['9876_03_22_210000_random_hack', Migrate::UP],
+ ['9999_99_99_999999_another_foo', Migrate::UP],
+ ['9876_03_22_210000_random_hack', Migrate::UP],
+ ['4567_11_01_000000_do_stuff', Migrate::DOWN]
+ );
+
+ $messages = [];
+ $migration->setOutput(function ($text) use (&$messages) {
+ $messages[] = $text;
+ });
+
+ $migration->run('foo', Migrate::UP);
+
+ $this->assertCount(4, $messages);
+ foreach (
+ [
+ 'init_foo' => 'skipping',
+ 'do_stuff' => 'skipping',
+ 'random_hack' => 'migrating',
+ 'another_foo' => 'migrating',
+ ] as $value => $type
+ ) {
+ $contains = false;
+ foreach ($messages as $message) {
+ if (!Str::contains(strtolower($message), $type) || !Str::contains(strtolower($message), $value)) {
+ continue;
+ }
+
+ $contains = true;
+ break;
+ }
+
+ $this->assertTrue($contains, sprintf('Missing message "%s: %s"', $type, $value));
+ }
+
+ $messages = [];
+ $migration->run('foo', Migrate::UP, true);
+ $this->assertCount(3, $messages);
+
+ $migration->run('foo', Migrate::DOWN, true);
+ }
+
+ /**
+ * @covers \Engelsystem\Database\Migration\Migrate::getMigrated
+ * @covers \Engelsystem\Database\Migration\Migrate::migrate
+ * @covers \Engelsystem\Database\Migration\Migrate::setMigrated
+ * @covers \Engelsystem\Database\Migration\Migrate::getMigrationFiles
+ * @covers \Engelsystem\Database\Migration\Migrate::initMigration
+ * @covers \Engelsystem\Database\Migration\Migrate::getTableQuery
+ */
+ public function testRunIntegration()
+ {
+ $app = new Application();
+ $dbManager = new CapsuleManager($app);
+ $dbManager->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
+ $dbManager->bootEloquent();
+ $db = $dbManager->getConnection();
+ $db->useDefaultSchemaGrammar();
+ $scheme = $db->getSchemaBuilder();
+
+ $app->instance('scheme', $scheme);
+ $app->bind(SchemaBuilder::class, 'scheme');
+
+ $migration = new Migrate($scheme, $app);
+
+ $messages = [];
+ $migration->setOutput(function ($msg) use (&$messages) {
+ $messages[] = $msg;
+ });
+
+ $migration->run(__DIR__ . '/Stub', Migrate::UP);
+
+ $this->assertTrue($scheme->hasTable('migrations'));
+
+ $migrations = $db->table('migrations')->get();
+ $this->assertCount(3, $migrations);
+
+ $this->assertTrue($migrations->contains('migration', '2001_04_11_123456_create_lorem_ipsum_table'));
+ $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'));
+
+ $migration->run(__DIR__ . '/Stub', Migrate::DOWN, true);
+
+ $migrations = $db->table('migrations')->get();
+ $this->assertCount(2, $migrations);
+
+ $migration->run(__DIR__ . '/Stub', Migrate::DOWN);
+
+ $migrations = $db->table('migrations')->get();
+ $this->assertCount(0, $migrations);
+
+ $this->assertFalse($scheme->hasTable('lorem_ipsum'));
+ }
+}
diff --git a/tests/Unit/Database/Migration/MigrationServiceProviderTest.php b/tests/Unit/Database/Migration/MigrationServiceProviderTest.php
new file mode 100644
index 00000000..a99cdebe
--- /dev/null
+++ b/tests/Unit/Database/Migration/MigrationServiceProviderTest.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Database\Migration;
+
+use Engelsystem\Database\Db;
+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;
+
+class MigrationServiceProviderTest extends ServiceProviderTest
+{
+ /**
+ * @covers \Engelsystem\Database\Migration\MigrationServiceProvider::register()
+ */
+ public function testRegister()
+ {
+ /** @var MockObject|Migrate $migration */
+ $migration = $this->getMockBuilder(Migrate::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ /** @var MockObject|CapsuleManager $dbManager */
+ $dbManager = $this->getMockBuilder(CapsuleManager::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ /** @var MockObject|Connection $dbConnection */
+ $dbConnection = $this->getMockBuilder(Connection::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ /** @var MockObject|SchemaBuilder $schemaBuilder */
+ $schemaBuilder = $this->getMockBuilder(SchemaBuilder::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $app = $this->getApp(['make', 'instance', 'bind']);
+
+ $app->expects($this->atLeastOnce())
+ ->method('instance')
+ ->withConsecutive(['db.scheme'], ['db.migration'])
+ ->willReturnOnConsecutiveCalls($schemaBuilder, $migration);
+
+ $this->setExpects($app, 'bind', [SchemaBuilder::class, 'db.scheme']);
+ $this->setExpects($app, 'make', [Migrate::class], $migration);
+
+ $this->setExpects($dbConnection, 'getSchemaBuilder', null, $schemaBuilder);
+ $this->setExpects($dbManager, 'getConnection', null, $dbConnection);
+ Db::setDbManager($dbManager);
+
+ $serviceProvider = new MigrationServiceProvider($app);
+ $serviceProvider->register();
+ }
+}
diff --git a/tests/Unit/Database/Migration/MigrationTest.php b/tests/Unit/Database/Migration/MigrationTest.php
new file mode 100644
index 00000000..43bded09
--- /dev/null
+++ b/tests/Unit/Database/Migration/MigrationTest.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Database;
+
+use AnotherStuff;
+use Illuminate\Database\Schema\Builder as SchemaBuilder;
+use PHPUnit\Framework\MockObject\MockBuilder;
+use PHPUnit\Framework\TestCase;
+
+class MigrationTest extends TestCase
+{
+ public function testConstructor()
+ {
+ require_once __DIR__ . '/Stub/2017_12_24_053300_another_stuff.php';
+
+ /** @var MockBuilder|SchemaBuilder $schemaBuilder */
+ $schemaBuilder = $this->getMockBuilder(SchemaBuilder::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $instance = new AnotherStuff($schemaBuilder);
+ $this->assertAttributeEquals($schemaBuilder, 'schema', $instance);
+ }
+}
diff --git a/tests/Unit/Database/Migration/Stub/2001_04_11_123456_create_lorem_ipsum_table.php b/tests/Unit/Database/Migration/Stub/2001_04_11_123456_create_lorem_ipsum_table.php
new file mode 100644
index 00000000..0cc89e07
--- /dev/null
+++ b/tests/Unit/Database/Migration/Stub/2001_04_11_123456_create_lorem_ipsum_table.php
@@ -0,0 +1,27 @@
+<?php
+
+use Engelsystem\Database\Migration\Migration;
+use Illuminate\Database\Schema\Blueprint;
+
+class CreateLoremIpsumTable extends Migration
+{
+ /**
+ * Run the migration
+ */
+ public function up()
+ {
+ $this->schema->create('lorem_ipsum', function (Blueprint $table) {
+ $table->increments('id');
+ $table->string('name')->unique();
+ $table->string('email');
+ });
+ }
+
+ /**
+ * Reverse the migration
+ */
+ public function down()
+ {
+ $this->schema->dropIfExists('lorem_ipsum');
+ }
+}
diff --git a/tests/Unit/Database/Migration/Stub/2017_12_24_053300_another_stuff.php b/tests/Unit/Database/Migration/Stub/2017_12_24_053300_another_stuff.php
new file mode 100644
index 00000000..d4d7e5f8
--- /dev/null
+++ b/tests/Unit/Database/Migration/Stub/2017_12_24_053300_another_stuff.php
@@ -0,0 +1,22 @@
+<?php
+
+use Engelsystem\Database\Migration\Migration;
+
+class AnotherStuff extends Migration
+{
+ /**
+ * Run the migration
+ */
+ public function up()
+ {
+ // nope
+ }
+
+ /**
+ * Reverse the migration
+ */
+ public function down()
+ {
+ // nope
+ }
+}
diff --git a/tests/Unit/Database/Migration/Stub/2022_12_22_221222_add_some_feature.php b/tests/Unit/Database/Migration/Stub/2022_12_22_221222_add_some_feature.php
new file mode 100644
index 00000000..cf2762de
--- /dev/null
+++ b/tests/Unit/Database/Migration/Stub/2022_12_22_221222_add_some_feature.php
@@ -0,0 +1,22 @@
+<?php
+
+use Engelsystem\Database\Migration\Migration;
+
+class AddSomeFeature extends Migration
+{
+ /**
+ * Run the migration
+ */
+ public function up()
+ {
+ // nope
+ }
+
+ /**
+ * Reverse the migration
+ */
+ public function down()
+ {
+ // nope
+ }
+}