|
1
|
|
|
<?php |
|
2
|
|
|
/******************************************************************************* |
|
3
|
|
|
* This file is part of the GraphQL Bundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) YnloUltratech <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
******************************************************************************/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Behat\Transformer; |
|
12
|
|
|
|
|
13
|
|
|
use Behat\Behat\Definition\Call\DefinitionCall; |
|
14
|
|
|
use Behat\Behat\Transformation\Transformer\ArgumentTransformer; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Transform any value to literal string, |
|
18
|
|
|
* can be used to escape scalar types like int or bool |
|
19
|
|
|
* |
|
20
|
|
|
* @example '2' => "2" |
|
21
|
|
|
* @example "'2'" => "2" |
|
22
|
|
|
* @example "'true'" => "true" |
|
23
|
|
|
* @example "'{faker.randomNumber}'" => "5531" |
|
24
|
|
|
*/ |
|
25
|
|
|
class TransformAnyValueToString implements ArgumentTransformer |
|
26
|
|
|
{ |
|
27
|
|
|
private const PATTERN = '/^\'.*\'$/'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var TransformStringToExpression |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $expressionTransformer; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* TransformAnyValueToString constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param TransformStringToExpression $expressionTransformer |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(TransformStringToExpression $expressionTransformer) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->expressionTransformer = $expressionTransformer; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function supportsDefinitionAndArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue) |
|
45
|
|
|
{ |
|
46
|
|
|
return preg_match(self::PATTERN, $argumentValue); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function transformArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue) |
|
50
|
|
|
{ |
|
51
|
|
|
if (preg_match('/^\'({.*})\'$/', $argumentValue, $matches)) { |
|
52
|
|
|
return (string) $this->expressionTransformer->transformArgument($definitionCall, $argumentIndex, $matches[1]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return (string) $argumentValue; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: