Skip to content

Commit 7ca56f3

Browse files
committed
add test for soft delete
1 parent 1b3ff0d commit 7ca56f3

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ protected function applyBeforeHandler(&$action,&$database,&$table,&$ids,&$callba
11401140
for ($i=0;$i<$max;$i++) {
11411141
if (!isset($ids[$i])) $ids[$i] = false;
11421142
if (!isset($inputs[$i])) $inputs[$i] = false;
1143-
$callback($action,$database,$table,$id,$inputs[$i]);
1143+
$callback($action,$database,$table,$ids[$i],$inputs[$i]);
11441144
}
11451145
}
11461146
}

tests/blog_mysql.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ CREATE TABLE `products` (
124124
`id` int(11) NOT NULL AUTO_INCREMENT,
125125
`name` varchar(255) NOT NULL,
126126
`price` decimal(10,2) NOT NULL,
127-
`properties` JSON NOT NULL,
127+
`properties` TEXT NOT NULL,
128128
`created_at` datetime NOT NULL,
129+
`deleted_at` datetime NULL,
129130
PRIMARY KEY (`id`)
130131
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
131132

tests/tests.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private function action($method,$url,$data='')
3434
'tenancy_function'=>function($action,$database,$table,$column) { return ($table=='users'&&$column=='id')?1:null; },
3535
'input_sanitizer'=>function($action,$database,$table,$column,$type,$value) { return is_string($value)?strip_tags($value):$value; },
3636
'input_validator'=>function($action,$database,$table,$column,$type,$value,$context) { return ($column=='category_id' && !is_numeric($value))?'must be numeric':true; },
37-
'before'=>function(&$action,&$database,&$table,&$id,&$input) { if ($action=='create' && $input!==false) $input->created_at = '2013-12-11 10:09:08'; },
37+
'before'=>function(&$action,&$database,&$table,&$id,&$input) { if ($action=='create' && $input!==false) $input->created_at = '2013-12-11 10:09:08'; else if ($action=='delete' && $table=='products') { $action='update'; $input = (object)array('deleted_at' => '2013-12-11 10:09:08'); } },
3838
'after'=>function($action,$database,$table,$id,$input,$output) { file_put_contents('log.txt',var_export(array($action,$database,$table,$id,$input,$output),true),FILE_APPEND); },
3939
// for tests
4040
'method'=>$method,
@@ -790,4 +790,13 @@ public function testAddProducts()
790790
$test->get('/products/2');
791791
$test->expect('{"id":2,"name":"Laptop","price":"1299.99","properties":{},"created_at":"2013-12-11 10:09:08"}');
792792
}
793+
794+
public function testSoftDeleteProduct()
795+
{
796+
$test = new API($this);
797+
$test->delete('/products/2');
798+
$test->expect('2');
799+
//$test->get('/products/2');
800+
//$test->expect('{"id":2,"name":"Laptop","price":"1299.99","properties":{},"created_at":"2013-12-11 10:09:08"}');
801+
}
793802
}

0 commit comments

Comments
 (0)