Commit 72f86cc1 authored by cornernote's avatar cornernote

added tests

parent 0e1ebba7
......@@ -4,7 +4,9 @@
.project
.buildpath
# ignore vendor
# ignore test data files and dirs
coverage.clover
build/
vendor/
# ignore temporary files
......
......@@ -36,10 +36,12 @@ In your ActiveRecord class:
```php
public function behaviors() {
return [
\cornernote\behaviors\SoftDeleteBehavior::className(),
// or
[
'class' => 'cornernote\behaviors\SoftDeleteBehavior',
'class' => \cornernote\behaviors\SoftDeleteBehavior::className(),
'attribute' => 'deleted_time',
'value' => new Expression('NOW()'),
'value' => new \yii\db\Expression('NOW()'), // for sqlite use - new \yii\db\Expression("date('now')")
],
];
}
......@@ -50,8 +52,10 @@ In your ActiveQuery class:
```php
public function behaviors() {
return [
\cornernote\behaviors\SoftDeleteQueryBehavior::className(),
// or
[
'class' => 'cornernote\behaviors\SoftDeleteQueryBehavior',
'class' => \cornernote\behaviors\SoftDeleteQueryBehavior::className(),
'attribute' => 'deleted_time',
],
];
......
......@@ -17,6 +17,10 @@
"require": {
"yiisoft/yii2": "*"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"phpunit/dbunit": "~1.0"
},
"autoload": {
"psr-4": {
"cornernote\\softdelete\\": "src"
......
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/functional/bootstrap.php"
<phpunit bootstrap="tests/unit/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
......@@ -11,12 +11,12 @@
stopOnFailure="false">
<testsuites>
<testsuite name="Yii2 SoftDelete Test Suite">
<directory>./tests/functional</directory>
<directory>tests/unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
......
<?php
/**
* @link https://github.com/cornernote/yii2-softdelete
* @copyright Copyright (c) 2013-2015 Brett O'Donnell <cornernote@gmail.com>
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace tests;
use Yii;
use yii\db\Connection;
/**
* DatabaseTestCase
*/
abstract class DatabaseTestCase extends \PHPUnit_Extensions_Database_TestCase
{
/**
* @inheritdoc
*/
public function getConnection()
{
return $this->createDefaultDBConnection(\Yii::$app->getDb()->pdo);
}
/**
* @inheritdoc
*/
public function getDataSet()
{
return $this->createFlatXMLDataSet(__DIR__ . '/data/test.xml');
}
/**
* @inheritdoc
*/
protected function setUp()
{
if (Yii::$app->get('db', false) === null) {
$this->markTestSkipped();
} else {
parent::setUp();
}
}
/**
* @inheritdoc
*/
public static function setUpBeforeClass()
{
try {
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);
}
}
} catch (\Exception $e) {
Yii::$app->clear('db');
}
}
}
\ No newline at end of file
<?php
/**
* SoftDeleteQueryTest.php
*
* @author Brett O'Donnell <cornernote@gmail.com>
* @link https://mrphp.com.au/
*/
namespace tests;
use tests\models\PostA;
use Yii;
/**
* SoftDeleteQueryTest
*/
class SoftDeleteQueryTest extends DatabaseTestCase
{
/**
* Find Deleted Posts
*/
public function testFindDeletedPosts()
{
$data = [];
$posts = PostA::find()->deleted()->all();
foreach ($posts as $post) {
$data[] = $post->toArray();
}
$this->assertEquals(require(__DIR__ . '/data/test-find-deleted-posts.php'), $data);
}
/**
* Find Not Deleted Posts
*/
public function testFindNotDeletedPosts()
{
$data = [];
$posts = PostA::find()->notDeleted()->all();
foreach ($posts as $post) {
$data[] = $post->toArray();
}
$this->assertEquals(require(__DIR__ . '/data/test-find-not-deleted-posts.php'), $data);
}
}
\ No newline at end of file
<?php
/**
* SoftDeleteTest.php
*
* @author Brett O'Donnell <cornernote@gmail.com>
* @link https://mrphp.com.au/
*/
namespace tests;
use tests\models\PostA;
use tests\models\PostB;
use Yii;
use yii\db\Connection;
/**
* SoftDeleteTest
*/
class SoftDeleteTest extends DatabaseTestCase
{
/**
* Soft Delete PostA
*/
public function testSoftDeletePostA()
{
/** @var PostA $post */
$post = PostA::findOne(2);
$post->delete();
$this->assertNotNull($post->deleted_at);
$post = PostA::findOne(2);
$this->assertNotNull($post->deleted_at);
}
/**
* Soft Un-Delete PostA
*/
public function testUnDeletePostA()
{
/** @var PostA $post */
$post = PostA::findOne(2);
$post->delete();
$this->assertNotNull($post->deleted_at);
$post = PostA::findOne(2);
$this->assertNotNull($post->deleted_at);
$post->unDelete();
$this->assertNull($post->deleted_at);
$post = PostA::findOne(2);
$this->assertNull($post->deleted_at);
}
/**
* Hard Delete PostA
*/
public function testHardDeletePostA()
{
/** @var PostA $post */
$post = PostA::findOne(2);
$post->hardDelete();
$post = PostA::findOne(2);
$this->assertNull($post);
}
/**
* Soft Delete PostB
*/
public function testSoftDeletePostB()
{
/** @var PostB $post */
$post = PostB::findOne(2);
$post->delete();
$this->assertNotNull($post->deleted_at);
$post = PostB::findOne(2);
$this->assertNotNull($post->deleted_at);
}
/**
* Soft Un-Delete PostB
*/
public function testUnDeletePostB()
{
/** @var PostB $post */
$post = PostB::findOne(2);
$post->delete();
$this->assertNotNull($post->deleted_at);
$post = PostB::findOne(2);
$this->assertNotNull($post->deleted_at);
$post->unDelete();
$this->assertNull($post->deleted_at);
$post = PostB::findOne(2);
$this->assertNull($post->deleted_at);
}
/**
* Hard Delete PostB
*/
public function testHardDeletePostB()
{
/** @var PostB $post */
$post = PostB::findOne(2);
$post->hardDelete();
$post = PostB::findOne(2);
$this->assertNull($post);
}
}
\ No newline at end of file
<?php
/**
* bootstrap.php
*
* @author Brett O'Donnell <cornernote@gmail.com>
* @link https://mrphp.com.au/
*/
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
require_once(__DIR__ . '/../../vendor/autoload.php');
require_once(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
Yii::setAlias('@tests', __DIR__);
new \yii\console\Application([
'id' => 'unit',
'basePath' => __DIR__,
]);
\ No newline at end of file
<?php
return [
[
'id' => 3,
'title' => 'Post title 3',
'body' => 'Post body 3',
'deleted_at' => 1434780293,
],
];
\ No newline at end of file
<?php
return [
[
'id' => 1,
'title' => 'Post title 1',
'body' => 'Post body 1',
'deleted_at' => null,
],
[
'id' => 2,
'title' => 'Post title 2',
'body' => 'Post body 2',
'deleted_at' => null,
],
];
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<post_a id="1" title="Post title 1" body="Post body 1"/>
<post_a id="2" title="Post title 2" body="Post body 2"/>
<post_a id="3" title="Post title 3" body="Post body 3" deleted_at="1434780293"/>
<post_b id="1" title="Post title 1" body="Post body 1"/>
<post_b id="2" title="Post title 2" body="Post body 2"/>
<post_b id="3" title="Post title 3" body="Post body 3" deleted_at="2015-06-20 16:10:37"/>
</dataset>
\ No newline at end of file
/**
* MySQL
*/
DROP TABLE IF EXISTS `post_a`;
CREATE TABLE `post_a` (
`id` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`body` TEXT NOT NULL,
`deleted_at` INT(11)
);
DROP TABLE IF EXISTS `post_b`;
CREATE TABLE `post_b` (
`id` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`body` TEXT NOT NULL,
`deleted_at` DATETIME
);
/**
* SQLite
*/
DROP TABLE IF EXISTS "post_a";
CREATE TABLE "post_a" (
"id" INTEGER NOT NULL PRIMARY KEY,
"title" TEXT NOT NULL,
"body" TEXT NOT NULL,
"deleted_at" INTEGER
);
DROP TABLE IF EXISTS "post_b";
CREATE TABLE "post_b" (
"id" INTEGER NOT NULL PRIMARY KEY,
"title" TEXT NOT NULL,
"body" TEXT NOT NULL,
"deleted_at" TEXT
);
<?php
/**
* @link https://github.com/cornernote/yii2-softdelete
* @copyright Copyright (c) 2013-2015 Mr PHP <info@mrphp.com.au>
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace tests\models;
use cornernote\softdelete\SoftDeleteBehavior;
use yii\db\ActiveRecord;
/**
* PostA
*
* @property integer $id
* @property string $title
* @property string $body
* @property integer $deleted_at
*
* @mixin SoftDeleteBehavior
*/
class PostA extends ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'post_a';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
SoftDeleteBehavior::className(),
];
}
/**
* @inheritdoc
*/
public static function find()
{
return new PostQuery(get_called_class());
}
}
<?php
/**
* @link https://github.com/cornernote/yii2-softdelete
* @copyright Copyright (c) 2013-2015 Mr PHP <info@mrphp.com.au>
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace tests\models;
use cornernote\softdelete\SoftDeleteBehavior;
use yii\db\ActiveRecord;
use yii\db\Expression;
/**
* PostB
*
* @property integer $id
* @property string $title
* @property string $body
* @property string $deleted_at
*
* @mixin SoftDeleteBehavior
*/
class PostB extends ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'post_b';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => SoftDeleteBehavior::className(),
'attribute' => 'deleted_at',
'value' => new Expression("date('now')"),
],
];
}
}
<?php
/**
* @link https://github.com/cornernote/yii2-softdelete
* @copyright Copyright (c) 2013-2015 Mr PHP <info@mrphp.com.au>
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace tests\models;
use cornernote\softdelete\SoftDeleteQueryBehavior;
use yii\db\ActiveQuery;
/**
* PostQuery
*
* @mixin SoftDeleteQueryBehavior
*/
class PostQuery extends ActiveQuery
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
SoftDeleteQueryBehavior::className(),
];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment