1
0
Fork 0
mirror of https://github.com/Oreolek/yii2-nested-sets.git synced 2024-05-16 15:58:17 +03:00

Added unit tests for mysql

This commit is contained in:
Alexander Kochetov 2015-01-15 04:38:54 +03:00
parent 48d0858138
commit 86a2657c0d
7 changed files with 150 additions and 17 deletions

View file

@ -7,6 +7,8 @@
namespace tests; namespace tests;
use Yii;
/** /**
* DatabaseTestCase * DatabaseTestCase
*/ */
@ -17,7 +19,7 @@ abstract class DatabaseTestCase extends \PHPUnit_Extensions_Database_TestCase
*/ */
public function getConnection() public function getConnection()
{ {
return $this->createDefaultDBConnection(\Yii::$app->db->pdo); return $this->createDefaultDBConnection(Yii::$app->getDb()->pdo);
} }
/** /**

View file

@ -0,0 +1,39 @@
<?php
/**
* @link https://github.com/creocoder/yii2-nested-sets
* @copyright Copyright (c) 2015 Alexander Kochetov
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace tests;
use Yii;
use yii\db\Connection;
/**
* MysqlNestedSetsBehaviorTest
*/
class MysqlNestedSetsBehaviorTest extends NestedSetsBehaviorTest
{
/**
* @inheritdoc
*/
public static function setUpBeforeClass()
{
Yii::$app->set('db', [
'class' => Connection::className(),
'dsn' => 'mysql:host=localhost;dbname=yii2_nested_sets_test',
'username' => 'root',
'password' => '',
]);
Yii::$app->getDb()->open();
$lines = explode(';', file_get_contents(__DIR__ . '/migrations/mysql.sql'));
foreach ($lines as $line) {
if (trim($line) !== '') {
Yii::$app->getDb()->pdo->exec($line);
}
}
}
}

View file

@ -0,0 +1,39 @@
<?php
/**
* @link https://github.com/creocoder/yii2-nested-sets
* @copyright Copyright (c) 2015 Alexander Kochetov
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace tests;
use Yii;
use yii\db\Connection;
/**
* MysqlNestedSetsQueryBehaviorTest
*/
class MysqlNestedSetsQueryBehaviorTest extends NestedSetsBehaviorTest
{
/**
* @inheritdoc
*/
public static function setUpBeforeClass()
{
Yii::$app->set('db', [
'class' => Connection::className(),
'dsn' => 'mysql:host=localhost;dbname=yii2_nested_sets_test',
'username' => 'root',
'password' => '',
]);
Yii::$app->getDb()->open();
$lines = explode(';', file_get_contents(__DIR__ . '/migrations/mysql.sql'));
foreach ($lines as $line) {
if (trim($line) !== '') {
Yii::$app->getDb()->pdo->exec($line);
}
}
}
}

View file

@ -9,6 +9,8 @@ namespace tests;
use tests\models\MultipleRootsTree; use tests\models\MultipleRootsTree;
use tests\models\Tree; use tests\models\Tree;
use Yii;
use yii\db\Connection;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
/** /**
@ -635,4 +637,24 @@ class NestedSetsBehaviorTest extends DatabaseTestCase
$this->assertTrue(Tree::findOne(4)->isLeaf()); $this->assertTrue(Tree::findOne(4)->isLeaf());
$this->assertFalse(Tree::findOne(1)->isLeaf()); $this->assertFalse(Tree::findOne(1)->isLeaf());
} }
/**
* @inheritdoc
*/
public static function setUpBeforeClass()
{
Yii::$app->set('db', [
'class' => Connection::className(),
'dsn' => 'sqlite::memory:',
]);
Yii::$app->getDb()->open();
$lines = explode(';', file_get_contents(__DIR__ . '/migrations/sqlite.sql'));
foreach ($lines as $line) {
if (trim($line) !== '') {
Yii::$app->getDb()->pdo->exec($line);
}
}
}
} }

View file

@ -9,6 +9,8 @@ namespace tests;
use tests\models\MultipleRootsTree; use tests\models\MultipleRootsTree;
use tests\models\Tree; use tests\models\Tree;
use Yii;
use yii\db\Connection;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
/** /**
@ -41,4 +43,24 @@ class NestedSetsQueryBehaviorTest extends DatabaseTestCase
ArrayHelper::toArray(MultipleRootsTree::find()->leaves()->all()) ArrayHelper::toArray(MultipleRootsTree::find()->leaves()->all())
); );
} }
/**
* @inheritdoc
*/
public static function setUpBeforeClass()
{
Yii::$app->set('db', [
'class' => Connection::className(),
'dsn' => 'sqlite::memory:',
]);
Yii::$app->getDb()->open();
$lines = explode(';', file_get_contents(__DIR__ . '/migrations/sqlite.sql'));
foreach ($lines as $line) {
if (trim($line) !== '') {
Yii::$app->getDb()->pdo->exec($line);
}
}
}
} }

View file

@ -10,20 +10,5 @@ Yii::setAlias('@tests', __DIR__);
new \yii\console\Application([ new \yii\console\Application([
'id' => 'unit', 'id' => 'unit',
'basePath' => __DIR__ . '/..', 'basePath' => __DIR__,
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'sqlite::memory:',
],
],
]); ]);
Yii::$app->db->open();
$lines = explode(';', file_get_contents(__DIR__ . '/migrations/sqlite.sql'));
foreach ($lines as $line) {
if (trim($line) !== '') {
Yii::$app->db->pdo->exec($line);
}
}

View file

@ -0,0 +1,24 @@
/**
* MySQL
*/
DROP TABLE IF EXISTS `tree`;
CREATE TABLE `tree` (
`id` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`lft` INT(11) NOT NULL,
`rgt` INT(11) NOT NULL,
`depth` INT(11) NOT NULL,
`name` VARCHAR(255) NOT NULL
);
DROP TABLE IF EXISTS `multiple_roots_tree`;
CREATE TABLE `multiple_roots_tree` (
`id` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`tree` INT(11),
`lft` INT(11) NOT NULL,
`rgt` INT(11) NOT NULL,
`depth` INT(11) NOT NULL,
`name` VARCHAR(255) NOT NULL
);