1+ <?php
2+
3+ namespace Tests \Api ;
4+
5+ use ApiTester ;
6+ use Codeception \Example ;
7+ use Codeception \Util \HttpCode ;
8+
9+ /**
10+ * Test all basic rest requests
11+ */
12+ class RestCest
13+ {
14+ /**
15+ * @return array
16+ */
17+ protected function endpoints ()
18+ {
19+ return [
20+ ['endpoint ' => 'posts ' ],
21+ //...
22+ //Other RESTful objects
23+ ];
24+ }
25+
26+ /**
27+ * @param ApiTester $I
28+ * @return mixed
29+ */
30+ private function authenticate (ApiTester $ I )
31+ {
32+ $ user = factory (\App \User::class)->create ();
33+ //$I->haveHttpHeader("token", $user->getToken());
34+ return $ user ;
35+ }
36+
37+ /**
38+ * @param ApiTester $I
39+ * @param Example $example
40+ * @dataprovider endpoints
41+ */
42+ public function testListForbidden (ApiTester $ I , Example $ example )
43+ {
44+ $ I ->sendGet ('/api/ ' .$ example ['endpoint ' ]);
45+ //Should be forbidden if we have auth layer
46+ //$I->seeResponseCodeIs(HttpCode::FORBIDDEN);
47+ $ I ->seeResponseIsJson ();
48+ }
49+
50+ /**
51+ * @param ApiTester $I
52+ * @param Example $example
53+ * @before authenticate
54+ * @dataprovider endpoints
55+ */
56+ public function testList (ApiTester $ I , Example $ example )
57+ {
58+ $ I ->sendGet ('/api/ ' .$ example ['endpoint ' ]);
59+ $ I ->seeResponseCodeIs (HttpCode::OK );
60+ $ I ->seeResponseIsJson ();
61+ }
62+
63+ /**
64+ * @param ApiTester $I
65+ * @param Example $example
66+ * @before authenticate
67+ * @dataprovider endpoints
68+ * @todo enhance method
69+ */
70+ /**public function testRead(ApiTester $I, Example $example)
71+ {
72+ $model = factory($example['model'])->create();
73+ $I->sendGet('/api/'.$example['endpoint'].'/'.$model->id);
74+ $I->seeResponseCodeIs(HttpCode::OK);
75+ $I->seeResponseIsJson();
76+ }**/
77+
78+ /**
79+ * @param ApiTester $I
80+ * @param Example $example
81+ * @before authenticate
82+ * @dataprovider endpoints
83+ */
84+ public function testReadNotFound (ApiTester $ I , Example $ example )
85+ {
86+ $ I ->sendGet ('/api/ ' .$ example ['endpoint ' ].'/ ' .sq ());
87+ $ I ->seeResponseCodeIs (HttpCode::NOT_FOUND );
88+ $ I ->seeResponseIsJson ();
89+ }
90+
91+ /**
92+ * @param ApiTester $I
93+ * @param Example $example
94+ * @before authenticate
95+ * @dataprovider endpoints
96+ * @todo fix and add
97+ */
98+ /**
99+ public function testCreate(ApiTester $I, Example $example)
100+ {
101+ $I->sendPost('/api/'.$example['endpoint'], []);
102+ $I->seeResponseCodeIs(HttpCode::CREATED);
103+ $I->seeResponseIsJson();
104+ }
105+ * **/
106+
107+ /**
108+ * @param ApiTester $I
109+ * @param Example $example
110+ * @before authenticate
111+ * @dataprovider endpoints
112+ */
113+ public function testUpdateNotFound (ApiTester $ I , Example $ example )
114+ {
115+ $ I ->haveHttpHeader ('Content-Type ' , 'application/json ' );
116+ $ I ->sendPUT ('/api/ ' .$ example ['endpoint ' ].'/ ' .sq ());
117+ $ I ->seeResponseCodeIs (HttpCode::NOT_FOUND );
118+ $ I ->seeResponseIsJson ();
119+ }
120+
121+ /**
122+ * @param ApiTester $I
123+ * @param Example $example
124+ * @before authenticate
125+ * @dataprovider endpoints
126+ */
127+ public function testDeleteNotFound (ApiTester $ I , Example $ example )
128+ {
129+ $ I ->sendDELETE ('/api/ ' .$ example ['endpoint ' ].'/ ' .sq ());
130+ $ I ->seeResponseCodeIs (HttpCode::NOT_FOUND );
131+ $ I ->seeResponseIsJson ();
132+ }
133+
134+
135+ }
0 commit comments