diff --git a/app/Config/Reporting/BaseReportEntry.php b/app/Config/Reporting/BaseReportEntry.php new file mode 100644 index 0000000..e4afb2d --- /dev/null +++ b/app/Config/Reporting/BaseReportEntry.php @@ -0,0 +1,49 @@ +<?php + +namespace Nasqueron\Notifications\Config\Reporting; + +abstract class BaseReportEntry { + + /// + /// Format + /// + + public abstract function toArray () : array; + public abstract function toFancyArray () : array; + + /// + /// Format helper methods + /// + + /** + * Returns a fancy string for reports. + * + * @param string $string The source string + * @param string $emptyStringGlyph The glyph to use if the string is empty + * @return string + */ + public static function fancyString (string $string, string $emptyStringGlyph) : string { + if ($string === "") { + return $emptyStringGlyph; + } + + return $string; + } + + /** + * Returns a fancy representation from a boolean for reports. + * + * @param bool $value The source value + * @param string $truthyStringGlyph The glyph to use if the value is true + * @param string $falsyStringGlyph The glyph to use if the value is false [facultative, by default an empty string] + * @return string The relevant glyph + */ + public static function fancyBool (bool $value, string $truthyStringGlyph, string $falsyStringGlyph = '') : string { + if ($value) { + return $truthyStringGlyph; + } + + return $falsyStringGlyph; + } + +} diff --git a/app/Config/Reporting/FeatureReportEntry.php b/app/Config/Reporting/FeatureReportEntry.php index 3f147c6..7c3b3c3 100644 --- a/app/Config/Reporting/FeatureReportEntry.php +++ b/app/Config/Reporting/FeatureReportEntry.php @@ -1,36 +1,64 @@ <?php namespace Nasqueron\Notifications\Config\Reporting; -class FeatureReportEntry { +final class FeatureReportEntry extends BaseReportEntry { /// /// Public properties /// /** * @var string */ public $name; /** * @var bool */ public $enabled; /// /// Constructor /// /** * Initializes a new instance of the FeatureReportEntry class. * * @var name The feature name * @var bool If the feature enabled, true. Otherwise, false. */ public function __construct (string $name, bool $enabled) { $this->name = $name; $this->enabled = $enabled; } + /// + /// Format + /// + + /** + * Gets the entry as an array. + * + * @return string[] + */ + public function toArray () : array { + return [ + $this->name, + (string)$this->enabled, + ]; + } + + /** + * Gets the entry as an array. Formats empty string. + * + * @return string[] + */ + public function toFancyArray () : array { + return [ + $this->name, + self::fancyBool($this->enabled, '✓'), + ]; + } + } diff --git a/app/Config/Reporting/ServiceReportEntry.php b/app/Config/Reporting/ServiceReportEntry.php index d91f41b..4dde243 100644 --- a/app/Config/Reporting/ServiceReportEntry.php +++ b/app/Config/Reporting/ServiceReportEntry.php @@ -1,102 +1,134 @@ <?php namespace Nasqueron\Notifications\Config\Reporting; use Nasqueron\Notifications\Config\Services\Service; use ProjectsMap; -class ServiceReportEntry { +final class ServiceReportEntry extends BaseReportEntry { /// /// Private members /// /** * @var Service */ private $service; /// /// Public properties /// /** * @var string */ public $gate; /** * @var string */ public $door; /** * @var string */ public $instance; /** * @var string */ public $status = ""; /// /// Constructor /// /** * Initializes a new instance of the ServiceReportEntry class. * * @param \Nasqueron\Notifications\Config\Services\Service $service The service */ public function __construct (Service $service) { $this->service = $service; $this->query(); } /// /// Report builder /// /** * Queries the service to fill public properties. */ protected function query () : void { // Direct properties $this->gate = $this->service->gate; $this->door = $this->service->door; $this->instance = (string)$this->service->instance; // Properties to query with business logic $this->status = $this->getServiceStatus(); } /** * @return string An issue to fix, or an empty string if all looks good. */ protected function getServiceStatus () : string { if ($this->isPhabricatorServiceWithNotCachedProjectsMap()) { return "Projects map not cached."; } return ""; } /** * Determines if the service matches the following issue to report: * - service is Phabricator * - instance doesn't have the projects' name/PHID map in cache * * @return bool */ protected function isPhabricatorServiceWithNotCachedProjectsMap () : bool { if ($this->service->gate !== 'Phabricator') { return false; } $map = ProjectsMap::fetch($this->service->door); return !$map->isCached(); } + /// + /// Format + /// + + /** + * Gets the entry as an array. Formats empty string. + * + * @return string[] + */ + public function toArray () : array { + return [ + $this->gate, + $this->door, + $this->instance, + $this->status, + ]; + } + + /** + * Gets the entry as an array. Formats empty string. + * + * @return string[] + */ + public function toFancyArray () : array { + return [ + $this->gate, + $this->door, + self::fancyString($this->instance, 'ø'), + self::fancyString($this->status, '✓'), + ]; + } + } diff --git a/app/Console/Commands/ConfigShow.php b/app/Console/Commands/ConfigShow.php index 4a2aac7..2d99b54 100644 --- a/app/Console/Commands/ConfigShow.php +++ b/app/Console/Commands/ConfigShow.php @@ -1,131 +1,105 @@ <?php namespace Nasqueron\Notifications\Console\Commands; use Illuminate\Console\Command; -use Nasqueron\Notifications\Config\Features; -use Nasqueron\Notifications\Config\Services\Service; - -use Config; -use ProjectsMap; -use Services; +use Nasqueron\Notifications\Config\Reporting\ConfigReport; class ConfigShow extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'config:show'; /** * The console command description. * * @var string */ protected $description = 'Show notifications center configuration'; /** - * Creates a new command instance. + * @var \Nasqueron\Notifications\Config\Reporting\ConfigReport */ - public function __construct () { - parent::__construct(); - } + private $report; /// /// Prepare information tables /// /** * Gets the services (defined in credentials.json) as table rows. * - * @return \Nasqueron\Notifications\Config\Services\Service[] + * @return array */ protected function getServicesTableRows () : array { $rows = []; - foreach (Services::get() as $service) { - $rows[] = [ - $service->gate, - $service->door, - $service->getInstanceName(), - $this->getServiveStatus($service) - ]; - } - return $rows; - } - /** - * Gets service status. - * - * @param \Nasqueron\Notifications\Config\Services\Service $service The service to check - * @return string A description of the issue if something is wrong; otherwise, "✓". - */ - protected function getServiveStatus (Service $service) : string { - if ($service->gate === 'Phabricator') { - // Ensure the projects map is cached - $map = \ProjectsMap::fetch($service->door); - if (!$map->isCached()) { - return "Projects map not cached."; - } + foreach ($this->report->services as $service) { + $rows[] = $service->toFancyArray(); } - return "✓"; + return $rows; } /** * Gets features as table rows * * @return array */ protected function getFeaturesTableRows () : array { $rows = []; - foreach (Features::getAll() as $key => $value) { - if ($value) { - $checkMark = '✓'; - } else { - $checkMark = ''; - } - - $rows[] = [$key, $checkMark]; + + foreach ($this->report->features as $feature) { + $rows[] = $feature->toFancyArray(); } + return $rows; } /// /// Handle the command /// /** * Executes the console command. */ public function handle () : void { + $this->prepareReport(); + $this->printGates(); $this->printFeatures(); $this->printServices(); } + protected final function prepareReport() : void { + $this->report = new ConfigReport(); + } + protected final function printGates () : void { $this->info("Gates:\n"); - foreach (Config::get('gate.controllers') as $gate) { + foreach ($this->report->gates as $gate) { $this->line('- ' . $gate); } } protected final function printFeatures () : void { $this->info("\nFeatures:\n"); $this->table( ['Feature', 'Enabled'], $this->getFeaturesTableRows() ); } protected final function printServices () : void { $this->info("\nServices declared in credentials:\n"); $this->table( ['Gate', 'Door', 'Instance', 'Status'], $this->getServicesTableRows() ); } } diff --git a/tests/Config/Reporting/BaseReportEntryTest.php b/tests/Config/Reporting/BaseReportEntryTest.php new file mode 100644 index 0000000..f6177b8 --- /dev/null +++ b/tests/Config/Reporting/BaseReportEntryTest.php @@ -0,0 +1,26 @@ +<?php + +namespace Nasqueron\Notifications\Tests\Config\Reporting; + +use Nasqueron\Notifications\Config\Reporting\BaseReportEntry; +use Nasqueron\Notifications\Tests\TestCase; + +class BaseReportEntryTest extends TestCase { + + public function testFancyString() { + $this->assertSame('ø', BaseReportEntry::fancyString('', 'ø')); + $this->assertSame('ø', BaseReportEntry::fancyString('ø', 'ø')); + $this->assertSame('o', BaseReportEntry::fancyString('o', 'ø')); + $this->assertSame('', BaseReportEntry::fancyString('', '')); + } + + public function testFancyBool() { + $this->assertSame('ø', BaseReportEntry::fancyBool(false, '✓', 'ø')); + $this->assertSame('✓', BaseReportEntry::fancyBool(true, '✓', 'ø')); + $this->assertSame('', BaseReportEntry::fancyBool(false, '✓')); + $this->assertSame('✓', BaseReportEntry::fancyBool(true, '✓')); + $this->assertSame('', BaseReportEntry::fancyBool(true, '', '')); + $this->assertSame('', BaseReportEntry::fancyBool(false, '', '')); + } + +} diff --git a/tests/Config/Reporting/FeatureReportEntryTest.php b/tests/Config/Reporting/FeatureReportEntryTest.php new file mode 100644 index 0000000..2730d95 --- /dev/null +++ b/tests/Config/Reporting/FeatureReportEntryTest.php @@ -0,0 +1,48 @@ +<?php + +namespace Nasqueron\Notifications\Tests\Config\Reporting; + +use Nasqueron\Notifications\Config\Reporting\FeatureReportEntry; +use Nasqueron\Notifications\Tests\TestCase; + +class FeatureReportEntryTest extends TestCase { + + /** + * @var FeatureReportEntry + */ + private $enabledFeatureEntry; + + /** + * @var FeatureReportEntry + */ + private $disabledFeatureEntry; + + public function setUp () { + $this->enabledFeatureEntry = new FeatureReportEntry("foo", true); + $this->disabledFeatureEntry = new FeatureReportEntry("bar", false); + } + + public function testToArray() { + $this->assertSame( + ["foo", (string)true], + $this->enabledFeatureEntry->toArray() + + ); + $this->assertSame( + ["bar", (string)false], + $this->disabledFeatureEntry->toArray() + ); + } + + public function testToFancyArray() { + $this->assertSame( + ["foo", "✓"], + $this->enabledFeatureEntry->toFancyArray() + ); + $this->assertSame( + ["bar", ""], + $this->disabledFeatureEntry->toFancyArray() + ); + } + +} diff --git a/tests/Config/Reporting/ServiceReportEntryTest.php b/tests/Config/Reporting/ServiceReportEntryTest.php new file mode 100644 index 0000000..e536293 --- /dev/null +++ b/tests/Config/Reporting/ServiceReportEntryTest.php @@ -0,0 +1,35 @@ +<?php + +namespace Nasqueron\Notifications\Tests\Config\Reporting; + +use Nasqueron\Notifications\Config\Reporting\ServiceReportEntry; +use Nasqueron\Notifications\Config\Services\Service; +use Nasqueron\Notifications\Tests\TestCase; + +class ServiceReportEntryTest extends TestCase { + + /** + * @var ServiceReportEntry + */ + private $serviceEntry; + + public function setUp () { + $service = $this->mockService(); + $this->serviceEntry = new ServiceReportEntry($service); + } + + public function testToArray() { + $this->assertSame( + ["Storm", "Acme", "http://www.perdu.com", ""], + $this->serviceEntry->toArray() + ); + } + + public function testToFancyArray() { + $this->assertSame( + ["Storm", "Acme", "http://www.perdu.com", "✓"], + $this->serviceEntry->toFancyArray() + ); + } + +}