1+ <?php
2+
3+ require_once (__DIR__ . '/../api.php ' );
4+
5+ class Api
6+ {
7+ /**
8+ * Database configuration array
9+ *
10+ * @var array
11+ */
12+ protected $ config ;
13+
14+ /**
15+ * @var PHP_CRUD_API_Test
16+ */
17+ protected $ test ;
18+
19+ /**
20+ * @var PHP_CRUD_API
21+ */
22+ protected $ api ;
23+
24+ public function __construct ($ test )
25+ {
26+ $ this ->test = $ test ;
27+ $ this ->config = $ test ::$ config ;
28+ $ this ->config ['dbengine ' ] = $ test ->getEngineName ();
29+ }
30+
31+ private function action ($ method ,$ url ,$ data ='' )
32+ {
33+ $ url = parse_url ($ url );
34+ $ query = isset ($ url ['query ' ])?$ url ['query ' ]:'' ;
35+ parse_str ($ query ,$ get );
36+
37+ $ this ->api = new PHP_CRUD_API (array (
38+ 'dbengine ' =>$ this ->config ['dbengine ' ],
39+ 'hostname ' =>$ this ->config ['hostname ' ],
40+ 'username ' =>$ this ->config ['username ' ],
41+ 'password ' =>$ this ->config ['password ' ],
42+ 'database ' =>$ this ->config ['database ' ],
43+ // callbacks
44+ 'table_authorizer ' =>function ($ action ,$ database ,$ table ) { return true ; },
45+ 'column_authorizer ' =>function ($ action ,$ database ,$ table ,$ column ) { return !($ column =='password ' &&$ action =='list ' ); },
46+ 'record_filter ' =>function ($ action ,$ database ,$ table ) { return ($ table =='posts ' )?array ('id,neq,13 ' ):false ; },
47+ 'tenancy_function ' =>function ($ action ,$ database ,$ table ,$ column ) { return ($ table =='users ' &&$ column =='id ' )?1 :null ; },
48+ 'input_sanitizer ' =>function ($ action ,$ database ,$ table ,$ column ,$ type ,$ value ) { return is_string ($ value )?strip_tags ($ value ):$ value ; },
49+ 'input_validator ' =>function ($ action ,$ database ,$ table ,$ column ,$ type ,$ value ,$ context ) { return ($ column =='category_id ' && !is_numeric ($ value ))?'must be numeric ' :true ; },
50+ 'before ' =>function (&$ action ,&$ database ,&$ table ,&$ id ,&$ input ) { if ($ table =='products ' ) if ($ action =='create ' ) $ input ->created_at = '2013-12-11 10:09:08 ' ; else if ($ action =='delete ' ) { $ action ='update ' ; $ input = (object )array ('deleted_at ' => '2013-12-11 11:10:09 ' ); } },
51+ '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 ); },
52+ // for tests
53+ 'method ' =>$ method ,
54+ 'request ' =>$ url ['path ' ],
55+ 'post ' =>$ data ,
56+ 'get ' =>$ get ,
57+ ));
58+ return $ this ;
59+ }
60+
61+ public function get ($ url )
62+ {
63+ return $ this ->action ('GET ' ,$ url );
64+ }
65+
66+ public function post ($ url ,$ data )
67+ {
68+ return $ this ->action ('POST ' ,$ url ,$ data );
69+ }
70+
71+ public function put ($ url ,$ data )
72+ {
73+ return $ this ->action ('PUT ' ,$ url ,$ data );
74+ }
75+
76+ public function delete ($ url )
77+ {
78+ return $ this ->action ('DELETE ' ,$ url );
79+ }
80+
81+ public function options ($ url )
82+ {
83+ return $ this ->action ('OPTIONS ' ,$ url );
84+ }
85+
86+ public function patch ($ url ,$ data )
87+ {
88+ return $ this ->action ('PATCH ' ,$ url ,$ data );
89+ }
90+
91+ public function expectAny ()
92+ {
93+ ob_start ();
94+ $ this ->api ->executeCommand ();
95+ ob_end_clean ();
96+ return $ this ;
97+ }
98+
99+ public function expect ($ output ,$ error =false )
100+ {
101+ $ exception = false ;
102+ ob_start ();
103+ try {
104+ $ this ->api ->executeCommand ();
105+ } catch (\Exception $ e ) {
106+ $ exception = $ e ->getMessage ();
107+ }
108+ $ data = ob_get_contents ();
109+ ob_end_clean ();
110+ if ($ exception ) $ this ->test ->assertEquals ($ error , $ exception );
111+ else $ this ->test ->assertEquals ($ output , $ data );
112+ return $ this ;
113+ }
114+ }
0 commit comments