diff --git a/app/Console/Commands/ConfigValidate.php b/app/Console/Commands/ConfigValidate.php new file mode 100644 --- /dev/null +++ b/app/Console/Commands/ConfigValidate.php @@ -0,0 +1,54 @@ +disk('local'); + } + + private function getConfigFiles () : array { + return array_filter( + $this->getFS()->allFiles(), + + // Filters *.json + function ($file) : bool { + return substr($file, -5) === ".json"; + } + ); + } + + /** + * Executes the console command. + */ + public function handle() : void { + $files = $this->getConfigFiles(); + + foreach ($files as $file) { + $content = $this->getFS()->get($file); + if (json_decode($content) === null) { + $this->line("$file — " . json_last_error_msg()); + } + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -15,6 +15,7 @@ */ protected $commands = [ \Nasqueron\Notifications\Console\Commands\ConfigShow::class, + \Nasqueron\Notifications\Console\Commands\ConfigValidate::class, \Nasqueron\Notifications\Console\Commands\Inspire::class, \Nasqueron\Notifications\Console\Commands\NotificationsPayload::class, \Nasqueron\Notifications\Console\Commands\PhabricatorProjectsMap::class, diff --git a/tests/Console/Commands/ConfigValidateTest.php b/tests/Console/Commands/ConfigValidateTest.php new file mode 100644 --- /dev/null +++ b/tests/Console/Commands/ConfigValidateTest.php @@ -0,0 +1,60 @@ +tester->execute(['command' => $this->command->getName()]); + + // When all files are valid, nothing is displayed + $this->assertEquals('', $this->tester->getDisplay()); + } + + /** + * @dataProvider provideErrors + */ + public function testSyntaxErrorExecute (string $content, string $error) { + $this->populateTestFile($content); // Not JSON + + $this->tester->execute(['command' => $this->command->getName()]); + + // When all files are valid, nothing is displayed + $this->assertRegexp("/$error/", $this->tester->getDisplay()); + } + + /** + * Provides invalid JSON strings and associated error + */ + public function provideErrors () : array { + return [ + ["lorem ipsum dolor", "Syntax error"], + ['{"}', "Control character error"] + ]; + } + + private function populateTestFile (string $content) : void { + Storage::disk('local')->put(self::TEST_FILE, $content); + } + + private function deleteTestFile () : void { + $fs = Storage::disk('local'); + if ($fs->exists(self::TEST_FILE)) { + $fs->delete(self::TEST_FILE); + } + } + + public function tearDown () { + $this->deleteTestFile(); + parent::tearDown(); + } +}