Nette tester Rest API odeslání rawBody

Jackpot
Člen | 2
+
+1
-

Ahoj,

jak v testech odeslat json v body (rawBody) požadavku tak abych mohl otestovat jeho zpracování?

helvete
Člen | 16
+
+1
-

Ahoj,

ja mam v test helperu neco jako toto:

protected function loadPresenterHttpRequest($presClassRef, array $query,
    callable $rawBodyCallback = null, $method = null
) {
    $urlScript = new \Nette\Http\UrlScript();
    foreach ($query as $paramName => $paramVal) {
        $urlScript->setQueryParameter($paramName, $paramVal);
    }
    $request = new \Nette\Http\Request(
        $urlScript,
        null,
        null,
        null,
        null,
        null,
        $method,
        null,
        null,
        $rawBodyCallback
    );
    $response = new \Nette\Http\Response();
    $session = new \Nette\Http\Session($request, $response);
    $user = new \Nette\Security\User(new \Nette\Http\UserStorage($session));
    $presenter = new $presClassRef();
    $presenter->injectPrimary(
        $this->container,
        null,
        null,
        $request,
        $response,
        $session,
        $user
    );
    $this->presenter = $presenter;
    $this->setPresenterDefaults();
}

A neco takovyhleho pak pouziju v danem testu, ktery potrebuje otestovat raw POST:

parent::loadPresenterHttpRequest(
     \Na\Me\Space\ExamplePresenter::class,
     [$queryParamName => $queryParamVal],
     static function() use ($postData) {return json_encode($postData);},
			self::POST
 );

Snad pomuze..

Stanislav Šimek
Člen | 2
+
+1
-

Řešení s použitím reflexe. Bez volání new $presClassRef().

use Nette\Application\IPresenterFactory;
use Nette\Application\UI\Presenter;
use Nette\DI\Container;
use Nette\Http\Request;
use Nette\Http\UrlScript;

class TestPresenterHelper {

	/** @var Container */
	private $container;

	/** @var IPresenterFactory */
	private $presenterFactory;

	public function __construct(Container $container) {
		$this->container = $container;
		$this->presenterFactory = $this->container->getByType(IPresenterFactory::class);
	}

	public function createPresenterWithHttpRequest(
			string $presenterName,
			array $query = [],
			$method = 'GET',
			callable $rawBodyCallback = null
	) {
		$presenter = $this->presenterFactory->createPresenter($presenterName);

		$urlScript = new UrlScript();
		foreach ($query as $paramName => $paramVal) {
			$urlScript->setQueryParameter($paramName, $paramVal);
		}

		$requestProperty = new \ReflectionProperty(Presenter::class, 'request');
		$requestProperty->setAccessible(true);
		$requestProperty->setValue(
				$presenter,
				new Request(
						$urlScript,
						null,
						null,
						null,
						null,
						null,
						$method,
						null,
						null,
						$rawBodyCallback
				)
		);

		return $presenter;
	}

}

Použití v testu s JSON payloadem:

		$presenter = $this->testPresenterHelper->createPresenterWithHttpRequest(
				'Api:Batch',
				[],
				'POST',
				function () {
					return '{"json": "request"}';
				}
		);
		$response = $presenter->run(new \Nette\Application\Request('App:Api:Batch', 'POST'));
		$responseJson = json_encode($response->getPayload());
Stanislav Šimek
Člen | 2
+
0
-

Oprava:

$requestProperty = new \ReflectionProperty(Presenter::class, 'httpRequest');