Skip to content

Commit 29aa927

Browse files
committed
send* methods to return values
1 parent 2f35e75 commit 29aa927

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

src/Codeception/Module/REST.php

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ public function amAWSAuthenticated($additionalAWSConfig = [])
431431
* ```php
432432
* <?php
433433
* //simple POST call
434-
* $I->sendPost('/message', ['subject' => 'Read this!', 'to' => '[email protected]']);
434+
* $response = $I->sendPost('/message', ['subject' => 'Read this!', 'to' => '[email protected]']);
435435
* //simple upload method
436436
* $I->sendPost('/message/24', ['inline' => 0], ['attachmentFile' => codecept_data_dir('sample_file.pdf')]);
437437
* //uploading a file with a custom name and mime-type. This is also useful to simulate upload errors.
@@ -465,7 +465,7 @@ public function amAWSAuthenticated($additionalAWSConfig = [])
465465
*/
466466
public function sendPost($url, $params = [], $files = [])
467467
{
468-
$this->execute('POST', $url, $params, $files);
468+
return $this->execute('POST', $url, $params, $files);
469469
}
470470

471471
/**
@@ -478,7 +478,7 @@ public function sendPost($url, $params = [], $files = [])
478478
*/
479479
public function sendHead($url, $params = [])
480480
{
481-
$this->execute('HEAD', $url, $params);
481+
return $this->execute('HEAD', $url, $params);
482482
}
483483

484484
/**
@@ -497,19 +497,32 @@ public function sendOptions($url, $params = [])
497497
/**
498498
* Sends a GET request to given uri.
499499
*
500+
* ```php
501+
* <?php
502+
* $response = $I->sendGet('/users');
503+
*
504+
* // send get with query params
505+
* $I->sendGet('/orders', ['id' => 1])
506+
* ```
507+
*
500508
* @param $url
501509
* @param array $params
502510
* @part json
503511
* @part xml
504512
*/
505513
public function sendGet($url, $params = [])
506514
{
507-
$this->execute('GET', $url, $params);
515+
return $this->execute('GET', $url, $params);
508516
}
509517

510518
/**
511519
* Sends PUT request to given uri.
512520
*
521+
* ```php
522+
* <?php
523+
* $response = $I->sendPut('/message/1', ['subject' => 'Read this!']);
524+
* ```
525+
*
513526
* @param $url
514527
* @param array|string|\JsonSerializable $params
515528
* @param array $files
@@ -518,12 +531,17 @@ public function sendGet($url, $params = [])
518531
*/
519532
public function sendPut($url, $params = [], $files = [])
520533
{
521-
$this->execute('PUT', $url, $params, $files);
534+
return $this->execute('PUT', $url, $params, $files);
522535
}
523536

524537
/**
525538
* Sends PATCH request to given uri.
526539
*
540+
* ```php
541+
* <?php
542+
* $response = $I->sendPatch('/message/1', ['subject' => 'Read this!']);
543+
* ```
544+
*
527545
* @param $url
528546
* @param array|string|\JsonSerializable $params
529547
* @param array $files
@@ -532,11 +550,17 @@ public function sendPut($url, $params = [], $files = [])
532550
*/
533551
public function sendPatch($url, $params = [], $files = [])
534552
{
535-
$this->execute('PATCH', $url, $params, $files);
553+
return $this->execute('PATCH', $url, $params, $files);
536554
}
537555

538556
/**
539557
* Sends DELETE request to given uri.
558+
*
559+
* ```php
560+
* <?php
561+
* $I->sendDelete('/message/1');
562+
* ```
563+
540564
*
541565
* @param $url
542566
* @param array $params
@@ -546,7 +570,7 @@ public function sendPatch($url, $params = [], $files = [])
546570
*/
547571
public function sendDelete($url, $params = [], $files = [])
548572
{
549-
$this->execute('DELETE', $url, $params, $files);
573+
return $this->execute('DELETE', $url, $params, $files);
550574
}
551575

552576
/**
@@ -561,7 +585,7 @@ public function sendDelete($url, $params = [], $files = [])
561585
*/
562586
public function send($method, $url, $params = [], $files = [])
563587
{
564-
$this->execute(strtoupper($method), $url, $params, $files);
588+
return $this->execute(strtoupper($method), $url, $params, $files);
565589
}
566590

567591
/**
@@ -688,6 +712,8 @@ protected function execute($method, $url, $parameters = [], $files = [])
688712
} else {
689713
$this->debugSection("Response", $printedResponse);
690714
}
715+
716+
return $this->response;
691717
}
692718

693719
/**

tests/unit/Codeception/Module/RestTest.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,34 +64,43 @@ public function testBeforeHookResetsVariables()
6464

6565
public function testGet()
6666
{
67-
$this->module->sendGET('/rest/user/');
67+
$response = $this->module->sendGET('/rest/user/');
6868
$this->module->seeResponseIsJson();
6969
$this->module->seeResponseContains('davert');
7070
$this->module->seeResponseContainsJson(['name' => 'davert']);
7171
$this->module->seeResponseCodeIs(200);
7272
$this->module->dontSeeResponseCodeIs(404);
73+
$this->assertNotEmpty($response);
74+
$this->assertStringContainsString('"name":"davert"', $response);
7375
}
7476

7577
public function testPost()
7678
{
77-
$this->module->sendPOST('/rest/user/', ['name' => 'john']);
79+
$response = $this->module->sendPOST('/rest/user/', ['name' => 'john']);
7880
$this->module->seeResponseContains('john');
7981
$this->module->seeResponseContainsJson(['name' => 'john']);
82+
$this->assertNotEmpty($response);
83+
$this->assertStringContainsString('"name":"john"', $response);
8084
}
8185

8286
public function testPut()
8387
{
84-
$this->module->sendPUT('/rest/user/', ['name' => 'laura']);
88+
$response = $this->module->sendPUT('/rest/user/', ['name' => 'laura']);
8589
$this->module->seeResponseContains('[email protected]');
8690
$this->module->seeResponseContainsJson(['name' => 'laura']);
8791
$this->module->dontSeeResponseContainsJson(['name' => 'john']);
92+
$this->assertNotEmpty($response);
93+
$this->assertStringContainsString('"name":"laura"', $response);
94+
8895
}
8996

9097
public function testSend()
9198
{
92-
$this->module->send('POST','/rest/user/', ['name' => 'john']);
99+
$response = $this->module->send('POST','/rest/user/', ['name' => 'john']);
93100
$this->module->seeResponseContains('john');
94101
$this->module->seeResponseContainsJson(['name' => 'john']);
102+
$this->assertNotEmpty($response);
103+
$this->assertStringContainsString('"name":"john"', $response);
95104
}
96105

97106
public function testGrabDataFromResponseByJsonPath()

0 commit comments

Comments
 (0)