diff --git a/bin/build-cultures.php b/bin/build-cultures.php index d55362b7..b262ecda 100644 --- a/bin/build-cultures.php +++ b/bin/build-cultures.php @@ -25,6 +25,7 @@ use TxTextControl\ReportingCloud\Assert\Assert; use TxTextControl\ReportingCloud\Exception\RuntimeException; use TxTextControl\ReportingCloud\Stdlib\ArrayUtils; +use TxTextControl\ReportingCloud\Stdlib\ConsoleUtils; // --------------------------------------------------------------------------------------------------------------------- @@ -44,7 +45,7 @@ } if (0 === count($values)) { - $format = 'Cannot download the available cultures from %s'; + $format = 'Cannot download the available cultures from "%s".'; $message = sprintf($format, $url); throw new RuntimeException($message); } @@ -61,11 +62,9 @@ ArrayUtils::varExportToFile($filename, $values, $generator); echo PHP_EOL; -echo sprintf('The available cultures (%d) are %s.', count($values), implode(', ', $values)); -echo PHP_EOL; -echo PHP_EOL; -echo sprintf('Written data file to %s', $filename); +ConsoleUtils::writeLn('The available cultures (%d) are %s.', count($values), implode(', ', $values)); echo PHP_EOL; +ConsoleUtils::writeLn('Written data file to "%s".', $filename); echo PHP_EOL; // --------------------------------------------------------------------------------------------------------------------- diff --git a/bin/build-dictionaries.php b/bin/build-dictionaries.php index e013dd60..e70be547 100644 --- a/bin/build-dictionaries.php +++ b/bin/build-dictionaries.php @@ -47,11 +47,9 @@ ArrayUtils::varExportToFile($filename, $values, $generator); echo PHP_EOL; -echo sprintf('The available dictionaries (%d) are %s.', count($values), implode(', ', $values)); -echo PHP_EOL; -echo PHP_EOL; -echo sprintf('Written data file to %s', $filename); +ConsoleUtils::writeLn('The available dictionaries (%d) are %s.', count($values), implode(', ', $values)); echo PHP_EOL; +ConsoleUtils::writeLn('Written data file to "%s".', $filename); echo PHP_EOL; // --------------------------------------------------------------------------------------------------------------------- diff --git a/demo/api-key-management.php b/demo/api-key-management.php index 476bd624..64b9709f 100644 --- a/demo/api-key-management.php +++ b/demo/api-key-management.php @@ -10,22 +10,16 @@ $reportingCloud = new ReportingCloud([ 'api_key' => ConsoleUtils::apiKey(), + 'test' => true, ]); -$apiKeys = $reportingCloud->getApiKeys(); - -if (!empty($apiKeys)) { - foreach ($apiKeys as $apiKey) { - if ($apiKey['key'] == ConsoleUtils::apiKey()) { - echo sprintf("Keeping API key %s...", $apiKey['key']); - echo PHP_EOL; - continue; - } - echo sprintf("Deleting API key %s...", $apiKey['key']); - echo PHP_EOL; - $reportingCloud->deleteApiKey($apiKey['key']); - unset($apiKey); +foreach ($reportingCloud->getApiKeys() as $apiKey) { + if ($apiKey['key'] === ConsoleUtils::apiKey()) { + ConsoleUtils::writeLn('Keeping API key "%s".', $apiKey['key']); + continue; } + ConsoleUtils::writeLn('Deleting API key "%s".', $apiKey['key']); + $reportingCloud->deleteApiKey($apiKey['key']); } $newApiKey = $reportingCloud->createApiKey(); @@ -38,7 +32,8 @@ 'api_key' => $newApiKey, ]); -dump($reportingCloud->getAccountSettings()); -dump($reportingCloud->getTemplateList()); +ConsoleUtils::dump($reportingCloud->getAccountSettings()); + +ConsoleUtils::dump($reportingCloud->getTemplateList()); // --------------------------------------------------------------------------------------------------------------------- diff --git a/demo/append-document-advanced.php b/demo/append-document-advanced.php index e546bb0f..d0cd0b1f 100644 --- a/demo/append-document-advanced.php +++ b/demo/append-document-advanced.php @@ -9,19 +9,20 @@ $reportingCloud = new ReportingCloud([ 'api_key' => ConsoleUtils::apiKey(), + 'test' => true, ]); $documents = [ [ - 'filename' => sprintf('%s/test_document.docx', Path::resource()), + 'filename' => sprintf('%s/maelzel_machine.docx', Path::resource()), 'divider' => ReportingCloud::DOCUMENT_DIVIDER_NEW_SECTION, ], [ - 'filename' => sprintf('%s/test_document.docx', Path::resource()), + 'filename' => sprintf('%s/maelzel_machine.docx', Path::resource()), 'divider' => ReportingCloud::DOCUMENT_DIVIDER_NEW_SECTION, ], [ - 'filename' => sprintf('%s/test_document.docx', Path::resource()), + 'filename' => sprintf('%s/maelzel_machine.docx', Path::resource()), 'divider' => ReportingCloud::DOCUMENT_DIVIDER_NEW_SECTION, ], ]; @@ -29,11 +30,11 @@ $destinationFilename = sprintf('%s/test_append_document_advanced.pdf', Path::output()); $documentSettings = [ - 'author' => 'James Henry Trotter', + 'author' => 'Wikipedia', 'creation_date' => time(), - 'creator_application' => 'An awesome creator', - 'document_subject' => 'The Old Green Grasshopper', - 'document_title' => 'James and the Giant Peach', + 'creator_application' => 'ReportingCloud PHP SDK', + 'document_subject' => 'The Mechanical Turk', + 'document_title' => 'Mälzel and the Machine', 'last_modification_date' => time(), 'user_password' => '1', ]; @@ -46,5 +47,4 @@ file_put_contents($destinationFilename, $binaryData); -echo sprintf('Written to "%s".', $destinationFilename); -echo PHP_EOL; +ConsoleUtils::writeLn('Written to "%s".', $destinationFilename); diff --git a/demo/append-document-basic.php b/demo/append-document-basic.php index bda55b12..283e6aa4 100644 --- a/demo/append-document-basic.php +++ b/demo/append-document-basic.php @@ -9,19 +9,20 @@ $reportingCloud = new ReportingCloud([ 'api_key' => ConsoleUtils::apiKey(), + 'test' => true, ]); $documents = [ [ - 'filename' => sprintf('%s/test_document.docx', Path::resource()), + 'filename' => sprintf('%s/maelzel_machine.docx', Path::resource()), 'divider' => ReportingCloud::DOCUMENT_DIVIDER_NEW_SECTION, ], [ - 'filename' => sprintf('%s/test_document.docx', Path::resource()), + 'filename' => sprintf('%s/maelzel_machine.docx', Path::resource()), 'divider' => ReportingCloud::DOCUMENT_DIVIDER_NEW_SECTION, ], [ - 'filename' => sprintf('%s/test_document.docx', Path::resource()), + 'filename' => sprintf('%s/maelzel_machine.docx', Path::resource()), 'divider' => ReportingCloud::DOCUMENT_DIVIDER_NEW_SECTION, ], ]; @@ -35,5 +36,4 @@ file_put_contents($destinationFilename, $binaryData); -echo sprintf('Written to "%s".', $destinationFilename); -echo PHP_EOL; +ConsoleUtils::writeLn('Written to "%s".', $destinationFilename); diff --git a/demo/convert-document-docx-to-pdf.php b/demo/convert-document-docx-to-pdf.php index c8987abe..fdeb9b1f 100644 --- a/demo/convert-document-docx-to-pdf.php +++ b/demo/convert-document-docx-to-pdf.php @@ -11,8 +11,8 @@ 'api_key' => ConsoleUtils::apiKey(), ]); -$sourceFilename = sprintf('%s/test_document.docx', Path::resource()); -$destinationFilename = sprintf('%s/test_document.pdf', Path::output()); +$sourceFilename = sprintf('%s/maelzel_machine.docx', Path::resource()); +$destinationFilename = sprintf('%s/maelzel_machine.pdf', Path::output()); $document = $reportingCloud->convertDocument( $sourceFilename, @@ -20,10 +20,8 @@ ); if (empty($document)) { - echo sprintf('Error converting "%s".', $sourceFilename); - echo PHP_EOL; + ConsoleUtils::writeLn('Error converting "%s".', $sourceFilename); } else { file_put_contents($destinationFilename, $document); - echo sprintf('"%s" was converted and written to "%s".', $sourceFilename, $destinationFilename); - echo PHP_EOL; + ConsoleUtils::writeLn('"%s" was converted and written to "%s".', $sourceFilename, $destinationFilename); } diff --git a/demo/convert-document-pdf-to-txt.php b/demo/convert-document-pdf-to-txt.php index 8a590e0d..59ddc37e 100644 --- a/demo/convert-document-pdf-to-txt.php +++ b/demo/convert-document-pdf-to-txt.php @@ -11,8 +11,8 @@ 'api_key' => ConsoleUtils::apiKey(), ]); -$sourceFilename = sprintf('%s/camera_controls.pdf', Path::resource()); -$destinationFilename = sprintf('%s/camera_controls.txt', Path::output()); +$sourceFilename = sprintf('%s/maelzel_machine.pdf', Path::resource()); +$destinationFilename = sprintf('%s/maelzel_machine.txt', Path::output()); $document = $reportingCloud->convertDocument( $sourceFilename, @@ -20,10 +20,8 @@ ); if (empty($document)) { - echo sprintf('Error converting "%s".', $sourceFilename); - echo PHP_EOL; + ConsoleUtils::writeLn('Error converting "%s".', $sourceFilename); } else { file_put_contents($destinationFilename, $document); - echo sprintf('"%s" was converted and written to "%s".', $sourceFilename, $destinationFilename); - echo PHP_EOL; + ConsoleUtils::writeLn('"%s" was converted and written to "%s".', $sourceFilename, $destinationFilename); } diff --git a/demo/find-and-replace-document.php b/demo/find-and-replace-document.php index 9b872f2e..d0caede4 100644 --- a/demo/find-and-replace-document.php +++ b/demo/find-and-replace-document.php @@ -45,5 +45,4 @@ file_put_contents($destinationFilename, $binaryData); -echo sprintf('Written to "%s".', $destinationFilename); -echo PHP_EOL; +ConsoleUtils::writeLn('Written to "%s".', $destinationFilename); diff --git a/demo/get-account-settings.php b/demo/get-account-settings.php index 5cf7427b..22f17798 100644 --- a/demo/get-account-settings.php +++ b/demo/get-account-settings.php @@ -10,4 +10,6 @@ 'api_key' => ConsoleUtils::apiKey(), ]); -dump($reportingCloud->getAccountSettings()); +foreach ($reportingCloud->getAccountSettings() as $key => $value) { + ConsoleUtils::writeLn('- %s: %s', $key, $value); +} diff --git a/demo/get-template-info.php b/demo/get-template-info.php index 0c6da647..03470b96 100644 --- a/demo/get-template-info.php +++ b/demo/get-template-info.php @@ -24,5 +24,5 @@ $reportingCloud->uploadTemplate($sourceFilename); } - dump($reportingCloud->getTemplateInfo($templateName)); + ConsoleUtils::dump($reportingCloud->getTemplateInfo($templateName)); } diff --git a/demo/get-template-thumbnails.php b/demo/get-template-thumbnails.php index 933765d5..ac32d9f8 100644 --- a/demo/get-template-thumbnails.php +++ b/demo/get-template-thumbnails.php @@ -45,8 +45,7 @@ file_put_contents($destinationFilename, $binaryData); - echo sprintf('"%s" was written to "%s".', $templateName, $destinationFilename); - echo PHP_EOL; + ConsoleUtils::writeLn('"%s" was written to "%s".', $templateName, $destinationFilename); } // --------------------------------------------------------------------------------------------------------------------- diff --git a/demo/instantiation.php b/demo/instantiation.php index 9a6f0e59..b1abddf5 100644 --- a/demo/instantiation.php +++ b/demo/instantiation.php @@ -4,6 +4,7 @@ include_once __DIR__ . '/bootstrap.php'; use TxTextControl\ReportingCloud\ReportingCloud; +use TxTextControl\ReportingCloud\Stdlib\ConsoleUtils; // --------------------------------------------------------------------------------------------------------------------- @@ -13,7 +14,7 @@ 'api_key' => 'xxxxxxxx', ]); -dump($reportingCloud); +ConsoleUtils::dump($reportingCloud); // --------------------------------------------------------------------------------------------------------------------- @@ -23,7 +24,7 @@ $reportingCloud->setApiKey('xxxxxxxx'); -dump($reportingCloud); +ConsoleUtils::dump($reportingCloud); // --------------------------------------------------------------------------------------------------------------------- @@ -39,7 +40,7 @@ 'debug' => true, ]); -dump($reportingCloud); +ConsoleUtils::dump($reportingCloud); // --------------------------------------------------------------------------------------------------------------------- @@ -55,6 +56,6 @@ $reportingCloud->setTimeout(100); $reportingCloud->setDebug(true); -dump($reportingCloud); +ConsoleUtils::dump($reportingCloud); // --------------------------------------------------------------------------------------------------------------------- diff --git a/demo/list-fonts.php b/demo/list-fonts.php index 7fe3b48d..23eff643 100644 --- a/demo/list-fonts.php +++ b/demo/list-fonts.php @@ -10,4 +10,6 @@ 'api_key' => ConsoleUtils::apiKey(), ]); -dump($reportingCloud->getFontList()); +foreach ($reportingCloud->getFontList() as $font) { + ConsoleUtils::writeLn('- %s', $font); +} diff --git a/demo/merge-document-advanced.php b/demo/merge-document-advanced.php index 49fa7b88..0d8b90f3 100644 --- a/demo/merge-document-advanced.php +++ b/demo/merge-document-advanced.php @@ -9,7 +9,6 @@ $reportingCloud = new ReportingCloud([ 'api_key' => ConsoleUtils::apiKey(), - 'test' => true, ]); // --------------------------------------------------------------------------------------------------------------------- @@ -98,8 +97,7 @@ file_put_contents($destinationFilename, $arrayOfBinaryData[0]); -echo sprintf('Written to "%s".', $destinationFilename); -echo PHP_EOL; +ConsoleUtils::writeLn('Written to "%s".', $destinationFilename); // --------------------------------------------------------------------------------------------------------------------- @@ -125,8 +123,7 @@ file_put_contents($destinationFilename, $arrayOfBinaryData[0]); -echo sprintf('Written to "%s".', $destinationFilename); -echo PHP_EOL; +ConsoleUtils::writeLn('Written to "%s".', $destinationFilename); // --------------------------------------------------------------------------------------------------------------------- @@ -172,8 +169,7 @@ file_put_contents($destinationFilename, $arrayOfBinaryData[0]); -echo sprintf('Written to "%s".', $destinationFilename); -echo PHP_EOL; +ConsoleUtils::writeLn('Written to "%s".', $destinationFilename); // --------------------------------------------------------------------------------------------------------------------- @@ -200,8 +196,7 @@ file_put_contents($destinationFilename, $arrayOfBinaryData[0]); -echo sprintf('Written to "%s".', $destinationFilename); -echo PHP_EOL; +ConsoleUtils::writeLn('Written to "%s".', $destinationFilename); // --------------------------------------------------------------------------------------------------------------------- @@ -236,8 +231,7 @@ file_put_contents($destinationFilename, $binaryData); - echo sprintf('Written to "%s".', $destinationFilename); - echo PHP_EOL; + ConsoleUtils::writeLn('Written to "%s".', $destinationFilename); } // --------------------------------------------------------------------------------------------------------------------- diff --git a/demo/merge-document-basic.php b/demo/merge-document-basic.php index f129cefe..eb39e4f8 100644 --- a/demo/merge-document-basic.php +++ b/demo/merge-document-basic.php @@ -29,5 +29,4 @@ file_put_contents($destinationFilename, $arrayOfBinaryData[0]); -echo sprintf('Written to "%s".', $destinationFilename); -echo PHP_EOL; +ConsoleUtils::writeLn('Written to "%s".', $destinationFilename); diff --git a/demo/merge-document-html.php b/demo/merge-document-html.php index d0bf0e18..e989c80f 100644 --- a/demo/merge-document-html.php +++ b/demo/merge-document-html.php @@ -39,5 +39,4 @@ file_put_contents($destinationFilename, $arrayOfBinaryData[0]); -echo sprintf('Written to "%s".', $destinationFilename); -echo PHP_EOL; +ConsoleUtils::writeLn('Written to "%s".', $destinationFilename); diff --git a/demo/merge-document-image.php b/demo/merge-document-image.php index 04e58008..a50542d6 100644 --- a/demo/merge-document-image.php +++ b/demo/merge-document-image.php @@ -36,5 +36,4 @@ file_put_contents($destinationFilename, $arrayOfBinaryData[0]); -echo sprintf('Written to "%s".', $destinationFilename); -echo PHP_EOL; +ConsoleUtils::writeLn('Written to "%s".', $destinationFilename); diff --git a/demo/proofing.php b/demo/proofing.php index 0ed4b1d0..5349d6c9 100644 --- a/demo/proofing.php +++ b/demo/proofing.php @@ -10,8 +10,8 @@ 'api_key' => ConsoleUtils::apiKey(), ]); -dump($reportingCloud->getAvailableDictionaries()); +ConsoleUtils::dump($reportingCloud->getAvailableDictionaries()); -dump($reportingCloud->getProofingSuggestions('ssky', 'en_US.dic', 10)); +ConsoleUtils::dump($reportingCloud->getProofingSuggestions('ssky', 'en_US.dic', 10)); -dump($reportingCloud->proofingCheck('Thiss is a testt about rockkets in the ssky', 'en_US.dic')); +ConsoleUtils::dump($reportingCloud->proofingCheck('Thiss is a testt about rockkets in the ssky', 'en_US.dic')); diff --git a/demo/run-all.php b/demo/run-all.php index 39401590..cd19a483 100644 --- a/demo/run-all.php +++ b/demo/run-all.php @@ -3,32 +3,46 @@ include_once __DIR__ . '/bootstrap.php'; +use TxTextControl\ReportingCloud\Stdlib\ConsoleUtils; + // --------------------------------------------------------------------------------------------------------------------- /** - * Return an array of filenames which should be executed + * Return an ArrayObject FileInfos which should be executed * - * @return array + * @return ArrayObject * */ -$getFilenames = function (): array { +$getDemos = function (): ArrayObject { + + $arrayObject = new ArrayObject(); + $iterator = new DirectoryIterator(__DIR__); - $ret = []; + $extensions = [ + 'php', + ]; - $di = new DirectoryIterator(__DIR__); - foreach ($di as $fileInfo) { - if (in_array($fileInfo->getFilename(), [basename(__FILE__), 'bootstrap.php', 'init.php'])) { + $skipFiles = [ + basename(__FILE__), + 'bootstrap.php', + 'init.php', + ]; + + foreach ($iterator as $fileInfo) { + if (!in_array($fileInfo->getExtension(), $extensions)) { continue; } - if ('php' !== $fileInfo->getExtension()) { + if (in_array($fileInfo->getFilename(), $skipFiles)) { continue; } - $ret[] = (string) $fileInfo->getPathname(); + $index = $fileInfo->getFilename(); + $value = clone $fileInfo; + $arrayObject->offsetSet($index, $value); } - sort($ret); + $arrayObject->ksort(); - return $ret; + return $arrayObject; }; // --------------------------------------------------------------------------------------------------------------------- @@ -38,28 +52,29 @@ // --------------------------------------------------------------------------------------------------------------------- -$filenames = $getFilenames(); -$count = count($filenames); -$counter = 0; +$counter = 1; +$fileInfos = $getDemos(); -foreach ($filenames as $filename) { +foreach ($fileInfos as $fileInfo) { - $counter++; - - $filename = (string) $filename; - - echo sprintf('%d/%d) Executing "%s"...', $counter, $count, basename($filename)); - echo PHP_EOL; + ConsoleUtils::writeLn( + '%d/%d) Executing "%s"...', + $counter, + $fileInfos->count(), + $fileInfo->getFilename() + ); echo PHP_EOL; - - $command = sprintf('%s %s', PHP_BINARY, escapeshellarg($filename)); + $command = sprintf( + '%s %s', + escapeshellarg(PHP_BINARY), + escapeshellarg($fileInfo->getPathname()) + ); passthru($command); - - echo PHP_EOL; - echo '...DONE.'; - echo PHP_EOL; - echo PHP_EOL; echo PHP_EOL; + ConsoleUtils::writeLn('...DONE.'); + echo PHP_EOL. PHP_EOL; + + $counter++; } // --------------------------------------------------------------------------------------------------------------------- diff --git a/demo/template-management.php b/demo/template-management.php index 209380ca..7052307a 100644 --- a/demo/template-management.php +++ b/demo/template-management.php @@ -24,26 +24,21 @@ // Uploaded, if it is not if (!$reportingCloud->templateExists($templateName)) { - echo sprintf('"%s" is not in template storage', $templateName); - echo PHP_EOL; + ConsoleUtils::writeLn('"%s" is not in template storage', $templateName); if ($reportingCloud->uploadTemplate($sourceFilename)) { - echo sprintf('Uploaded "%s".', $sourceFilename); - echo PHP_EOL; + ConsoleUtils::writeLn('Uploaded "%s".', $sourceFilename); } else { - echo sprintf('Error uploading "%s".', $sourceFilename); - echo PHP_EOL; + ConsoleUtils::writeLn('Error uploading "%s".', $sourceFilename); } } - // --------------------------------------------------------------------------------------------------------------------- // Get the number of pages in a template $pageCount = $reportingCloud->getTemplatePageCount($templateName); -echo sprintf('"%s" contains %d page%s.', $templateName, $pageCount, $pageCount > 1 ? 's' : ''); -echo PHP_EOL; +ConsoleUtils::writeLn('"%s" contains %d page%s.', $templateName, $pageCount, $pageCount > 1 ? 's' : ''); // --------------------------------------------------------------------------------------------------------------------- @@ -52,14 +47,11 @@ $binaryData = $reportingCloud->downloadTemplate($templateName); if ($binaryData) { - echo sprintf('"%s" was downloaded.', $templateName); - echo PHP_EOL; + ConsoleUtils::writeLn('"%s" was downloaded.', $templateName); file_put_contents($destinationFilename, $binaryData); - echo sprintf('"%s" was written to "%s".', $templateName, $destinationFilename); - echo PHP_EOL; + ConsoleUtils::writeLn('"%s" was written to "%s".', $templateName, $destinationFilename); } else { - echo sprintf('Error downloading "%s".', $templateName); - echo PHP_EOL; + ConsoleUtils::writeLn('Error downloading "%s".', $templateName); } // --------------------------------------------------------------------------------------------------------------------- @@ -68,27 +60,22 @@ $templateCount = $reportingCloud->getTemplateCount(); -echo sprintf('There are %d template%s in template storage.', $templateCount, $templateCount > 1 ? 's' : ''); -echo PHP_EOL; +ConsoleUtils::writeLn('There are %d template%s in template storage.', $templateCount, $templateCount > 1 ? 's' : ''); // --------------------------------------------------------------------------------------------------------------------- // Get an array of all templates in template storage -echo "They are as follows:"; -echo PHP_EOL; +ConsoleUtils::writeLn("They are as follows:"); foreach ($reportingCloud->getTemplateList() as $record) { $templateName = $record['template_name']; $modifiedFormatted = date('r', $record['modified']); // modified is a unix timestamp - echo sprintf('- %s', $templateName); - echo PHP_EOL; - echo sprintf('- %s', $record['modified']); - echo PHP_EOL; - echo sprintf('- %s', $modifiedFormatted); - echo PHP_EOL . PHP_EOL; + ConsoleUtils::writeLn('- %s', $templateName); + ConsoleUtils::writeLn('- %s', $record['modified']); + ConsoleUtils::writeLn('- %s', $modifiedFormatted); } // --------------------------------------------------------------------------------------------------------------------- @@ -96,11 +83,9 @@ // Delete a template in template storage if ($reportingCloud->deleteTemplate($templateName)) { - echo sprintf('"%s" was deleted.', $templateName); - echo PHP_EOL; + ConsoleUtils::writeLn('"%s" was deleted.', $templateName); } else { - echo sprintf('Error deleting "%s".', $templateName); - echo PHP_EOL; + ConsoleUtils::writeLn('Error deleting "%s".', $templateName); } // --------------------------------------------------------------------------------------------------------------------- diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ba6bc0a3..df15db73 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -17,9 +17,6 @@ src - - src/Stdlib/ConsoleUtils.php - diff --git a/resource/camera_controls.pdf b/resource/camera_controls.pdf deleted file mode 100644 index 8198a797..00000000 Binary files a/resource/camera_controls.pdf and /dev/null differ diff --git a/resource/maelzel_machine.docx b/resource/maelzel_machine.docx new file mode 100644 index 00000000..8047249d Binary files /dev/null and b/resource/maelzel_machine.docx differ diff --git a/resource/maelzel_machine.pdf b/resource/maelzel_machine.pdf new file mode 100644 index 00000000..9706d84d Binary files /dev/null and b/resource/maelzel_machine.pdf differ diff --git a/src/Stdlib/ConsoleUtils.php b/src/Stdlib/ConsoleUtils.php index 62a1ea3a..49f36e04 100644 --- a/src/Stdlib/ConsoleUtils.php +++ b/src/Stdlib/ConsoleUtils.php @@ -27,7 +27,7 @@ class ConsoleUtils extends AbstractStdlib * * @const REPORTING_CLOUD_API_KEY */ - private const API_KEY = 'REPORTING_CLOUD_API_KEY'; + public const API_KEY = 'REPORTING_CLOUD_API_KEY'; /** * Check that either the API key has been defined in environment variables @@ -118,4 +118,36 @@ public static function errorMessage(): string return $ret; } + + /** + * Dump information about a variable + * (var_dump is wrapped to suppress psalm warning) + * + * @param mixed|null $array + * + * @psalm-suppress ForbiddenCode + */ + public static function dump($array): void + { + var_dump($array); + } + + /** + * Write a line to the console + * + * @param string $format + * @param mixed ...$args + */ + public static function writeLn(string $format, ...$args): void + { + $args = (array) $args; + + if (count($args) > 0) { + echo vsprintf($format, $args); + } else { + echo $format; + } + + echo PHP_EOL; + } } diff --git a/test/Stdlib/ConsoleUtilsTest.php b/test/Stdlib/ConsoleUtilsTest.php new file mode 100644 index 00000000..989ca69e --- /dev/null +++ b/test/Stdlib/ConsoleUtilsTest.php @@ -0,0 +1,118 @@ +assertFalse(ConsoleUtils::checkCredentials()); + + putenv("${key}={$oldValue}"); + + $this->assertTrue(ConsoleUtils::checkCredentials()); + } + + public function testApiKeyFromEnv(): void + { + $key = ConsoleUtils::API_KEY; + $value = 'xxxxxxxxxxxxxxxxxxxx'; + + $oldValue = getenv($key); + + putenv("${key}={$value}"); + + $this->assertEquals(ConsoleUtils::apiKey(), $value); + + putenv("${key}={$oldValue}"); + } + + public function testErrorMessage(): void + { + $errorMessage = ConsoleUtils::errorMessage(); + + $this->assertContains('Error: ReportingCloud API key not defined.', $errorMessage); + $this->assertContains('For further assistance and customer service please refer to:', $errorMessage); + } + + public function testDump(): void + { + ob_start(); + ConsoleUtils::dump([1, 2, 3, 4, 5]); + $actual = ob_get_clean(); + + $this->assertContains('array(5)', $actual); + + $this->assertContains('[0]', $actual); + $this->assertContains('[1]', $actual); + $this->assertContains('[2]', $actual); + $this->assertContains('[3]', $actual); + $this->assertContains('[4]', $actual); + + $this->assertContains('int(1)', $actual); + $this->assertContains('int(2)', $actual); + $this->assertContains('int(3)', $actual); + $this->assertContains('int(4)', $actual); + $this->assertContains('int(5)', $actual); + } + + public function testWriteLineWith0Args(): void + { + $expected = '10 hello "world".' . PHP_EOL; + + ob_start(); + ConsoleUtils::writeLn('10 hello "world".'); + $actual = ob_get_clean(); + + $this->assertEquals($expected, $actual); + } + + public function testWriteLineWith1Arg(): void + { + $expected = '10 x hello.' . PHP_EOL; + + ob_start(); + ConsoleUtils::writeLn('%d x hello.', 10); + $actual = ob_get_clean(); + + $this->assertEquals($expected, $actual); + } + + public function testWriteLineWith2Args(): void + { + $expected = '10 x hello "world".' . PHP_EOL; + + ob_start(); + ConsoleUtils::writeLn('%d x hello "%s".', 10, 'world'); + $actual = ob_get_clean(); + + $this->assertEquals($expected, $actual); + } +}