Skip to content

Commit 1ec32bb

Browse files
committed
Make the input for the rest api controller mockable
1 parent 88747c7 commit 1ec32bb

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/Controller/PhpInput.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ShoppinPal\YapepCommon\Controller;
5+
6+
class PhpInput
7+
{
8+
public function getStdIn(): string
9+
{
10+
return file_get_contents('php://input');
11+
}
12+
}

src/Controller/RestApiController.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use ShoppinPal\YapepCommon\DataObject\RestResponseDo;
77
use ShoppinPal\YapepCommon\Exception\RestException;
8+
use YapepBase\Application;
89
use YapepBase\Config;
910
use YapepBase\Controller\HttpController;
1011
use YapepBase\Exception\ControllerException;
@@ -44,10 +45,15 @@ public function __construct(IRequest $request, IResponse $response)
4445
*/
4546
public function run($action)
4647
{
48+
/** @var PhpInput $input */
49+
$input = Application::getInstance()->getDiContainer()[PhpInput::class];
50+
if (null === $input) {
51+
$input = new PhpInput();
52+
}
4753
try {
4854
$this->response->setContentType(MimeType::JSON);
4955

50-
$this->requestBody = file_get_contents('php://input');
56+
$this->requestBody = $input->getStdIn();
5157

5258
if (!empty($this->requestBody)) {
5359
$requestData = json_decode($this->requestBody, true);

src/Test/Controller/FakeInput.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ShoppinPal\YapepCommon\Test\Controller;
5+
6+
use ShoppinPal\YapepCommon\Controller\PhpInput;
7+
8+
class FakeInput extends PhpInput
9+
{
10+
/** @var string */
11+
private $stdIn;
12+
13+
public function __construct(string $stdIn)
14+
{
15+
$this->stdIn = $stdIn;
16+
}
17+
18+
public function getStdIn(): string
19+
{
20+
return $this->stdIn;
21+
}
22+
23+
}

0 commit comments

Comments
 (0)