1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Renderer\Helper; |
4
|
|
|
|
5
|
|
|
use PhpUnitGen\Model\PropertyInterface\TypeInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ValueHelper. |
9
|
|
|
* |
10
|
|
|
* @author Paul Thébaud <[email protected]>. |
11
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
12
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
13
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
14
|
|
|
* @since Class available since Release 2.0.0. |
15
|
|
|
*/ |
16
|
|
|
class ValueHelper |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Generate a PHP value as a string for the given type (bool, int ...). |
20
|
|
|
* |
21
|
|
|
* @param int|null $type The type to generate value for (from TypeInterface::*). |
22
|
|
|
* @param string|null $customType The custom type as a string if exists. |
23
|
|
|
* |
24
|
|
|
* @return string The generated PHP value string. |
25
|
|
|
*/ |
26
|
|
|
public function invoke(?int $type = null, ?string $customType = null): string |
27
|
|
|
{ |
28
|
|
|
switch (true) { |
29
|
|
|
case TypeInterface::CUSTOM === $type: |
30
|
|
|
return sprintf('$this->createMock(%s::class)', $customType); |
31
|
|
|
case TypeInterface::OBJECT === $type: |
32
|
|
|
return '$this->createMock(\\DateTime::class)'; |
33
|
|
|
case TypeInterface::BOOL === $type: |
34
|
|
|
return 'true'; |
35
|
|
|
case TypeInterface::INT === $type: |
36
|
|
|
return '42'; |
37
|
|
|
case TypeInterface::FLOAT === $type: |
38
|
|
|
return '42.42'; |
39
|
|
|
case TypeInterface::ARRAY === $type: |
40
|
|
|
case TypeInterface::ITERABLE === $type: |
41
|
|
|
return '["a", "strings", "array"]'; |
42
|
|
|
case TypeInterface::CALLABLE === $type: |
43
|
|
|
return 'function(): void {/* A callable */}'; |
44
|
|
|
case TypeInterface::STRING === $type: |
45
|
|
|
case TypeInterface::MIXED === $type: |
46
|
|
|
return '"a string to test"'; |
47
|
|
|
default: |
48
|
|
|
return '/** @todo Insert a value matching type here */'; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|