Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add runnable parameters convertor #12

Merged
merged 8 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
8 changes: 6 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ parameters:
count: 1
path: ./src/DependencyInjection
-
message: '#Cannot call method (.*)? on object|null.*#'
count: 2
message: '#Call to an undefined method object::clear().*#'
count: 1
path: ./src/Service/ResetManager/DoctrineResetManager
-
message: '#Call to an undefined method object::getConnection().*#'
count: 1
path: ./src/Service/ResetManager/DoctrineResetManager
Binary file added src/.DS_Store
Binary file not shown.
56 changes: 56 additions & 0 deletions src/Context/ApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RouterInterface;
use Throwable;

class ApiContext implements Context
{
Expand Down Expand Up @@ -114,6 +115,7 @@ public function theRequestContainsParams(PyStringNode $params): void
);

$newRequestParams = (array) json_decode($processedParams, true, 512, JSON_THROW_ON_ERROR);
$newRequestParams = $this->convertRunnableCodeParams($newRequestParams);
$this->requestParams = array_merge($this->requestParams, $newRequestParams);
$this->savedValues = array_merge($this->savedValues, $newRequestParams);
}
Expand Down Expand Up @@ -299,6 +301,60 @@ protected function compareStructureResponse(string $variableFields, PyStringNode
}
}

/**
* @param array $requestParams
*
* @When I have :requestParams with runnable values (placed in <>)
*
* @Then :requestParams should be converted with executing runnable code
alekseytupichenkov marked this conversation as resolved.
Show resolved Hide resolved
*/
public function convertRunnableCodeParams(array $requestParams): array
{
foreach ($requestParams as $key => $value) {
if (is_array($value)) {
$requestParams[$key] = $this->convertRunnableCodeParams($value);
continue;
}

if (!is_string($value)) {
continue;
}

$pregMatchValue = preg_match('/^<.*>$/', $value);
alekseytupichenkov marked this conversation as resolved.
Show resolved Hide resolved

if ($pregMatchValue === 0 || $pregMatchValue === false) {
continue;
}

$command = substr($value, 1, -1);
Vartanchik marked this conversation as resolved.
Show resolved Hide resolved

try {
$resultValue = eval('return ' . $command . ';');
} catch (Throwable $exception) {
throw new RuntimeException(
sprintf(
'Failed run your code %s, error message: %s',
$value,
$exception->getMessage()
)
);
}

if (is_null($resultValue)) {
throw new \RuntimeException(
sprintf(
'Running code: %s - should not return the null',
$command
)
);
}

$requestParams[$key] = $resultValue;
}

return $requestParams;
}

private function resetRequestOptions(): void
{
$this->headers = [];
Expand Down
Binary file added tests/.DS_Store
Binary file not shown.
Binary file added tests/Unit/.DS_Store
Binary file not shown.
130 changes: 130 additions & 0 deletions tests/Unit/Context/ApiContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

declare(strict_types=1);

namespace BehatApiContext\Tests\Unit\Context;

use BehatApiContext\Context\ApiContext;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RouterInterface;

class ApiContextTest extends TestCase
{
private const PARAMS_VALUES = 'paramsValues';
private const INITIAL_PARAM_VALUE = 'initialParamValue';
private ApiContext $apiContext;

protected function setUp(): void
{
$routerMock = $this->createMock(RouterInterface::class);
$requestStackMock = $this->createMock(RequestStack::class);
$kernelMock = $this->createMock(KernelInterface::class);

$this->apiContext = new ApiContext($routerMock, $requestStackMock, $kernelMock);
}

/**
* @param array $paramsValues
* @param string $initialParamValue
*
* @dataProvider getConvertRunnableCodeParamsDataSuccess
*/
public function testConvertRunnableCodeParamsSuccess(array $paramsValues, string $initialParamValue): void
{
$this->assertEquals($initialParamValue, $paramsValues['dateFrom']);
$this->assertEquals($initialParamValue, $paramsValues['levelOne']['dateFrom']);
$this->assertEquals($initialParamValue, $paramsValues['levelOne']['levelTwo']['dateFrom']);

$resultParamsValues = $this->apiContext->convertRunnableCodeParams($paramsValues);

$this->assertIsInt($resultParamsValues['dateFrom']);
$this->assertIsInt($resultParamsValues['levelOne']['dateFrom']);
$this->assertIsInt($resultParamsValues['levelOne']['levelTwo']['dateFrom']);
$this->assertEquals(10, strlen((string)$resultParamsValues['dateFrom']));
$this->assertEquals(10, strlen((string)$resultParamsValues['levelOne']['dateFrom']));
$this->assertEquals(10, strlen((string)$resultParamsValues['levelOne']['levelTwo']['dateFrom']));
$this->assertEquals($paramsValues['tripId'], $resultParamsValues['tripId']);
$this->assertEquals($paramsValues['dateTo'], $resultParamsValues['dateTo']);
$this->assertEquals($paramsValues['levelOne']['dateTo'], $resultParamsValues['levelOne']['dateTo']);
$this->assertEquals(
$paramsValues['levelOne']['levelTwo']['dateTo'],
$resultParamsValues['levelOne']['levelTwo']['dateTo']
);
}

/**
* @param array $paramsValues
*
* @dataProvider getConvertRunnableCodeParamsDataError
*/
public function testConvertRunnableCodeParamsError(array $paramsValues): void
{
$this->expectException(RuntimeException::class);
$this->apiContext->convertRunnableCodeParams($paramsValues);
}

public function getConvertRunnableCodeParamsDataSuccess(): array
{
return [
[
self::PARAMS_VALUES => [
'tripId' => '26e185b9-a233-470e-b2d4-2818908a075f',
'dateTo' => 1680361181,
'dateFrom' => '<(new DateTimeImmutable())->getTimestamp()>',
'levelOne' => [
'dateTo' => 1680343281,
'dateFrom' => '<(new DateTimeImmutable())->getTimestamp()>',
'levelTwo' => [
'dateTo' => 1680360351,
'dateFrom' => '<(new DateTimeImmutable())->getTimestamp()>',
]
],
],
self::INITIAL_PARAM_VALUE => '<(new DateTimeImmutable())->getTimestamp()>',
],
];
}

public function getConvertRunnableCodeParamsDataError(): array
{
return [
[
self::PARAMS_VALUES => [
'tripId' => '26e185b9-a233-470e-b2d4-2818908a075f',
'dateTo' => 1680360081,
'dateFrom' => '<(new DateTimeImutable())->getTimestamp()>',
],
],
[
self::PARAMS_VALUES => [
'tripId' => '26e185b9-a233-470e-b2d4-2818908a075f',
'levelOne' => [
'dateTo' => 1680360081,
'dateFrom' => '<(DateTimeImmutable)->getTimestamp()>',
],
],
],
[
self::PARAMS_VALUES => [
'tripId' => '26e185b9-a233-470e-b2d4-2818908a075f',
'levelOne' => [
'levelTwo' => [
'dateTo' => 1680360081,
'dateFrom' => '<(ne DateTimeImmutable())->getTimestamp()>',
]
],
],
],
[
self::PARAMS_VALUES => [
'tripId' => '26e185b9-a233-470e-b2d4-2818908a075f',
'dateTo' => 1680360081,
'dateFrom' => '<>',
],
],
];
}
}