From d0956f851cb0771ed3ab52af3137ce122107d345 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Mon, 31 Oct 2022 00:42:29 +0100 Subject: [PATCH] Initial unit tests, code cleanup --- bin/dataset-info | 2 +- composer.json | 7 +++- phpstan.neon | 13 ++++++ phpunit.xml | 27 ++++++++++++ src/Dataset.php | 26 ++++++++++++ src/DatasetManager.php | 71 ++++++++++++++++++++++---------- src/InvalidDatasetException.php | 8 ++++ src/Readers/CsvReader.php | 3 ++ src/Readers/JsonReader.php | 3 ++ src/UnknownDatasetExeption.php | 13 ++++++ tests/autoload.php | 3 ++ tests/foo/bar/dataset.json | 7 ++++ tests/src/DatasetManagerTest.php | 21 ++++++++++ 13 files changed, 180 insertions(+), 24 deletions(-) create mode 100644 phpstan.neon create mode 100644 phpunit.xml create mode 100644 src/InvalidDatasetException.php create mode 100644 src/UnknownDatasetExeption.php create mode 100644 tests/autoload.php create mode 100644 tests/foo/bar/dataset.json create mode 100644 tests/src/DatasetManagerTest.php diff --git a/bin/dataset-info b/bin/dataset-info index 14e0018..0009739 100755 --- a/bin/dataset-info +++ b/bin/dataset-info @@ -26,5 +26,5 @@ foreach ($datasets as $dataset) { if (!$headers) $headers = array_keys($row); $rows++; } - echo " ".$rows." rows\n - ".join("\n - ",$headers)."\n"; + echo " # ".$rows." rows\n - ".join("\n - ",$headers)."\n"; } diff --git a/composer.json b/composer.json index 110e8c7..d4e4eca 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,9 @@ "email": "cvagnetoft@gmail.com" } ], - "require": {}, - "bin": [ "bin/dataset-info" ] + "bin": [ "bin/dataset-info" ], + "require-dev": { + "phpunit/phpunit": "^9.5", + "phpstan/phpstan": "^1.8" + } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..36c0964 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,13 @@ +parameters: + level: 5 + + excludePaths: + - doc + - vendor + - var + - tests + + # Paths to include in the analysis + paths: + - src + diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..3ca25f0 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,27 @@ + + + + + tests + + + + + + src + + + diff --git a/src/Dataset.php b/src/Dataset.php index 71eba33..1ae1008 100644 --- a/src/Dataset.php +++ b/src/Dataset.php @@ -7,6 +7,10 @@ use NoccyLabs\Dataset\Readers\JsonReader; class Dataset { + protected string $packageName; + + protected string $datasetName; + protected string $identifier; protected array $options; @@ -18,6 +22,8 @@ class Dataset $this->identifier = $identifier; $this->options = $options; $this->version = $version; + + [$this->packageName, $this->datasetName] = explode("#", $identifier, 2); } public function getIdentifier(): string @@ -25,11 +31,31 @@ class Dataset return $this->identifier; } + public function getPackageName(): string + { + return $this->packageName; + } + + public function getDatasetName(): string + { + return $this->datasetName; + } + public function getVersion(): ?string { return $this->version; } + public function getComment(): ?string + { + return array_key_exists('comment', $this->options) ? $this->options['comment'] : null; + } + + public function getLicense(): ?string + { + return array_key_exists('license', $this->options) ? $this->options['license'] : null; + } + public function open(): ReaderInterface { $filename = $this->options['filename']; diff --git a/src/DatasetManager.php b/src/DatasetManager.php index ecfa9c1..3123122 100644 --- a/src/DatasetManager.php +++ b/src/DatasetManager.php @@ -22,16 +22,46 @@ class DatasetManager } } + /** + * Return all the available datasets + * + * @return Array The available datasets + */ public function getAvailableDatasets(): array { return self::$datasets; } + /** + * Directly return a reader for a specific dataset. + * + * @param string $identifier The dataset identifier + * @return ReaderInterface A reader for the data + * @throws InvalidDatasetException if the dataset can not be opened + * @throws UnknownDatasetException if the dataset does not exist + */ public function openDataset(string $identifier): ReaderInterface { return $this->getDataset($identifier)->open(); } + + /** + * Return a Dataset object containing metadata and methods to retrieve + * a reader for the data in the set. + * + * @param string $identifier The dataset identifier + * @throws UnknownDatasetException if the dataset does not exist + */ + public function getDataset(string $identifier): Dataset + { + if (!array_key_exists($identifier, self::$datasets)) { + throw UnknownDatasetException::DatasetNotFound(); + } + + return self::$datasets[$identifier]; + } + /** * Find the vendor directory and try to locate all bundled datasets * @@ -67,8 +97,8 @@ class DatasetManager * * * - * @param string The package name (org/package) - * @param string The full path to the package (..../vendor/org/package) + * @param string $package The package name (org/package) + * @param string $path The full path to the package (..../vendor/org/package) */ private function scanPackageDatasets(string $package, string $path) { @@ -84,10 +114,15 @@ class DatasetManager } $this->loadDatasets($json['datasets'], null, $package, $path); - //printf("found %d sets in %s\n", count($json['datasets']), $package); } + /** + * + * + * + * + */ private function loadDatasets(array $datasets, ?string $prefix, string $package, string $path) { foreach ($datasets as $name=>$options) { @@ -103,14 +138,21 @@ class DatasetManager } } + /** + * + * + * + */ private function determineVendorPath(): ?string { - if (defined("COMPOSER_VENDOR_PATH")) { - return COMPOSER_VENDOR_PATH; + $d = defined("NOCCYLABS_DATASET_TEST") ? (dirname(__DIR__)."/tests") : __DIR__; + while ($d != dirname($d)) { + if (file_exists($d."/autoload.php")) break; + $d = dirname($d); } - if (file_exists(__DIR__."/../../../autoload.php")) { - // we are installed as a composer package - return dirname(__DIR__, 3); + + if (file_exists($d."/autoload.php")) { + return $d; } return null; } @@ -127,17 +169,4 @@ class DatasetManager self::$datasets[$id] = $dataset; } - - - /** - * - * - * @throws InvalidDatasetException if the dataset can not be opened - * @throws UnknownDatasetExcception if the dataset does not exist - */ - public function getDataset(string $identifier): Dataset - { - - return self::$datasets[$identifier]; - } } diff --git a/src/InvalidDatasetException.php b/src/InvalidDatasetException.php new file mode 100644 index 0000000..88de968 --- /dev/null +++ b/src/InvalidDatasetException.php @@ -0,0 +1,8 @@ +files = glob($filename); + $this->options = $options; } private function checkLoadedSlice() diff --git a/src/Readers/JsonReader.php b/src/Readers/JsonReader.php index d7ef2f4..8e44440 100644 --- a/src/Readers/JsonReader.php +++ b/src/Readers/JsonReader.php @@ -8,6 +8,8 @@ class JsonReader implements ReaderInterface { private array $files = []; + private array $options = []; + private int $currentFile = 0; private ?int $loadedFile = null; @@ -21,6 +23,7 @@ class JsonReader implements ReaderInterface public function __construct(string $filename, array $options) { $this->files = glob($filename); + $this->options = $options; } private function checkLoadedSlice() diff --git a/src/UnknownDatasetExeption.php b/src/UnknownDatasetExeption.php new file mode 100644 index 0000000..c3b0219 --- /dev/null +++ b/src/UnknownDatasetExeption.php @@ -0,0 +1,13 @@ +getAvailableDatasets(); + $this->assertEquals(1, count($sets), "Expected 1 loaded set"); + } +}