BodyMatchers
1 year ago
CoreTestCase.php
1 year ago
HeadersMatcher.php
1 year ago
StatusCodeMatcher.php
1 year ago
TestParam.php
1 year ago
TestParam.php
84 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Core\TestCase; |
| 6 | |
| 7 | use apimatic\jsonmapper\JsonMapperException; |
| 8 | use Closure; |
| 9 | use Core\Client; |
| 10 | use Core\Types\Sdk\CoreFileWrapper; |
| 11 | use Core\Utils\CoreHelper; |
| 12 | use Exception; |
| 13 | |
| 14 | class TestParam |
| 15 | { |
| 16 | /** |
| 17 | * Returns a typeGroup type TestParam. |
| 18 | * |
| 19 | * @param string $json Json value to be mapped by the typeGroup |
| 20 | * @param string $typeGroup Group of types in string format i.e. oneOf(...), anyOf(...) |
| 21 | * @param array $deserializers Methods required for the de-serialization of specific types in |
| 22 | * in the provided typeGroup, should be an array in the format: |
| 23 | * ['path/to/method returnType', ...]. Default: [] |
| 24 | * @return mixed Returns the mapped value from json |
| 25 | * @throws JsonMapperException |
| 26 | */ |
| 27 | public static function typeGroup(string $json, string $typeGroup, array $deserializers = []) |
| 28 | { |
| 29 | return Client::getJsonHelper()->mapTypes(CoreHelper::deserialize($json, false), $typeGroup, $deserializers); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Returns an object type TestParam. |
| 34 | * |
| 35 | * @param string $json Json value to be mapped by the class |
| 36 | * @param string|null $classname Name of the class inclusive of its namespace, |
| 37 | * Default: object |
| 38 | * @param int $dimension Greater than 0 if trying to map an array of |
| 39 | * class with some dimensions, Default: 0 |
| 40 | * @return mixed Returns the mapped value from json |
| 41 | * @throws Exception |
| 42 | */ |
| 43 | public static function object(string $json, ?string $classname = null, int $dimension = 0) |
| 44 | { |
| 45 | if (is_null($classname)) { |
| 46 | return CoreHelper::deserialize($json); |
| 47 | } |
| 48 | return Client::getJsonHelper()->mapClass(CoreHelper::deserialize($json, false), $classname, $dimension); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns a custom TestParam. |
| 53 | * |
| 54 | * @param string $json Json value to be deserialized using custom callback |
| 55 | * @param callable $callback Callback use to deserialize the given value |
| 56 | * @return mixed Returns the result from the callback |
| 57 | */ |
| 58 | public static function custom(string $json, callable $callback) |
| 59 | { |
| 60 | return Closure::fromCallable($callback)(CoreHelper::deserialize($json, false)); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns a file type TestParam. |
| 65 | * |
| 66 | * @param string $url URL of the file to download |
| 67 | */ |
| 68 | public static function file(string $url) |
| 69 | { |
| 70 | $realPath = CoreFileWrapper::getDownloadedRealFilePath($url); |
| 71 | return self::localFile($realPath); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns a localFile TestParam. |
| 76 | * |
| 77 | * @param string $realPath Local path to the file |
| 78 | */ |
| 79 | public static function localFile(string $realPath) |
| 80 | { |
| 81 | return Client::getConverter()->createFileWrapper($realPath, null, ''); |
| 82 | } |
| 83 | } |
| 84 |