Browse Source

Merge pull request #367 from noobiek/master

added tests for tenancy + fix for PHP 7.2
Maurits van der Schee 6 years ago
parent
commit
e16469a3ad
No account linked to committer's email address
2 changed files with 81 additions and 1 deletions
  1. 1
    1
      api.php
  2. 80
    0
      tests/Tests.php

+ 1
- 1
api.php View File

@@ -1136,7 +1136,7 @@ class PHP_CRUD_API {
1136 1136
 
1137 1137
 	protected function applyBeforeHandler(&$action,&$database,&$table,&$ids,&$callback,&$inputs) {
1138 1138
 		if (is_callable($callback,true)) {
1139
-			$max = count($ids)?:count($inputs);
1139
+			$max = (is_array($ids)&&count($ids))?:count($inputs);
1140 1140
 			$values = array('action'=>$action,'database'=>$database,'table'=>$table);
1141 1141
 			for ($i=0;$i<$max;$i++) {
1142 1142
 				$action = $values['action'];

+ 80
- 0
tests/Tests.php View File

@@ -674,4 +674,84 @@ abstract class Tests extends TestBase
674 674
         $test->get('/users?columns=username,location');
675 675
         $test->expect('{"users":{"columns":["username","location"],"records":[["user1","POINT(30 20)"]]}}');
676 676
     }
677
+
678
+    public function testTenancyCreateColumns()
679
+    {
680
+        // creation should fail, since due to tenancy function it will try to create with id=1, which is a PK and is already taken
681
+        $test = new Api($this);
682
+        $test->post('/users?columns=username,password,location', '{"username":"user3","password":"pass3","location":null}');
683
+        $test->expect('null');
684
+    }
685
+
686
+    public function testTenancyCreateExclude()
687
+    {
688
+        // creation should fail, since due to tenancy function it will try to create with id=1, which is a PK and is already taken
689
+        $test = new Api($this);
690
+        $test->post('/users?exclude=id', '{"username":"user3","password":"pass3","location":null}');
691
+        $test->expect('null');
692
+    }
693
+
694
+    public function testTenancyListColumns()
695
+    {
696
+        // should list only user with id=1 (exactly 1 record)
697
+        $test = new Api($this);
698
+        $test->get('/users?columns=username,location');
699
+        $test->expect('{"users":{"columns":["username","location"],"records":[["user1",null]]}}');
700
+    }
701
+
702
+    public function testTenancyListExclude()
703
+    {
704
+        // should list only user with id=1 (exactly 1 record)
705
+        $test = new Api($this);
706
+        $test->get('/users?exclude=id');
707
+        $test->expect('{"users":{"columns":["username","location"],"records":[["user1",null]]}}');
708
+    }
709
+
710
+    public function testTenancyReadColumns()
711
+    {
712
+        // should fail, since due to tenancy function user id=2 is unvailable to us
713
+        $test = new Api($this);
714
+        $test->get('/users/2?columns=username,location');
715
+        $test->expect(false, 'Not found (object)');
716
+    }
717
+
718
+    public function testTenancyReadExclude()
719
+    {
720
+        // should fail, since due to tenancy function user id=2 is unvailable to us
721
+        $test = new Api($this);
722
+        $test->get('/users/2?exclude=id');
723
+        $test->expect(false, 'Not found (object)');
724
+    }
725
+
726
+    public function testTenancyUpdateColumns()
727
+    {
728
+        // should fail, since due to tenancy function user id=2 is unvailable to us
729
+        $test = new Api($this);
730
+        $test->put('/users/2?columns=location', '{"location":"somelocation"}');
731
+        $test->expect('0');
732
+    }
733
+
734
+    public function testTenancyUpdateExclude()
735
+    {
736
+        // should fail, since due to tenancy function user id=2 is unvailable to us
737
+        $test = new Api($this);
738
+        $test->put('/users/2?exclude=id', '{"location":"somelocation"}');
739
+        $test->expect('0');
740
+    }
741
+
742
+    public function testTenancyDeleteColumns()
743
+    {
744
+        // should fail, since due to tenancy function user id=2 is unvailable to us
745
+        $test = new Api($this);
746
+        $test->delete('/users/2?columns=location');
747
+        $test->expect('0');
748
+    }
749
+
750
+    public function testTenancyDeleteExclude()
751
+    {
752
+        // should fail, since due to tenancy function user id=2 is unvailable to us
753
+        $test = new Api($this);
754
+        $test->delete('/users/2?exclude=id');
755
+        $test->expect('0');
756
+    }
677 757
 }

Loading…
Cancel
Save