Browse Source

add test for soft delete

mevdschee 8 years ago
parent
commit
7ca56f37b5
3 changed files with 13 additions and 3 deletions
  1. 1
    1
      api.php
  2. 2
    1
      tests/blog_mysql.sql
  3. 10
    1
      tests/tests.php

+ 1
- 1
api.php View File

@@ -1140,7 +1140,7 @@ class PHP_CRUD_API {
1140 1140
 			for ($i=0;$i<$max;$i++) {
1141 1141
 				if (!isset($ids[$i])) $ids[$i] = false;
1142 1142
 				if (!isset($inputs[$i])) $inputs[$i] = false;
1143
-				$callback($action,$database,$table,$id,$inputs[$i]);
1143
+				$callback($action,$database,$table,$ids[$i],$inputs[$i]);
1144 1144
 			}
1145 1145
 		}
1146 1146
 	}

+ 2
- 1
tests/blog_mysql.sql View File

@@ -124,8 +124,9 @@ CREATE TABLE `products` (
124 124
   `id` int(11) NOT NULL AUTO_INCREMENT,
125 125
   `name` varchar(255) NOT NULL,
126 126
   `price` decimal(10,2) NOT NULL,
127
-  `properties` JSON NOT NULL,
127
+  `properties` TEXT NOT NULL,
128 128
   `created_at` datetime NOT NULL,
129
+  `deleted_at` datetime NULL,
129 130
   PRIMARY KEY (`id`)
130 131
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
131 132
 

+ 10
- 1
tests/tests.php View File

@@ -34,7 +34,7 @@ class API
34 34
 				'tenancy_function'=>function($action,$database,$table,$column) { return ($table=='users'&&$column=='id')?1:null; },
35 35
 				'input_sanitizer'=>function($action,$database,$table,$column,$type,$value) { return is_string($value)?strip_tags($value):$value; },
36 36
 				'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'); } },
38 38
 				'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); },
39 39
 				// for tests
40 40
 				'method'=>$method,
@@ -790,4 +790,13 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
790 790
 		$test->get('/products/2');
791 791
 		$test->expect('{"id":2,"name":"Laptop","price":"1299.99","properties":{},"created_at":"2013-12-11 10:09:08"}');
792 792
 	}
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
+	}
793 802
 }

Loading…
Cancel
Save