<?php
namespace tests\thinkphp\library\traits\model;
use think\Db;
use think\Model;
use traits\model\SoftDelete;
class softDeleteTest extends \PHPUnit_Framework_TestCase
{
const TEST_TIME = 10000;
public function setUp()
{
$config = (new testClassWithSoftDelete())->connection;
$sql[] = <<<SQL
DROP TABLE IF EXISTS `tp_soft_delete`;
SQL;
$sql[] = <<<SQL
CREATE TABLE `tp_soft_delete` (
`id` int(10) unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` char(40) NOT NULL DEFAULT '' COMMENT '用户名',
`delete_time` int(10) DEFAULT NULL COMMENT '软删除时间'
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='ThinkPHP SoftDelete Test';
SQL;
$time = self::TEST_TIME;
$sql[] = "INSERT INTO tp_soft_delete (`id`, `name`, `delete_time`) VALUES (1, 'valid data1', null)";
$sql[] = "INSERT INTO tp_soft_delete (`id`, `name`, `delete_time`) VALUES (2, 'invalid data2', {$time})";
$sql[] = "INSERT INTO tp_soft_delete (`id`, `name`, `delete_time`) VALUES (3, 'invalid data3', {$time})";
$sql[] = "INSERT INTO tp_soft_delete (`id`, `name`, `delete_time`) VALUES (4, 'valid data4', null)";
$sql[] = "INSERT INTO tp_soft_delete (`id`, `name`, `delete_time`) VALUES (5, 'valid data5', null)";
foreach ($sql as $one) {
Db::connect($config)->execute($one);
}
}
public function testTrashed()
{
$selections = testClassWithSoftDelete::withTrashed()->select();
$this->assertFalse($selections[0]->trashed());
$this->assertTrue($selections[1]->trashed());
$this->assertTrue($selections[2]->trashed());
}
public function testDefaultTrashed()
{
$this->assertCount(3, testClassWithSoftDelete::all());
}
public function testWithTrashed()
{
$this->assertCount(5, testClassWithSoftDelete::withTrashed()->select());
}
public function testOnlyTrashed()
{
$this->assertCount(2, testClassWithSoftDelete::onlyTrashed()->select());
}
public function testSoftDelete()
{
$this->assertEquals(1, testClassWithSoftDelete::get(1)->delete());
$this->assertNotNull(testClassWithSoftDelete::withTrashed()->find(1)->getData('delete_time'));
}
public function testForceDelete()
{
$this->assertEquals(1, testClassWithSoftDelete::get(1)->delete(true));
$this->assertNull(testClassWithSoftDelete::get(1));
}
public function testSoftDestroy()
{
$this->assertEquals(5, testClassWithSoftDelete::destroy([1, 2, 3, 4, 5, 6]));
$this->assertNotNull(testClassWithSoftDelete::withTrashed()->find(2)->getData('delete_time'));
$this->assertNotEquals(self::TEST_TIME, testClassWithSoftDelete::withTrashed()->find(2)->getData('delete_time'));
$this->assertNotEquals(self::TEST_TIME, testClassWithSoftDelete::withTrashed()->find(3)->getData('delete_time'));
$this->assertNotNull(testClassWithSoftDelete::withTrashed()->find(4)->getData('delete_time'));
$this->assertNotNull(testClassWithSoftDelete::withTrashed()->find(5)->getData('delete_time'));
}
public function testForceDestroy()
{
$this->assertEquals(5, testClassWithSoftDelete::destroy([1, 2, 3, 4, 5, 6], true));
$this->assertNull(testClassWithSoftDelete::withTrashed()->find(1));
$this->assertNull(testClassWithSoftDelete::withTrashed()->find(2));
$this->assertNull(testClassWithSoftDelete::withTrashed()->find(3));
$this->assertNull(testClassWithSoftDelete::withTrashed()->find(4));
$this->assertNull(testClassWithSoftDelete::withTrashed()->find(5));
}
public function testRestore()
{
$selections = testClassWithSoftDelete::withTrashed()->select();
$this->assertEquals(0, $selections[0]->restore());
$this->assertEquals(1, $selections[1]->restore());
$this->assertEquals(1, $selections[2]->restore());
$this->assertEquals(0, $selections[3]->restore());
$this->assertEquals(0, $selections[4]->restore());
$this->assertNull(testClassWithSoftDelete::withTrashed()->find(1)->getData('delete_time'));
$this->assertNull(testClassWithSoftDelete::withTrashed()->find(2)->getData('delete_time'));
}
public function testGetDeleteTimeField()
{
$testClass = new testClassWithSoftDelete();
$this->assertEquals('delete_time', $testClass->getDeleteTimeField());
$testClass->deleteTime = 'create_time';
$this->assertEquals('create_time', $testClass->getDeleteTimeField());
$testClass->deleteTime = 'test.create_time';
$this->assertEquals('create_time', $testClass->getDeleteTimeField());
$testClass->deleteTime = 'create_time';
$this->assertEquals('__TABLE__.create_time', $testClass->getDeleteTimeField(true));
}
}
class testClassWithSoftDelete extends Model
{
public $table = 'tp_soft_delete';
public $deleteTime = 'delete_time';
public $connection = [
'type' => 'mysql',
'hostname' => '127.0.0.1',
'database' => 'test',
'username' => 'root',
'password' => '',
'hostport' => '',
'dsn' => '',
'params' => [],
'charset' => 'utf8',
'prefix' => '',
'debug' => true,
'deploy' => 0,
'rw_separate' => false,
'master_num' => 1,
'slave_no' => '',
'fields_strict' => true,
'resultset_type' => 'array',
'auto_timestamp' => false,
'sql_explain' => false,
];
use SoftDelete {
getDeleteTimeField as public;
}
}