Page MenuHomeDevCentral

D949.diff
No OneTemporary

D949.diff

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 @@
+<?php
+
+namespace Nasqueron\Notifications\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Filesystem\FilesystemAdapter;
+
+use App;
+
+class ConfigValidate extends Command {
+
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'config:validate';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Validates JSON configuration files';
+
+ private function getFS () : FilesystemAdapter {
+ return App::make('filesystem')->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 @@
+<?php
+
+namespace Nasqueron\Notifications\Tests\Console\Commands;
+
+use Storage;
+
+class ConfigValidateTest extends TestCase {
+
+ /**
+ * @var string
+ */
+ protected $class = 'Nasqueron\Notifications\Console\Commands\ConfigValidate';
+
+ const TEST_FILE = 'bug.json';
+
+ public function testRegularExecute () {
+ $this->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();
+ }
+}

File Metadata

Mime Type
text/plain
Expires
Tue, Nov 19, 00:01 (9 h, 37 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2251509
Default Alt Text
D949.diff (3 KB)

Event Timeline