Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
CRAP | |
63.64% |
7 / 11 |
| AbstractJsonPathProvider | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6.20 | |
63.64% |
7 / 11 |
| __construct | |
0.00% |
0 / 1 |
6.20 | |
63.64% |
7 / 11 |
|||
| <?php | |
| namespace Alxvng\QATracker\DataProvider; | |
| use JsonException; | |
| use Symfony\Component\Filesystem\Filesystem; | |
| abstract class AbstractJsonPathProvider implements DataProviderInterface | |
| { | |
| public const MIME_TYPES = [ | |
| 'application/json', | |
| 'text/plain', | |
| ]; | |
| protected string $inputFilePath; | |
| protected string $jsonPathQuery; | |
| /** | |
| * XpathProvider constructor. | |
| * | |
| * @param string $baseDir | |
| * @param string $inputFilePath | |
| * @param string $jsonPathQuery | |
| */ | |
| public function __construct(string $baseDir, string $inputFilePath, string $jsonPathQuery) | |
| { | |
| $fs = new Filesystem(); | |
| $this->inputFilePath = $fs->isAbsolutePath($inputFilePath) ? $inputFilePath : $baseDir.'/'.$inputFilePath; | |
| $this->jsonPathQuery = $jsonPathQuery; | |
| if (!file_exists($this->inputFilePath)) { | |
| throw new \RuntimeException(sprintf('Unable to find file at %s', $this->inputFilePath)); | |
| } | |
| if (!in_array(mime_content_type($this->inputFilePath), static::MIME_TYPES, true)) { | |
| throw new \RuntimeException(sprintf('The file %s (%s) must have one the mime types : %s', $this->inputFilePath, mime_content_type($this->inputFilePath), implode(',', static::MIME_TYPES))); | |
| } | |
| try { | |
| json_decode(file_get_contents($this->inputFilePath), true, 512, JSON_THROW_ON_ERROR); | |
| } catch (JsonException $e) { | |
| throw new \RuntimeException(sprintf('The file %s seems does not contain valid json data', $this->inputFilePath)); | |
| } | |
| } | |
| } |