From 29a6f41ed85c804420f8527c35843d79f6293fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Nov=C3=A1k?= Date: Tue, 30 Jul 2024 10:05:23 +0200 Subject: [PATCH] PST-1908 update dependencies and PHP, add phpstan baseline --- Dockerfile | 2 +- composer.json | 11 +- phpcs.xml | 8 +- phpstan-baseline.neon | 151 +++++++++++++++++++ phpstan.neon | 4 +- src/Analyzer.php | 15 +- src/Cache.php | 11 +- src/CsvRow.php | 16 +- src/Exception/InconsistentValueException.php | 4 +- src/Exception/JsonParserException.php | 6 +- src/NodePath.php | 3 +- src/Parser.php | 38 +++-- src/Structure.php | 31 ++-- tests/phpunit/AnalyzerTest.php | 45 +++--- tests/phpunit/HeaderConflictsTest.php | 45 +++--- tests/phpunit/HeadersParentTest.php | 25 +-- tests/phpunit/HeadersTest.php | 29 ++-- tests/phpunit/ParserTest.php | 99 ++++++------ tests/phpunit/ParserTestCase.php | 8 +- tests/phpunit/RealDataTest.php | 34 +++-- tests/phpunit/StructureTest.php | 36 ++--- 21 files changed, 407 insertions(+), 214 deletions(-) create mode 100644 phpstan-baseline.neon diff --git a/Dockerfile b/Dockerfile index abf5b8f..242c98e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM php:7.4-cli +FROM php:8.1-cli ARG COMPOSER_FLAGS="--prefer-dist --no-interaction" ARG DEBIAN_FRONTEND=noninteractive diff --git a/composer.json b/composer.json index 4efb66a..9593e1c 100755 --- a/composer.json +++ b/composer.json @@ -10,16 +10,16 @@ } ], "require": { - "php": ">=7.4", + "php": "^8.1", "ext-json": "*", - "keboola/php-csvtable": "^1.1.1", + "keboola/php-csvtable": "~2.1.0", "keboola/php-temp": "^2.0", "keboola/php-utils": "^4.1", "monolog/monolog": "^2.2" }, "require-dev": { "phpunit/phpunit": "^9.0", - "phpstan/phpstan": "^0.12.14", + "phpstan/phpstan": "^1.0", "php-parallel-lint/php-parallel-lint": "^1.2", "keboola/coding-standard": ">=9.0" }, @@ -56,6 +56,9 @@ }, "config": { "sort-packages": true, - "optimize-autoloader": true + "optimize-autoloader": true, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/phpcs.xml b/phpcs.xml index 67e37cf..1006ac6 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,4 +1,10 @@ - + + */ + + + */ + + \ No newline at end of file diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..0967323 --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,151 @@ +parameters: + ignoreErrors: + - + message: "#^Binary operation \"\\*\" between int\\|string and 0\\.25 results in an error\\.$#" + count: 1 + path: src/Cache.php + + - + message: "#^Method Keboola\\\\Json\\\\Cache\\:\\:getNext\\(\\) should return array\\|null but returns mixed\\.$#" + count: 2 + path: src/Cache.php + + - + message: "#^Property Keboola\\\\Json\\\\Cache\\:\\:\\$temp \\(resource\\) in empty\\(\\) is not falsy\\.$#" + count: 3 + path: src/Cache.php + + - + message: "#^PHPDoc tag @var above a method has no effect\\.$#" + count: 1 + path: src/CsvRow.php + + - + message: "#^Part \\$value \\(mixed\\) of encapsed string cannot be cast to string\\.$#" + count: 1 + path: src/CsvRow.php + + - + message: "#^Method Keboola\\\\Json\\\\NodePath\\:\\:getLast\\(\\) should return string but returns string\\|false\\.$#" + count: 1 + path: src/NodePath.php + + - + message: "#^Cannot access offset string on mixed\\.$#" + count: 1 + path: src/Parser.php + + - + message: "#^Parameter \\#1 \\$column of method Keboola\\\\Json\\\\CsvRow\\:\\:setValue\\(\\) expects string, mixed given\\.$#" + count: 4 + path: src/Parser.php + + - + message: "#^Parameter \\#1 \\$data of method Keboola\\\\Json\\\\Parser\\:\\:parse\\(\\) expects array, mixed given\\.$#" + count: 1 + path: src/Parser.php + + - + message: "#^Parameter \\#1 \\$path of class Keboola\\\\Json\\\\NodePath constructor expects array\\, array\\ given\\.$#" + count: 1 + path: src/Parser.php + + - + message: "#^Parameter \\#3 \\$parentId of method Keboola\\\\Json\\\\Parser\\:\\:parse\\(\\) expects array\\|string\\|null, mixed given\\.$#" + count: 1 + path: src/Parser.php + + - + message: "#^Right side of \\|\\| is always false\\.$#" + count: 1 + path: src/Parser.php + + - + message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#" + count: 1 + path: src/Structure.php + + - + message: "#^Cannot access offset string on mixed\\.$#" + count: 2 + path: src/Structure.php + + - + message: "#^Cannot assign offset string to string\\.$#" + count: 1 + path: src/Structure.php + + - + message: "#^Offset string on string in isset\\(\\) does not exist\\.$#" + count: 2 + path: src/Structure.php + + - + message: "#^PHPDoc tag @var above a method has no effect\\.$#" + count: 1 + path: src/Structure.php + + - + message: "#^Parameter \\#1 \\$definitions of method Keboola\\\\Json\\\\Structure\\:\\:validateDefinitions\\(\\) expects array, mixed given\\.$#" + count: 1 + path: src/Structure.php + + - + message: "#^Parameter \\#1 \\$nodeName of method Keboola\\\\Json\\\\Structure\\:\\:decodeNodeName\\(\\) expects string, mixed given\\.$#" + count: 1 + path: src/Structure.php + + - + message: "#^Parameter \\#2 \\$data of method Keboola\\\\Json\\\\Structure\\:\\:storeValue\\(\\) expects array, mixed given\\.$#" + count: 1 + path: src/Structure.php + + - + message: "#^Parameter \\#3 \\$newType of method Keboola\\\\Json\\\\Structure\\:\\:handleUpgrade\\(\\) expects string, mixed given\\.$#" + count: 1 + path: src/Structure.php + + - + message: "#^Part \\$value \\(mixed\\) of encapsed string cannot be cast to string\\.$#" + count: 1 + path: src/Structure.php + + - + message: "#^Cannot access property \\$data on mixed\\.$#" + count: 1 + path: tests/phpunit/ParserTest.php + + - + message: "#^Parameter \\#1 \\$data of method Keboola\\\\Json\\\\Parser\\:\\:process\\(\\) expects array, mixed given\\.$#" + count: 2 + path: tests/phpunit/ParserTest.php + + - + message: "#^Cannot access offset 0 on mixed\\.$#" + count: 1 + path: tests/phpunit/RealDataTest.php + + - + message: "#^Cannot access property \\$components on mixed\\.$#" + count: 1 + path: tests/phpunit/RealDataTest.php + + - + message: "#^Cannot access property \\$statuses on mixed\\.$#" + count: 1 + path: tests/phpunit/RealDataTest.php + + - + message: "#^Parameter \\#1 \\$array of function array_walk expects array\\|object, array\\\\|false given\\.$#" + count: 2 + path: tests/phpunit/RealDataTest.php + + - + message: "#^Parameter \\#1 \\$data of method Keboola\\\\Json\\\\Parser\\:\\:process\\(\\) expects array, mixed given\\.$#" + count: 5 + path: tests/phpunit/RealDataTest.php + + - + message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#" + count: 4 + path: tests/phpunit/RealDataTest.php diff --git a/phpstan.neon b/phpstan.neon index 6bb3be1..bc6619c 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,8 +1,10 @@ +includes: + - phpstan-baseline.neon + parameters: checkMissingIterableValueType: false ignoreErrors: - '#json_decode expects string, string\|false given#' - - '#Offset .* does not exist on array\|null#' - '#Cannot access offset 1 on array\|false#' - '#Parameter .* of (function|method) .* expects array, array.*\|.* given#' - '#Parameter .* of function fgetcsv expects resource, resource\|false given.#' diff --git a/src/Analyzer.php b/src/Analyzer.php index b9a33f9..a14db91 100644 --- a/src/Analyzer.php +++ b/src/Analyzer.php @@ -21,7 +21,7 @@ public function __construct( LoggerInterface $logger, Structure $structure, bool $nestedArraysAsJson = false, - bool $strict = false + bool $strict = false, ) { $this->nestedArrayAsJson = $nestedArraysAsJson; $this->strict = $strict; @@ -39,6 +39,9 @@ public function getLogger(): LoggerInterface return $this->log; } + /** + * @param mixed[] $data + */ public function analyzeData(array $data, string $rootType): void { if (empty($data)) { @@ -49,10 +52,7 @@ public function analyzeData(array $data, string $rootType): void $this->structure->saveNodeValue($path, 'nodeType', 'array'); } - /** - * @param mixed $item - */ - private function analyzeItem($item, NodePath $nodePath): string + private function analyzeItem(mixed $item, NodePath $nodePath): string { if (is_scalar($item)) { if ($this->strict) { @@ -85,6 +85,9 @@ private function analyzeItem($item, NodePath $nodePath): string return $nodeType; } + /** + * @param mixed[] $array + */ private function analyzeArray(array $array, NodePath $nodePath): void { $oldType = null; @@ -114,7 +117,7 @@ private function checkType(?string $oldType, string $newType, NodePath $nodePath { if (!is_null($oldType) && ($newType !== $oldType) && ($newType !== 'null') && ($oldType !== 'null')) { throw new JsonParserException( - "Data in '$nodePath' contains incompatible data types '$oldType' and '$newType'." + "Data in '$nodePath' contains incompatible data types '$oldType' and '$newType'.", ); } return $newType; diff --git a/src/Cache.php b/src/Cache.php index 9f6d335..e4daeff 100755 --- a/src/Cache.php +++ b/src/Cache.php @@ -4,8 +4,11 @@ namespace Keboola\Json; +use function Keboola\Utils\returnBytes; + class Cache { + /** @var mixed[] */ protected array $data = []; /** @@ -18,12 +21,15 @@ class Cache protected ?int $memoryLimit = null; + /** + * @param mixed[] $data + */ public function store(array $data): void { // TODO ensure at least X MB is left free (X should be possible to change -> Parser::getCache()->setMemLimit(X)) // either to stop using memory once X mem is used or once X is left from PHP limit if (ini_get('memory_limit') !== '-1' - && memory_get_usage() > (\Keboola\Utils\returnBytes(ini_get('memory_limit')) * 0.25) + && memory_get_usage() > (returnBytes(ini_get('memory_limit')) * 0.25) || ($this->memoryLimit !== null && memory_get_usage() > $this->memoryLimit) ) { // cache @@ -41,6 +47,9 @@ public function store(array $data): void } } + /** + * @return mixed[]|null + */ public function getNext(): ?array { if (!empty($this->temp) && !feof($this->temp)) { diff --git a/src/CsvRow.php b/src/CsvRow.php index 80e2c04..46648a9 100644 --- a/src/CsvRow.php +++ b/src/CsvRow.php @@ -8,25 +8,30 @@ class CsvRow { + /** + * @var mixed[] + */ protected array $data = []; + /** + * @var mixed[] $columns + */ public function __construct(array $columns) { $this->data = array_fill_keys($columns, null); } /** - * @param mixed $value * @throws JsonParserException */ - public function setValue(string $column, $value): void + public function setValue(string $column, mixed $value): void { if (!array_key_exists($column, $this->data)) { throw new JsonParserException( "Error assigning '{$value}' to a non-existing column '{$column}'!", [ 'columns' => array_keys($this->data), - ] + ], ); } @@ -36,13 +41,16 @@ public function setValue(string $column, $value): void [ 'type' => gettype($value), 'value' => json_encode($value), - ] + ], ); } $this->data[$column] = $value; } + /** + * @return mixed[] + */ public function getRow(): array { return $this->data; diff --git a/src/Exception/InconsistentValueException.php b/src/Exception/InconsistentValueException.php index bfb4298..6a2da20 100644 --- a/src/Exception/InconsistentValueException.php +++ b/src/Exception/InconsistentValueException.php @@ -4,6 +4,8 @@ namespace Keboola\Json\Exception; -class InconsistentValueException extends \Exception +use Exception; + +class InconsistentValueException extends Exception { } diff --git a/src/Exception/JsonParserException.php b/src/Exception/JsonParserException.php index 5542cd7..9952ea2 100755 --- a/src/Exception/JsonParserException.php +++ b/src/Exception/JsonParserException.php @@ -5,10 +5,14 @@ namespace Keboola\Json\Exception; use Keboola\Utils\Exception; +use Throwable; class JsonParserException extends Exception { - public function __construct(string $message = '', array $data = [], int $code = 0, ?\Throwable $previous = null) + /** + * @param array $data + */ + public function __construct(string $message = '', array $data = [], int $code = 0, ?Throwable $previous = null) { parent::__construct($message, $code, $previous); $this->setData($data); diff --git a/src/NodePath.php b/src/NodePath.php index 143653c..51c6edf 100644 --- a/src/NodePath.php +++ b/src/NodePath.php @@ -6,10 +6,11 @@ class NodePath { + /** @var array */ private array $path; /** - * NodePath constructor. + * @param array $path */ public function __construct(array $path) { diff --git a/src/Parser.php b/src/Parser.php index 84b15cf..621930b 100755 --- a/src/Parser.php +++ b/src/Parser.php @@ -5,9 +5,10 @@ namespace Keboola\Json; use Keboola\CsvTable\Table; -use Keboola\Temp\Temp; use Keboola\Json\Exception\JsonParserException; use Keboola\Json\Exception\NoDataException; +use Keboola\Temp\Temp; +use stdClass; /** * JSON to CSV data analyzer and parser/converter @@ -46,6 +47,7 @@ class Parser private Temp $temp; + /** @var string[] */ private array $primaryKeys = []; private Analyzer $analyzer; @@ -84,7 +86,7 @@ public function __destruct() * which will name the column(s) by array key provided * @throws NoDataException */ - public function process(array $data, string $type = 'root', $parentId = null): void + public function process(array $data, string $type = 'root', string|array|null $parentId = null): void { if (empty($data) || $data === [null]) { throw new NoDataException("Empty data set received for '{$type}'", [ @@ -101,9 +103,8 @@ public function process(array $data, string $type = 'root', $parentId = null): v /** * Parse data of known type - * @param string|array|null $parentId */ - private function parse(array $data, NodePath $nodePath, $parentId = null): void + private function parse(array $data, NodePath $nodePath, string|array|null $parentId = null): void { $parentId = $this->validateParentId($parentId); $csvFile = $this->createCsvFile($this->structure->getTypeFromNodePath($nodePath), $nodePath, $parentId); @@ -133,15 +134,14 @@ private function parse(array $data, NodePath $nodePath, $parentId = null): void * If the row contains an array, it's recursively parsed * * @param \stdClass $dataRow Input data - * @param NodePath $nodePath * @param array $parentCols to inject parent columns, which aren't part of $this->struct * @param string $outerObjectHash Outer object hash to distinguish different parents in deep nested arrays */ private function parseRow( - \stdClass $dataRow, + stdClass $dataRow, NodePath $nodePath, array $parentCols = [], - ?string $outerObjectHash = null + ?string $outerObjectHash = null, ): CsvRow { $csvRow = new CsvRow($this->getHeaders($nodePath, $parentCols)); // Generate parent ID for arrays @@ -158,12 +158,12 @@ private function parseRow( * Handle the actual write to CsvRow */ private function parseField( - \stdClass $dataRow, + stdClass $dataRow, CsvRow $csvRow, string $arrayParentId, string $column, string $dataType, - NodePath $nodePath + NodePath $nodePath, ): void { // A hack allowing access to numeric keys in object if (!isset($dataRow->{$column}) @@ -199,7 +199,7 @@ private function parseField( $this->parse( $dataRow->{$column}, $nodePath->addChild($column)->addChild(Structure::ARRAY_NAME), - $arrayParentId + $arrayParentId, ); break; case 'object': @@ -223,7 +223,7 @@ private function parseField( $this->analyzer->getLogger()->error( "Data parse error in '{$column}' - unexpected '" . gettype($dataRow->{$column}) . "' where '{$dataType}' was expected!", - [ 'data' => $jsonColumn, 'row' => json_encode($dataRow) ] + [ 'data' => $jsonColumn, 'row' => json_encode($dataRow) ], ); $sf = $this->structure->getNodeProperty($nodePath->addChild($column), 'headerNames'); $csvRow->setValue($sf, $jsonColumn); @@ -291,7 +291,7 @@ private function getHeaders(NodePath $nodePath, ?array &$parent = null): array $nodeName = $this->structure->decodeNodeName($nodeName); $ch = $this->getHeaders($nodePath->addChild($nodeName), $pparent); $headers = array_merge($headers, $ch); - } else if (is_array($data)) { + } elseif (is_array($data)) { $headers[] = $data['headerNames']; } } @@ -308,7 +308,7 @@ private function createCsvFile(string $type, NodePath $nodePath, array &$parentI $type, $this->getHeaders($nodePath, $parentId), true, - $this->temp + $this->temp, ); $this->csvFiles[$type]->addAttributes(['fullDisplayName' => $type]); if (!empty($this->primaryKeys[$type])) { @@ -319,7 +319,7 @@ private function createCsvFile(string $type, NodePath $nodePath, array &$parentI return $this->csvFiles[$type]; } - private function getPrimaryKeyValue(\stdClass $dataRow, NodePath $nodePath, ?string $outerObjectHash = null): string + private function getPrimaryKeyValue(stdClass $dataRow, NodePath $nodePath, ?string $outerObjectHash = null): string { $column = $this->structure->getTypeFromNodePath($nodePath); if (!empty($this->primaryKeys[$column])) { @@ -332,7 +332,7 @@ private function getPrimaryKeyValue(\stdClass $dataRow, NodePath $nodePath, ?str $this->analyzer->getLogger()->warning( "Primary key for type '{$column}' was set to '". $this->primaryKeys[$column] . "', but its column '{$pKeyCol}' does not exist! Using hash to link child objects instead.", - ['row' => $dataRow] + ['row' => $dataRow], ); } else { $values[] = $dataRow->{$pKeyCol}; @@ -349,10 +349,9 @@ private function getPrimaryKeyValue(\stdClass $dataRow, NodePath $nodePath, ?str /** * Ensure the parentId array is not multidimensional * - * @param string|array|null $parentId * @throws JsonParserException */ - private function validateParentId($parentId): array + private function validateParentId(string|array|null $parentId): array { if (!empty($parentId)) { if (is_array($parentId)) { @@ -361,7 +360,7 @@ private function validateParentId($parentId): array 'Error assigning parentId to a CSV file! $parentId array cannot be multidimensional.', [ 'parentId' => $parentId, - ] + ], ); } } else { @@ -407,9 +406,8 @@ public function addPrimaryKeys(array $pks): void /** * Set maximum memory used before Cache starts using php://temp - * @param string|int $limit */ - public function setCacheMemoryLimit($limit): void + public function setCacheMemoryLimit(string|int $limit): void { $this->cache->setMemoryLimit((int) $limit); } diff --git a/src/Structure.php b/src/Structure.php index 347b6d1..da0aecc 100644 --- a/src/Structure.php +++ b/src/Structure.php @@ -36,6 +36,7 @@ class Structure /** * Allowed data types + * @var string[] */ private static array $nodeDataTypes = [ 'null', 'array', 'object', 'scalar', 'string', 'integer', 'double', 'boolean', @@ -43,11 +44,18 @@ class Structure /** * Allowed node types + * @var string[] */ private static array $nodeTypes = ['parent']; + /** + * @var mixed[] + */ private array $data = []; + /** + * @var string[] + */ private array $headerIndex = []; private bool $autoUpgradeToArray; @@ -74,7 +82,7 @@ public function __construct(bool $autoUpgradeToArray = true) * @param mixed $value Scalar value. * @throws InconsistentValueException */ - public function saveNodeValue(NodePath $nodePath, string $property, $value): void + public function saveNodeValue(NodePath $nodePath, string $property, mixed $value): void { try { $this->data = $this->storeValue($nodePath, $this->data, $property, $value); @@ -115,13 +123,13 @@ public function decodeNodeName(string $nodeName): string /** * Store a particular value of a particular property for a given node. * @param NodePath $nodePath Node Path - * @param array $data Structure data. + * @param mixed[] $data Structure data. * @param string $property Name of the property (e.g. 'nodeType') * @param mixed $value Scalar value of the property. - * @return array Structure data + * @return mixed[] Structure data * @throws InconsistentValueException In case the values is already set and not same. */ - private function storeValue(NodePath $nodePath, array $data, string $property, $value): array + private function storeValue(NodePath $nodePath, array $data, string $property, mixed $value): array { $nodeName = ''; $nodePath = $nodePath->popFirst($nodeName); @@ -144,9 +152,7 @@ private function storeValue(NodePath $nodePath, array $data, string $property, $ /** * Upgrade a node to array - * @param array $node - * @param NodePath $nodePath - * @param string $newType + * @var string[] $node * @throws JsonParserException */ private function handleUpgrade(array $node, NodePath $nodePath, string $newType): void @@ -184,7 +190,7 @@ private function handleUpgrade(array $node, NodePath $nodePath, string $newType) } else { throw new JsonParserException( 'Unhandled nodeType change from "' . $node['nodeType'] . - '" to "' . $newType . '" in "' . $nodePath->__toString() . '"' + '" to "' . $newType . '" in "' . $nodePath->__toString() . '"', ); } } @@ -293,7 +299,7 @@ public function getNode(NodePath $nodePath, ?array $data = null): ?array * @param string $property Property name (e.g. 'nodeType') * @return mixed Property value or null if the node or property does not exist. */ - public function getNodeProperty(NodePath $nodePath, string $property) + public function getNodeProperty(NodePath $nodePath, string $property): mixed { $data = $this->getNode($nodePath); if (isset($data[$property])) { @@ -338,7 +344,6 @@ private function getNodeChildrenProperties(NodePath $nodePath, string $property) /** * Get columns from a node and return their data types. - * @param NodePath $nodePath * @return array Index is column name, value is data type. */ public function getColumnTypes(NodePath $nodePath): array @@ -368,7 +373,7 @@ public function generateHeaderNames(): void $nodeData, new NodePath([$baseType, $nodeName]), self::ARRAY_NAME, // # root is always array - $baseType + $baseType, ); } } @@ -463,9 +468,6 @@ private function getUniqueName(string $baseType, string $headerName): string /** * Generate header name for a node and sub-nodes. * @param array $data Node data - * @param NodePath $nodePath - * @param string $parentName - * @param string $baseType */ private function generateHeaderName(array &$data, NodePath $nodePath, string $parentName, string $baseType): void { @@ -501,7 +503,6 @@ private function generateHeaderName(array &$data, NodePath $nodePath, string $pa /** * Load structure data. - * @param array $definitions */ public function load(array $definitions): void { diff --git a/tests/phpunit/AnalyzerTest.php b/tests/phpunit/AnalyzerTest.php index 4e3c1f8..fb421e0 100755 --- a/tests/phpunit/AnalyzerTest.php +++ b/tests/phpunit/AnalyzerTest.php @@ -9,6 +9,7 @@ use Keboola\Json\Structure; use PHPUnit\Framework\TestCase; use Psr\Log\NullLogger; +use stdClass; class AnalyzerTest extends TestCase { @@ -70,7 +71,7 @@ public function testAnalyzeExperimental(): void ], 'parent_aliases' => [], ], - $analyzer->getStructure()->getData() + $analyzer->getStructure()->getData(), ); } @@ -133,7 +134,7 @@ public function testAnalyze(): void ], 'parent_aliases' => [], ], - $analyzer->getStructure()->getData() + $analyzer->getStructure()->getData(), ); } @@ -207,7 +208,7 @@ public function testAnalyzeComplex(): void ], 'parent_aliases' => [], ], - $analyzer->getStructure()->getData() + $analyzer->getStructure()->getData(), ); } @@ -266,7 +267,7 @@ public function testAnalyzeConflict(): void ], 'parent_aliases' => [], ], - $analyzer->getStructure()->getData() + $analyzer->getStructure()->getData(), ); } @@ -323,7 +324,7 @@ public function testAnalyzeStrict(): void ], 'parent_aliases' => [], ], - $analyzer->getStructure()->getData() + $analyzer->getStructure()->getData(), ); } @@ -402,7 +403,7 @@ public function testAnalyzeAutoArrays(): void ], 'parent_aliases' => [], ], - $analyzer->getStructure()->getData() + $analyzer->getStructure()->getData(), ); } @@ -432,7 +433,7 @@ public function testAnalyzeAutoArraysError(): void $this->expectException(JsonParserException::class); $this->expectExceptionMessage( - "Data array in 'root.[].arrOfScalars' contains incompatible types 'object' and 'scalar'" + "Data array in 'root.[].arrOfScalars' contains incompatible types 'object' and 'scalar'", ); $analyzer->analyzeData($data, 'root'); } @@ -459,7 +460,7 @@ public function testAnalyzeBadData(): void public function testAnalyzeEmpty(): void { $analyzer = new Analyzer(new NullLogger(), new Structure()); - $analyzer->analyzeData([new \stdClass], 'test'); + $analyzer->analyzeData([new stdClass], 'test'); self::assertEquals( [ @@ -473,7 +474,7 @@ public function testAnalyzeEmpty(): void ], 'parent_aliases' => [], ], - $analyzer->getStructure()->getData() + $analyzer->getStructure()->getData(), ); } @@ -481,7 +482,7 @@ public function testAnalyzeRowEmpty(): void { $analyzer = new Analyzer(new NullLogger(), new Structure()); $analyzer->analyzeData([ - new \stdClass, + new stdClass, (object) [ 'k' => 'v', 'field' => [ @@ -511,7 +512,7 @@ public function testAnalyzeRowEmpty(): void ], 'parent_aliases' => [], ], - $analyzer->getStructure()->getData() + $analyzer->getStructure()->getData(), ); } @@ -556,7 +557,7 @@ public function testAnalyzeKnownArray(): void ], 'parent_aliases' => [], ], - $analyzer->getStructure()->getData() + $analyzer->getStructure()->getData(), ); } @@ -679,7 +680,7 @@ public function testAnalyzeEmptyArrayOfObject(): void ], 'parent_aliases' => [], ], - $analyzer->getStructure()->getData() + $analyzer->getStructure()->getData(), ); } @@ -734,7 +735,7 @@ public function testAnalyzeEmptyArrayOfObjectAutoUpgrade(): void ], 'parent_aliases' => [], ], - $analyzer->getStructure()->getData() + $analyzer->getStructure()->getData(), ); } @@ -752,7 +753,7 @@ public function testArrayOfNull(): void 'obj' => [null], ], ], - 's2null' + 's2null', ); $analyzer->analyzeData( @@ -766,7 +767,7 @@ public function testArrayOfNull(): void 'obj' => [null], ], ], - 'null2s' + 'null2s', ); self::assertEquals( @@ -817,7 +818,7 @@ public function testArrayOfNull(): void ], 'parent_aliases' => [], ], - $analyzer->getStructure()->getData() + $analyzer->getStructure()->getData(), ); } @@ -829,7 +830,7 @@ public function testUnsupportedNestingStrict(): void [1,2,3,[7,8]], [4,5,6], ], - 'test' + 'test', ); self::assertEquals( [ @@ -843,7 +844,7 @@ public function testUnsupportedNestingStrict(): void ], 'parent_aliases' => [], ], - $analyzer->getStructure()->getData() + $analyzer->getStructure()->getData(), ); } @@ -855,7 +856,7 @@ public function testUnsupportedNestingNoStrict(): void [1,2,3,[7,8]], [4,5,6], ], - 'test' + 'test', ); self::assertEquals( [ @@ -869,7 +870,7 @@ public function testUnsupportedNestingNoStrict(): void ], 'parent_aliases' => [], ], - $analyzer->getStructure()->getData() + $analyzer->getStructure()->getData(), ); } @@ -920,7 +921,7 @@ public function testAnalyzeTypesStrict(): void ], 'parent_aliases' => [], ], - $analyzer->getStructure()->getData() + $analyzer->getStructure()->getData(), ); } } diff --git a/tests/phpunit/HeaderConflictsTest.php b/tests/phpunit/HeaderConflictsTest.php index 5de4350..32235ad 100644 --- a/tests/phpunit/HeaderConflictsTest.php +++ b/tests/phpunit/HeaderConflictsTest.php @@ -9,6 +9,7 @@ use Keboola\Json\Structure; use PHPUnit\Framework\TestCase; use Psr\Log\NullLogger; +use function Keboola\Utils\jsonDecode; class HeaderConflictsTest extends TestCase { @@ -16,7 +17,7 @@ public function testObjectArrayCombinedConflictObject(): void { $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [{ "first_third_fourth": "origin", @@ -27,7 +28,7 @@ public function testObjectArrayCombinedConflictObject(): void } } }] - }' + }', ); $parser->process($testFile->components); @@ -44,7 +45,7 @@ public function testObjectArrayCombinedConflictArray(): void { $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [{ "first": { @@ -55,7 +56,7 @@ public function testObjectArrayCombinedConflictArray(): void }, "first_second": "origin" }] - }' + }', ); $parser->process($testFile->components); @@ -72,7 +73,7 @@ public function testObjectArrayCombinedConflictParentId(): void { $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [{ "JSON_parentId": "origin", @@ -83,7 +84,7 @@ public function testObjectArrayCombinedConflictParentId(): void } } }] - }' + }', ); $parser->process($testFile->components, 'root', 'someValue'); @@ -100,7 +101,7 @@ public function testObjectArrayCombinedConflictParentIdArray(): void { $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [ { @@ -130,7 +131,7 @@ public function testObjectArrayCombinedConflictParentIdArray(): void } } ] - }' + }', ); $parser->process($testFile->components, 'boo', ['someKey' => 'someValue']); @@ -153,7 +154,7 @@ public function testObjectArrayCombinedMultiConflict(): void { $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [{ "first_second": "origin", @@ -162,7 +163,7 @@ public function testObjectArrayCombinedMultiConflict(): void }, "first.second": "origin2" }] - }' + }', ); $parser->process($testFile->components); @@ -179,7 +180,7 @@ public function testObjectArrayCombinedConflictParentIdArrayMultiBatch(): void { $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [ { @@ -192,12 +193,12 @@ public function testObjectArrayCombinedConflictParentIdArrayMultiBatch(): void } } ] - }' + }', ); $parser->process($testFile->components, 'boo', ['someKey' => 'someValue']); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [ { @@ -218,7 +219,7 @@ public function testObjectArrayCombinedConflictParentIdArrayMultiBatch(): void } } ] - }' + }', ); $parser->process($testFile->components, 'boo', ['someKey' => 'someValue']); @@ -241,7 +242,7 @@ public function testObjectArrayNestedConflictParentIdArrayMultiBatch(): void { $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [ { @@ -256,12 +257,12 @@ public function testObjectArrayNestedConflictParentIdArrayMultiBatch(): void ] } ] - }' + }', ); $parser->process($testFile->components, 'boo', ['someKey' => 'someValue']); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [ { @@ -287,7 +288,7 @@ public function testObjectArrayNestedConflictParentIdArrayMultiBatch(): void ] } ] - }' + }', ); $parser->process($testFile->components, 'boo', ['someKey' => 'someValue']); self::assertEquals(['boo', 'boo_first'], array_keys($parser->getCsvFiles())); @@ -330,7 +331,7 @@ public function testObjectArrayCombinedConflictParentIdMetadata(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure()), $metadata); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [ { @@ -338,12 +339,12 @@ public function testObjectArrayCombinedConflictParentIdMetadata(): void "column": "test" } ] - }' + }', ); $parser->process($testFile->components, 'components', ['column' => 'someValue']); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [ { @@ -351,7 +352,7 @@ public function testObjectArrayCombinedConflictParentIdMetadata(): void "column": "test2" } ] - }' + }', ); $parser->process($testFile->components, 'components', ['column' => 'someValue']); self::assertEquals(['components'], array_keys($parser->getCsvFiles())); diff --git a/tests/phpunit/HeadersParentTest.php b/tests/phpunit/HeadersParentTest.php index 39d460c..4a00171 100644 --- a/tests/phpunit/HeadersParentTest.php +++ b/tests/phpunit/HeadersParentTest.php @@ -9,6 +9,7 @@ use Keboola\Json\Structure; use PHPUnit\Framework\TestCase; use Psr\Log\NullLogger; +use function Keboola\Utils\jsonDecode; class HeadersParentTest extends TestCase { @@ -17,14 +18,14 @@ public function testObjectNestedArray(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [{ "first": { "second": ["a", "b"] } }] - }' + }', ); $parser->process($testFile->components); $result = "\"first_second\"\n\"root.first_97360eb9d751f9ade2eac71d59bcb37d\"\n"; @@ -39,7 +40,7 @@ public function testObjectArrayCombinedParentId(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [{ "first": { @@ -49,7 +50,7 @@ public function testObjectArrayCombinedParentId(): void } } }] - }' + }', ); $parser->process($testFile->components, 'root', 'someId'); @@ -67,7 +68,7 @@ public function testObjectArrayCombinedParentIdArray(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [{ "first": { @@ -77,7 +78,7 @@ public function testObjectArrayCombinedParentIdArray(): void } } }] - }' + }', ); $parser->process($testFile->components, 'root', ['someId' => 'someValue']); @@ -95,7 +96,7 @@ public function testObjectArrayCombinedTypeParentIdArray(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [{ "first": { @@ -105,7 +106,7 @@ public function testObjectArrayCombinedTypeParentIdArray(): void } } }] - }' + }', ); $parser->process($testFile->components, 'root_first_second', ['someId' => 'someValue']); @@ -117,7 +118,7 @@ public function testObjectArrayCombinedTypeParentIdArray(): void "\"b\",\"root_first_second.first_1c00277aca5b2395406ccaaabc24fbd7\"\n"; self::assertEquals( $result, - file_get_contents($parser->getCsvFiles()['root_first_second_first_second']->getPathName()) + file_get_contents($parser->getCsvFiles()['root_first_second_first_second']->getPathName()), ); } @@ -126,7 +127,7 @@ public function testObjectArrayCombinedTypeInner(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [{ "first": { @@ -136,7 +137,7 @@ public function testObjectArrayCombinedTypeInner(): void } } }] - }' + }', ); $parser->process($testFile->components, 'first_second'); @@ -148,7 +149,7 @@ public function testObjectArrayCombinedTypeInner(): void "\"b\",\"first_second.first_f907b0c59507357e04c8d96eae1acf5c\"\n"; self::assertEquals( $result, - file_get_contents($parser->getCsvFiles()['first_second_first_second']->getPathName()) + file_get_contents($parser->getCsvFiles()['first_second_first_second']->getPathName()), ); } } diff --git a/tests/phpunit/HeadersTest.php b/tests/phpunit/HeadersTest.php index 1ecfa72..45b73c6 100644 --- a/tests/phpunit/HeadersTest.php +++ b/tests/phpunit/HeadersTest.php @@ -10,6 +10,7 @@ use Keboola\Json\Structure; use PHPUnit\Framework\TestCase; use Psr\Log\NullLogger; +use function Keboola\Utils\jsonDecode; class HeadersTest extends TestCase { @@ -17,8 +18,8 @@ public function testEmptyArray(): void { $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( - '{"components": []}' + $testFile = jsonDecode( + '{"components": []}', ); $this->expectException(NoDataException::class); @@ -31,8 +32,8 @@ public function testEmptyObject(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( - '{"components": [{}]}' + $testFile = jsonDecode( + '{"components": [{}]}', ); $parser->process($testFile->components); self::assertEquals(['root'], array_keys($parser->getCsvFiles())); @@ -44,8 +45,8 @@ public function testAlmostEmptyObject(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( - '{"components": [{"a": null}]}' + $testFile = jsonDecode( + '{"components": [{"a": null}]}', ); $parser->process($testFile->components); @@ -58,8 +59,8 @@ public function testLongHeader(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( - '{"components": [{"AReallyTrulyIncrediblyHellishlyLongFromOuterSpaceAndAgePropertyName": null}]}' + $testFile = jsonDecode( + '{"components": [{"AReallyTrulyIncrediblyHellishlyLongFromOuterSpaceAndAgePropertyName": null}]}', ); $parser->process($testFile->components); @@ -72,8 +73,8 @@ public function testObject(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( - '{"components": [{"a": "b"}]}' + $testFile = jsonDecode( + '{"components": [{"a": "b"}]}', ); $parser->process($testFile->components); @@ -86,8 +87,8 @@ public function testArray(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( - '{"components": ["a", "b"]}' + $testFile = jsonDecode( + '{"components": ["a", "b"]}', ); $parser->process($testFile->components); @@ -100,7 +101,7 @@ public function testObjectNested(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [{ "first": { @@ -109,7 +110,7 @@ public function testObjectNested(): void } } }] - }' + }', ); $parser->process($testFile->components); diff --git a/tests/phpunit/ParserTest.php b/tests/phpunit/ParserTest.php index 73aa0fd..08b5b53 100755 --- a/tests/phpunit/ParserTest.php +++ b/tests/phpunit/ParserTest.php @@ -11,6 +11,7 @@ use Monolog\Handler\TestHandler; use Monolog\Logger; use Psr\Log\NullLogger; +use function Keboola\Utils\jsonDecode; class ParserTest extends ParserTestCase { @@ -40,7 +41,7 @@ public function testZeroValues(): void "\"0\",\"entities.hashtags_7166de1f0241156ee048591b4492bc56\"\n" . "\"4\",\"entities.hashtags_7166de1f0241156ee048591b4492bc56\"\n" . "\"\",\"entities.hashtags_7166de1f0241156ee048591b4492bc56\"\n", - file_get_contents($parser->getCsvFiles()['entities_hashtags_indices']->getPathname()) + file_get_contents($parser->getCsvFiles()['entities_hashtags_indices']->getPathname()), ); } @@ -59,7 +60,7 @@ public function testPrimaryKey(): void self::assertEquals('id,date', $parser->getCsvFiles()['root']->getPrimaryKey()); self::assertEquals( '"stuff","root_1;2015-10-21"' . "\n", - file($parser->getCsvFiles()['root_data']->getPathName())[1] + file($parser->getCsvFiles()['root_data']->getPathName())[1], ); } @@ -82,7 +83,7 @@ public function testParentIdPrimaryKey(): void foreach ($parser->getCsvFiles() as $type => $file) { self::assertEquals( file_get_contents($this->getDataDir() . "PrimaryKeyTest/{$type}.csv"), - file_get_contents($file->getPathname()) + file_get_contents($file->getPathname()), ); } } @@ -99,7 +100,7 @@ public function testParentIdPrimaryKeyMultiLevel(): void foreach ($parser->getCsvFiles() as $type => $file) { self::assertEquals( file_get_contents($this->getDataDir() . "PrimaryKeyTest/{$type}.csv"), - file_get_contents($file->getPathname()) + file_get_contents($file->getPathname()), ); } } @@ -121,7 +122,7 @@ public function testParentIdHash(): void foreach ($parser->getCsvFiles() as $type => $file) { self::assertEquals( file_get_contents($this->getDataDir() . "PrimaryKeyTest/{$type}.csv"), - file_get_contents($file->getPathname()) + file_get_contents($file->getPathname()), ); } } @@ -182,7 +183,7 @@ public function testParentIdHashSameValues(): void foreach ($parser->getCsvFiles() as $type => $file) { self::assertEquals( file_get_contents($this->getDataDir() . "{$type}.csv"), - file_get_contents($file->getPathname()) + file_get_contents($file->getPathname()), ); } } @@ -245,7 +246,7 @@ public function testParentIdHashSameValuesDeepNesting(): void foreach ($parser->getCsvFiles() as $type => $file) { self::assertEquals( file_get_contents($this->getDataDir() . "{$type}.csv"), - file_get_contents($file->getPathname()) + file_get_contents($file->getPathname()), ); } } @@ -290,7 +291,7 @@ protected function timeDiffCompare(Parser $parser): void $new = file($files[$later]->getPathname()); self::assertNotEquals( $old, - $new + $new, ); // ditch headers @@ -310,7 +311,7 @@ public function testNoStrictScalarChange(): void { $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var array $data */ - $data = \Keboola\Utils\jsonDecode('[ + $data = jsonDecode('[ {"field": 128}, {"field": "string"}, {"field": true} @@ -324,7 +325,7 @@ public function testNoStrictScalarChange(): void '"string"' . "\n", '"1"' . "\n", // true gets converted to "1"! should be documented! ], - file($parser->getCsvFiles()['threepack']->getPathname()) + file($parser->getCsvFiles()['threepack']->getPathname()), ); } @@ -361,7 +362,7 @@ public function testProcessEmptyObjects(): void . '"16565201397","Comment","457400607","This is the latest comment for this ticket. You also ' . 'changed the ticket status to Pending.","

This is the latest comment for this ticket. You also ' . "changed the ticket status to Pending.

\",\"1\",\"\",\"web\",\"\",\"2013-09-01T20:22:29Z\"\n", - file_get_contents($parser->getCsvFiles()['root']->getPathname()) + file_get_contents($parser->getCsvFiles()['root']->getPathname()), ); } @@ -380,11 +381,11 @@ public function testArrayParentId(): void [ 'first_parent' => 1, 'second_parent' => 'two', - ] + ], ); self::assertEquals( file_get_contents($this->getDataDir() . 'ParentIdsTest.csv'), - file_get_contents($parser->getCsvFiles()['test']->getPathName()) + file_get_contents($parser->getCsvFiles()['test']->getPathName()), ); } @@ -398,7 +399,7 @@ public function testProcessSimpleArray(): void '"a"' . "\n", '"b"' . "\n", ], - file($parser->getCsvFiles()['root']->getPathname()) + file($parser->getCsvFiles()['root']->getPathname()), ); } @@ -447,11 +448,11 @@ public function testNestedArrays(): void self::assertEquals( true, $logHandler->hasWarning("Converting nested array 'root.[]' to JSON string."), - 'Warning should have been logged' + 'Warning should have been logged', ); self::assertEquals( file_get_contents($this->getDataDir() . 'NestedArraysJson.csv'), - file_get_contents($parser->getCsvFiles()['root']->getPathName()) + file_get_contents($parser->getCsvFiles()['root']->getPathName()), ); } @@ -479,7 +480,7 @@ public function testHeaderSpecialChars(): void '"id","KeywordRanking_attributes_date","KeywordRanking_stuff_I_ARE_POTAT"' . ',"KeywordRanking_stuff_kek_ser_ou_ly"' . "\n" . '"123456","2015-03-20","aaa$@!","now"' . "\n", - file_get_contents($parser->getCsvFiles()['root']->getPathName()) + file_get_contents($parser->getCsvFiles()['root']->getPathName()), ); } @@ -558,7 +559,7 @@ public function testAutoUpgradeToArray(): void '"root_eae48f50d1159c41f633f876d6c66411"' . "\n" . '"root_83cb9491934903381f6808ac79842022"' . "\n" . '"root_6d231f9592a4e259452229e2be31f42e"' . "\n", - file_get_contents($parser->getCsvFiles()['root']->getPathName()) + file_get_contents($parser->getCsvFiles()['root']->getPathName()), ); self::assertEquals( @@ -567,7 +568,7 @@ public function testAutoUpgradeToArray(): void '"val2.1.1","val2.1.2","root_83cb9491934903381f6808ac79842022"' . "\n" . '"val2.2.1","","root_83cb9491934903381f6808ac79842022"' . "\n" . '"val3.1","val3.2","root_6d231f9592a4e259452229e2be31f42e"' . "\n", - file_get_contents($parser->getCsvFiles()['root_key']->getPathName()) + file_get_contents($parser->getCsvFiles()['root_key']->getPathName()), ); // Test with array first @@ -598,7 +599,7 @@ public function testAutoUpgradeToArray(): void '"key"' . "\n" . '"arr_d03523e758a12366bd7062ee727c4939"' . "\n" . '"arr_6d231f9592a4e259452229e2be31f42e"' . "\n", - file_get_contents($parser->getCsvFiles()['arr']->getPathName()) + file_get_contents($parser->getCsvFiles()['arr']->getPathName()), ); self::assertEquals( @@ -606,7 +607,7 @@ public function testAutoUpgradeToArray(): void '"val2.1.1","val2.1.2","arr_d03523e758a12366bd7062ee727c4939"' . "\n" . '"val2.2.1","val2.2.2","arr_d03523e758a12366bd7062ee727c4939"' . "\n" . '"val3.1","val3.2","arr_6d231f9592a4e259452229e2be31f42e"' . "\n", - file_get_contents($parser->getCsvFiles()['arr_key']->getPathName()) + file_get_contents($parser->getCsvFiles()['arr_key']->getPathName()), ); } @@ -698,7 +699,7 @@ public function testAutoUpgradeToArrayString(): void '"root_0c616a2609bd2e8d88574f3f856170c5"' . "\n" . '"root_3cc17a87c69e64707ac357e84e5a9eb8"' . "\n" . '"root_af523454cc66582ad5dcec3f171b35ed"' . "\n", - file_get_contents($parser->getCsvFiles()['root']->getPathName()) + file_get_contents($parser->getCsvFiles()['root']->getPathName()), ); self::assertEquals( @@ -707,7 +708,7 @@ public function testAutoUpgradeToArrayString(): void '"str2.1","root_3cc17a87c69e64707ac357e84e5a9eb8"' . "\n" . '"str2.2","root_3cc17a87c69e64707ac357e84e5a9eb8"' . "\n" . '"str3","root_af523454cc66582ad5dcec3f171b35ed"' . "\n", - file_get_contents($parser->getCsvFiles()['root_key']->getPathName()) + file_get_contents($parser->getCsvFiles()['root_key']->getPathName()), ); } @@ -734,7 +735,7 @@ public function testIncompleteData(): void self::assertEquals( '"id","value"' . "\n" . '"1",""' . "\n", - file_get_contents($parser->getCsvFiles()['root']->getPathName()) + file_get_contents($parser->getCsvFiles()['root']->getPathName()), ); } @@ -770,7 +771,7 @@ public function testArrayOfNull(): void 'obj' => [null], ], ], - 's2null' + 's2null', ); $parser->process( @@ -784,7 +785,7 @@ public function testArrayOfNull(): void 'obj' => [null], ], ], - 'null2s' + 'null2s', ); self::assertEquals( @@ -792,7 +793,7 @@ public function testArrayOfNull(): void '"s2null_eb89917794221aeda822735efbab9069","s2null_eb89917794221aeda822735efbab9069"' . "\n" . '"s2null_77cca534224f13ec1fa45c6c0c98557d","s2null_77cca534224f13ec1fa45c6c0c98557d"' . "\n" . '', - file_get_contents($parser->getCsvFiles()['s2null']->getPathName()) + file_get_contents($parser->getCsvFiles()['s2null']->getPathName()), ); self::assertEquals( @@ -800,7 +801,7 @@ public function testArrayOfNull(): void '"stringArr","s2null_eb89917794221aeda822735efbab9069"' . "\n" . '"","s2null_77cca534224f13ec1fa45c6c0c98557d"' . "\n" . '', - file_get_contents($parser->getCsvFiles()['s2null_val']->getPathName()) + file_get_contents($parser->getCsvFiles()['s2null_val']->getPathName()), ); self::assertEquals( @@ -808,7 +809,7 @@ public function testArrayOfNull(): void '"objValue","s2null_eb89917794221aeda822735efbab9069"' . "\n" . '"","s2null_77cca534224f13ec1fa45c6c0c98557d"' . "\n" . '', - file_get_contents($parser->getCsvFiles()['s2null_obj']->getPathName()) + file_get_contents($parser->getCsvFiles()['s2null_obj']->getPathName()), ); self::assertEquals( @@ -816,7 +817,7 @@ public function testArrayOfNull(): void '"null2s_eb89917794221aeda822735efbab9069","null2s_eb89917794221aeda822735efbab9069"' . "\n" . '"null2s_77cca534224f13ec1fa45c6c0c98557d","null2s_77cca534224f13ec1fa45c6c0c98557d"' . "\n". '', - file_get_contents($parser->getCsvFiles()['null2s']->getPathName()) + file_get_contents($parser->getCsvFiles()['null2s']->getPathName()), ); self::assertEquals( @@ -824,7 +825,7 @@ public function testArrayOfNull(): void '"stringArr","null2s_eb89917794221aeda822735efbab9069"' . "\n" . '"","null2s_77cca534224f13ec1fa45c6c0c98557d"' . "\n" . '', - file_get_contents($parser->getCsvFiles()['null2s_val']->getPathName()) + file_get_contents($parser->getCsvFiles()['null2s_val']->getPathName()), ); self::assertEquals( @@ -832,7 +833,7 @@ public function testArrayOfNull(): void '"objValue","null2s_eb89917794221aeda822735efbab9069"' . "\n" . '"","null2s_77cca534224f13ec1fa45c6c0c98557d"' . "\n" . '', - file_get_contents($parser->getCsvFiles()['null2s_obj']->getPathName()) + file_get_contents($parser->getCsvFiles()['null2s_obj']->getPathName()), ); } @@ -841,14 +842,14 @@ public function testParseNumericKeys(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure(), true)); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( - '{"data": [{"1": "one", "2": "two"}]}' + $testFile = jsonDecode( + '{"data": [{"1": "one", "2": "two"}]}', ); $parser->process($testFile->data, 'someType'); self::assertEquals(['someType'], array_keys($parser->getCsvFiles())); self::assertEquals( "\"1\",\"2\"\n\"one\",\"two\"\n", - file_get_contents($parser->getCsvFiles()['someType']->getPathName()) + file_get_contents($parser->getCsvFiles()['someType']->getPathName()), ); } @@ -857,20 +858,20 @@ public function testParseNestedArrayEnabled(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure(), true)); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( - '{"a": [["c", "d"], ["e", "f"]]}' + $testFile = jsonDecode( + '{"a": [["c", "d"], ["e", "f"]]}', ); $parser->process([$testFile], 'someType'); self::assertEquals(['someType', 'someType_a'], array_keys($parser->getCsvFiles())); self::assertEquals( "\"a\"\n\"someType_0a3f2bc488aa446db98866f181f43dbb\"\n", - file_get_contents($parser->getCsvFiles()['someType']->getPathName()) + file_get_contents($parser->getCsvFiles()['someType']->getPathName()), ); self::assertEquals( "\"data\",\"JSON_parentId\"\n" . "\"[\"\"c\"\",\"\"d\"\"]\",\"someType_0a3f2bc488aa446db98866f181f43dbb\"\n" . "\"[\"\"e\"\",\"\"f\"\"]\",\"someType_0a3f2bc488aa446db98866f181f43dbb\"\n", - file_get_contents($parser->getCsvFiles()['someType_a']->getPathName()) + file_get_contents($parser->getCsvFiles()['someType_a']->getPathName()), ); } @@ -879,14 +880,14 @@ public function testParseNullInconsistency(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure(), true)); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( - '{"data": [null, "a"]}' + $testFile = jsonDecode( + '{"data": [null, "a"]}', ); $parser->process($testFile->data, 'someType'); self::assertEquals(['someType'], array_keys($parser->getCsvFiles())); self::assertEquals( "\"data\"\n\"\"\n\"a\"\n", - file_get_contents($parser->getCsvFiles()['someType']->getPathName()) + file_get_contents($parser->getCsvFiles()['someType']->getPathName()), ); } @@ -895,8 +896,8 @@ public function testParseInvalidParentId(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( - '{"data": ["a", "b"]}' + $testFile = jsonDecode( + '{"data": ["a", "b"]}', ); $parser->process($testFile->data, 'someType', ['someColumn' => ['this' => 'is wrong']]); @@ -904,7 +905,7 @@ public function testParseInvalidParentId(): void $this->expectException(JsonParserException::class); $this->expectExceptionMessage( - 'Error assigning parentId to a CSV file! $parentId array cannot be multidimensional' + 'Error assigning parentId to a CSV file! $parentId array cannot be multidimensional', ); $parser->getCsvFiles(); } @@ -914,8 +915,8 @@ public function testParseInvalidPrimaryKey(): void $parser = new Parser(new Analyzer(new NullLogger(), new Structure(), true)); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( - '{"data": [{"id": "a", "val": ["a"]}, {"id": "b", "val": ["b"]}]}' + $testFile = jsonDecode( + '{"data": [{"id": "a", "val": ["a"]}, {"id": "b", "val": ["b"]}]}', ); $parser->addPrimaryKeys(['someType_val' => 'id']); $parser->process($testFile->data, 'someType'); @@ -923,12 +924,12 @@ public function testParseInvalidPrimaryKey(): void self::assertEquals( "\"id\",\"val\"\n\"a\",\"someType_ee9689ff88c83c395a3ffd9a0e747920\"\n". "\"b\",\"someType_37fb9eda31010642e996aa72bc998558\"\n", - file_get_contents($parser->getCsvFiles()['someType']->getPathName()) + file_get_contents($parser->getCsvFiles()['someType']->getPathName()), ); self::assertEquals( "\"data\",\"JSON_parentId\"\n\"a\",\"someType_ee9689ff88c83c395a3ffd9a0e747920\"\n" . "\"b\",\"someType_37fb9eda31010642e996aa72bc998558\"\n", - file_get_contents($parser->getCsvFiles()['someType_val']->getPathName()) + file_get_contents($parser->getCsvFiles()['someType_val']->getPathName()), ); } } diff --git a/tests/phpunit/ParserTestCase.php b/tests/phpunit/ParserTestCase.php index 17703bc..107584c 100644 --- a/tests/phpunit/ParserTestCase.php +++ b/tests/phpunit/ParserTestCase.php @@ -5,17 +5,15 @@ namespace Keboola\Json\Tests; use PHPUnit\Framework\TestCase; +use function Keboola\Utils\jsonDecode; class ParserTestCase extends TestCase { - /** - * @return mixed - */ - protected function loadJson(string $fileName) + protected function loadJson(string $fileName): mixed { $testFilesPath = $this->getDataDir() . $fileName . '.json'; $file = (string) file_get_contents($testFilesPath); - return \Keboola\Utils\jsonDecode($file); + return jsonDecode($file); } protected function getDataDir(): string diff --git a/tests/phpunit/RealDataTest.php b/tests/phpunit/RealDataTest.php index f34d5e6..e57da05 100644 --- a/tests/phpunit/RealDataTest.php +++ b/tests/phpunit/RealDataTest.php @@ -9,6 +9,8 @@ use Keboola\Json\Parser; use Keboola\Json\Structure; use Psr\Log\NullLogger; +use function Keboola\Utils\arrayToObject; +use function Keboola\Utils\jsonDecode; class RealDataTest extends ParserTestCase { @@ -23,7 +25,7 @@ public function testProcess(): void // compare result files self::assertEquals( file_get_contents("{$testFilesPath}/{$name}.csv"), - file_get_contents($table->getPathname()) + file_get_contents($table->getPathname()), ); // compare column counts @@ -78,7 +80,7 @@ public function testTypeCharacters(): void 'a_b_c_d_e_f_statuses_retweeted_status_entities_media', 'a_b_c_d_e_f_statuses_retweeted_status_entities_media_indices', ], - array_keys($parser->getCsvFiles()) + array_keys($parser->getCsvFiles()), ); } @@ -132,7 +134,7 @@ public function testValidateHeader(): void 'KIND_Conversions_Submissions : KIND_Projects_Conversions_Submissions: View-through Revenue', 'KIND_Conversions_Submissions : KIND_Projects_Conversions_Submissions: Total Revenue']; $data = array_combine($header, array_fill(0, count($header), 'boo')); - $parser->process(['items' => \Keboola\Utils\arrayToObject($data)], 'root'); + $parser->process(['items' => arrayToObject($data)], 'root'); $file = $parser->getCsvFiles()['root']; $expectedHeader = [ @@ -203,7 +205,7 @@ public function testProcessWithAutoUpgradeToArray(): void // compare result files self::assertEquals( file_get_contents("{$testFilesPath}/{$name}.csv"), - file_get_contents($table->getPathname()) + file_get_contents($table->getPathname()), ); // compare column counts @@ -244,7 +246,7 @@ public function testFloatHash(): void { $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [ { @@ -260,7 +262,7 @@ public function testFloatHash(): void ] } ] - }' + }', ); $parser->process($testFile->components); self::assertEquals(['root', 'root_data'], array_keys($parser->getCsvFiles())); @@ -278,7 +280,7 @@ public function testEmptyArray(): void { $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); /** @var \stdClass $testFile */ - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '{ "components": [ { @@ -290,7 +292,7 @@ public function testEmptyArray(): void "data": ["test"] } ] - }' + }', ); $parser->process($testFile->components); self::assertEquals(['root', 'root_data'], array_keys($parser->getCsvFiles())); @@ -306,7 +308,7 @@ public function testEmptyArray(): void public function testEmptyObject(): void { $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '[ { "id": 1, @@ -315,7 +317,7 @@ public function testEmptyObject(): void "data": {}, "flags": [] } - ]' + ]', ); $parser->process($testFile); self::assertEquals(['root'], array_keys($parser->getCsvFiles())); @@ -327,7 +329,7 @@ public function testEmptyObject(): void public function testEmptyAndNonEmptyObject(): void { $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '[ { "id": 1, @@ -336,10 +338,10 @@ public function testEmptyAndNonEmptyObject(): void "data": {}, "flags": [] } - ]' + ]', ); $parser->process($testFile); - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '[ { "id": 2, @@ -348,7 +350,7 @@ public function testEmptyAndNonEmptyObject(): void "data": {"a": "b"}, "flags": [] } - ]' + ]', ); $parser->process($testFile); self::assertEquals(['root'], array_keys($parser->getCsvFiles())); @@ -361,7 +363,7 @@ public function testEmptyAndNonEmptyObject(): void public function testAlmostEmptyObject(): void { $parser = new Parser(new Analyzer(new NullLogger(), new Structure())); - $testFile = \Keboola\Utils\jsonDecode( + $testFile = jsonDecode( '[ { "id": 1, @@ -373,7 +375,7 @@ public function testAlmostEmptyObject(): void }, "flags": [] } - ]' + ]', ); $parser->process($testFile); self::assertEquals(['root'], array_keys($parser->getCsvFiles())); diff --git a/tests/phpunit/StructureTest.php b/tests/phpunit/StructureTest.php index b0668df..bb94b66 100644 --- a/tests/phpunit/StructureTest.php +++ b/tests/phpunit/StructureTest.php @@ -42,7 +42,7 @@ public function testSaveNode(): void ], 'parent_aliases' => [], ], - $structure->getData() + $structure->getData(), ); } @@ -64,7 +64,7 @@ public function testSaveNodeReserved2(): void $structure->saveNode(new NodePath(['root', Structure::ARRAY_NAME]), ['nodeType' => 'object']); $structure->saveNode( new NodePath(['root', Structure::ARRAY_NAME, Structure::ARRAY_NAME]), - ['nodeType' => 'scalar'] + ['nodeType' => 'scalar'], ); $this->expectException(JsonParserException::class); @@ -91,7 +91,7 @@ public function testSaveValue(): void ], 'parent_aliases' => [], ], - $structure->getData() + $structure->getData(), ); } @@ -135,7 +135,7 @@ public function testSaveValueConflictTypeUpgradeAllowed1(): void ], 'parent_aliases' => [], ], - $structure->getData() + $structure->getData(), ); } @@ -157,7 +157,7 @@ public function testSaveValueConflictTypeUpgradeAllowed2(): void ], 'parent_aliases' => [], ], - $structure->getData() + $structure->getData(), ); } @@ -170,7 +170,7 @@ public function testSaveValueConflictTypeUpgradeArrayAllowed1(): void $structure->saveNodeValue( new NodePath(['root', Structure::ARRAY_NAME, 'str', Structure::ARRAY_NAME]), 'nodeType', - 'scalar' + 'scalar', ); $structure->saveNodeValue(new NodePath(['root', Structure::ARRAY_NAME, 'str']), 'nodeType', 'array'); self::assertEquals( @@ -191,7 +191,7 @@ public function testSaveValueConflictTypeUpgradeArrayAllowed1(): void ], 'parent_aliases' => [], ], - $structure->getData() + $structure->getData(), ); } @@ -204,7 +204,7 @@ public function testSaveValueConflictTypeUpgradeArrayAllowed2(): void $structure->saveNode(new NodePath(['root', Structure::ARRAY_NAME, 'obj', '[]']), ['nodeType' => 'object']); $structure->saveNode( new NodePath(['root', Structure::ARRAY_NAME, 'obj', Structure::ARRAY_NAME, 'prop']), - ['nodeType' => 'scalar'] + ['nodeType' => 'scalar'], ); $structure->saveNodeValue(new NodePath(['root', Structure::ARRAY_NAME, 'obj']), 'nodeType', 'object'); self::assertEquals( @@ -228,7 +228,7 @@ public function testSaveValueConflictTypeUpgradeArrayAllowed2(): void ], 'parent_aliases' => [], ], - $structure->getData() + $structure->getData(), ); } @@ -239,15 +239,15 @@ public function testSaveValueConflictTypeUpgradeArrayAllowed3(): void $structure->saveNode(new NodePath(['root', Structure::ARRAY_NAME]), ['nodeType' => 'object']); $structure->saveNode( new NodePath(['root', Structure::ARRAY_NAME, 'obj']), - ['nodeType' => 'object', 'headerNames' => 'my-obj'] + ['nodeType' => 'object', 'headerNames' => 'my-obj'], ); $structure->saveNode( new NodePath(['root', Structure::ARRAY_NAME, 'obj', Structure::ARRAY_NAME]), - ['nodeType' => 'object'] + ['nodeType' => 'object'], ); $structure->saveNode( new NodePath(['root', Structure::ARRAY_NAME, 'obj', Structure::ARRAY_NAME, 'prop']), - ['nodeType' => 'scalar'] + ['nodeType' => 'scalar'], ); $structure->saveNodeValue(new NodePath(['root', Structure::ARRAY_NAME, 'obj']), 'nodeType', 'array'); self::assertEquals( @@ -273,7 +273,7 @@ public function testSaveValueConflictTypeUpgradeArrayAllowed3(): void ], 'parent_aliases' => [], ], - $structure->getData() + $structure->getData(), ); } @@ -424,7 +424,7 @@ public function testGetColumnTypes1(): void $structure->load($data); self::assertEquals( ['prop1' => 'array', 'prop2' => 'scalar'], - $structure->getColumnTypes(new NodePath(['root', 'obj'])) + $structure->getColumnTypes(new NodePath(['root', 'obj'])), ); } @@ -454,7 +454,7 @@ public function testGetColumnTypes2(): void $structure->load($data); self::assertEquals( ['prop2' => 'scalar'], - $structure->getColumnTypes(new NodePath(['root', 'obj', 'prop2'])) + $structure->getColumnTypes(new NodePath(['root', 'obj', 'prop2'])), ); } @@ -484,7 +484,7 @@ public function testGetColumnTypes3(): void $structure->load($data); self::assertEquals( [], - $structure->getColumnTypes(new NodePath(['root', 'obj', 'prop1'])) + $structure->getColumnTypes(new NodePath(['root', 'obj', 'prop1'])), ); } @@ -560,7 +560,7 @@ public function testHeaderNames(): void ], 'parent_aliases' => [], ], - $structure->getData() + $structure->getData(), ); } @@ -569,7 +569,7 @@ public function testGetTypeFromPath(): void $structure = new Structure(); self::assertEquals( 'root_prop', - $structure->getTypeFromNodePath(new NodePath(['root', Structure::ARRAY_NAME, 'prop'])) + $structure->getTypeFromNodePath(new NodePath(['root', Structure::ARRAY_NAME, 'prop'])), ); }