1
0
Fork 0
mirror of https://github.com/Oreolek/yii2-nested-sets.git synced 2024-06-17 07:10:46 +03:00

Base for unit tests added

This commit is contained in:
Alexander Kochetov 2015-01-01 09:10:10 +03:00
parent 427deedd5a
commit 579a497145
10 changed files with 257 additions and 0 deletions

8
phpunit.xml.dist Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite>
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>

View file

@ -0,0 +1,30 @@
<?php
/**
* @link https://github.com/creocoder/yii2-nested-sets-behavior
* @copyright Copyright (c) 2015 Alexander Kochetov
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace tests;
/**
* DatabaseTestCase
*/
abstract class DatabaseTestCase extends \PHPUnit_Extensions_Database_TestCase
{
/**
* @inheritdoc
*/
public function getConnection()
{
return $this->createDefaultDBConnection(\Yii::$app->db->pdo);
}
/**
* @inheritdoc
*/
public function getDataSet()
{
return $this->createFlatXMLDataSet(__DIR__ . '/datasets/_init.xml');
}
}

View file

@ -0,0 +1,59 @@
<?php
/**
* @link https://github.com/creocoder/yii2-nested-sets-behavior
* @copyright Copyright (c) 2015 Alexander Kochetov
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace tests;
use tests\models\Tree;
/**
* NestedSetsBehaviorTest
*/
class NestedSetsBehaviorTest extends DatabaseTestCase
{
/**
* @covers \creocoder\nestedsets\NestedSetsBehavior::makeRoot
* @covers \creocoder\nestedsets\NestedSetsBehavior::beforeInsert
*/
public function testMakeNewRoot()
{
$node = new Tree();
$node->id = 1;
$node->name = 'Item';
$this->assertTrue($node->makeRoot());
$dataSet = $this->getConnection()->createDataSet(['tree']);
$expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/datasets/tree-after-make-new-root.xml');
$this->assertDataSetsEqual($expectedDataSet, $dataSet);
}
/**
* @covers \creocoder\nestedsets\NestedSetsBehavior::makeRoot
* @covers \creocoder\nestedsets\NestedSetsBehavior::beforeInsert
* @expectedException \yii\db\Exception
*/
public function testMakeNewRootExceptionIsRaisedWhenRootIsExists()
{
$dataSet = $this->createFlatXMLDataSet(__DIR__ . '/datasets/tree.xml');
$this->getDatabaseTester()->setDataSet($dataSet);
$this->getDatabaseTester()->onSetUp();
$node = new Tree();
$node->id = 2;
$node->name = 'Item';
$node->makeRoot();
}
/**
* @covers \creocoder\nestedsets\NestedSetsBehavior::beforeInsert
* @expectedException \yii\base\NotSupportedException
*/
public function testExceptionIsRaisedWhenInsertIsCalled()
{
$node = new Tree();
$node->id = 1;
$node->name = 'Item';
$node->insert();
}
}

29
tests/bootstrap.php Normal file
View file

@ -0,0 +1,29 @@
<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
Yii::setAlias('@tests', __DIR__);
new \yii\console\Application([
'id' => 'unit',
'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);
}
}

5
tests/datasets/_init.xml Normal file
View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<tree/>
<multiple_roots_tree/>
</dataset>

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<tree id="1" lft="1" rgt="2" depth="0" name="Item"/>
</dataset>

4
tests/datasets/tree.xml Normal file
View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<tree id="1" lft="1" rgt="2" depth="0" name="Item"/>
</dataset>

View file

@ -0,0 +1,24 @@
/**
* SQLite
*/
DROP TABLE IF EXISTS "tree";
CREATE TABLE "tree" (
"id" INTEGER NOT NULL PRIMARY KEY,
"lft" INTEGER NOT NULL,
"rgt" INTEGER NOT NULL,
"depth" INTEGER NOT NULL,
"name" TEXT NOT NULL
);
DROP TABLE IF EXISTS "multiple_roots_tree";
CREATE TABLE "multiple_roots_tree" (
"id" INTEGER NOT NULL PRIMARY KEY,
"tree" INTEGER,
"lft" INTEGER NOT NULL,
"rgt" INTEGER NOT NULL,
"depth" INTEGER NOT NULL,
"name" TEXT NOT NULL
);

68
tests/models/Tree.php Normal file
View file

@ -0,0 +1,68 @@
<?php
/**
* @link https://github.com/creocoder/yii2-nested-sets-behavior
* @copyright Copyright (c) 2015 Alexander Kochetov
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace tests\models;
use creocoder\nestedsets\NestedSetsBehavior;
/**
* Tree
*
* @property integer $id
* @property integer $lft
* @property integer $rgt
* @property integer $depth
* @property string $name
*/
class Tree extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'tree';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
NestedSetsBehavior::className(),
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
['name', 'required'],
];
}
/**
* @inheritdoc
*/
public function transactions()
{
return [
self::SCENARIO_DEFAULT => self::OP_ALL,
];
}
/**
* @inheritdoc
*/
public static function find()
{
return new TreeQuery(get_called_class());
}
}

View file

@ -0,0 +1,26 @@
<?php
/**
* @link https://github.com/creocoder/yii2-nested-sets-behavior
* @copyright Copyright (c) 2015 Alexander Kochetov
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace tests\models;
use creocoder\nestedsets\NestedSetsQueryBehavior;
/**
* TreeQuery
*/
class TreeQuery extends \yii\db\ActiveQuery
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
NestedSetsQueryBehavior::className(),
];
}
}