Page MenuHomeDevCentral

No OneTemporary

diff --git a/app/Console/Commands/ConfigValidate.php b/app/Console/Commands/ConfigValidate.php
new file mode 100644
index 0000000..7199951
--- /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
index 15c7d33..7d50181 100644
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -1,69 +1,70 @@
<?php
namespace Nasqueron\Notifications\Console;
use Illuminate\Console\Command;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var string[]
*/
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,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule (Schedule $schedule) : void {
$schedule->command('inspire')
->hourly();
}
/**
* Gets a command by name
*
* @param string $name The command name (first word of the command signature)
* @return \Illuminate\Console\Command
* @throws \RuntimeException when command doesn't exit
*/
public function get (string $name) : Command {
$commands = $this->all();
if (array_key_exists($name, $commands)) {
return $commands[$name];
}
throw new \RuntimeException("Command $name doesn't exist.");
}
/**
* Gets a command by class
*
* @param string $name The command class
* @return \Illuminate\Console\Command
* @throws \RuntimeException when command doesn't exit
*/
public function getByClass (string $class) : Command {
$commands = $this->all();
foreach ($commands as $command) {
if ($command instanceof $class) {
return $command;
}
}
throw new \RuntimeException("Command $class doesn't exist.");
}
}
diff --git a/tests/Console/Commands/ConfigValidateTest.php b/tests/Console/Commands/ConfigValidateTest.php
new file mode 100644
index 0000000..7444095
--- /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/x-diff
Expires
Mon, Nov 25, 10:25 (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2259995
Default Alt Text
(5 KB)

Event Timeline