Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F44414571
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
7 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php
index 5b93e8e..15c7d33 100644
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -1,32 +1,69 @@
<?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\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/TestCase.php b/tests/Console/Commands/TestCase.php
index 22e2a32..d9c1611 100644
--- a/tests/Console/Commands/TestCase.php
+++ b/tests/Console/Commands/TestCase.php
@@ -1,61 +1,37 @@
<?php
namespace Nasqueron\Notifications\Tests\Console\Commands;
-use Symfony\Component\Console\Tester\CommandTester;
-
use Nasqueron\Notifications\Config\Services\Service;
use Nasqueron\Notifications\Tests\TestCase as BaseTestCase;
-use Artisan;
+use Illuminate\Contracts\Console\Kernel;
+use Symfony\Component\Console\Tester\CommandTester;
+
use Mockery;
class TestCase extends BaseTestCase {
///
/// Commands test environment
///
/**
* @var Symfony\Component\Console\Command
*/
protected $command;
/**
* @var Symfony\Component\Console\Tester\CommandTester;
*/
protected $tester;
public function setUp () {
parent::setUp();
- $this->command = $this->findCommand($this->class);
+ $kernel = $this->app->make(Kernel::class);
+ $this->command = $kernel->getByClass($this->class);
$this->tester = new CommandTester($this->command);
}
- ///
- /// Helper methods to manipulate command arrays
- ///
-
- /**
- * Finds the first instance of the expected type in the specified array.
- *
- * @param mixed $expectedType The type to find among the array elements
- * @param array $haystack The array where to find
- * @return mixed|null If not found, null. Otherwise, the found item.
- */
- protected static function findInstanceOf ($expectedType, $haystack) {
- foreach ($haystack as $item) {
- if ($item instanceof $expectedType) {
- return $item;
- }
- }
-
- return null;
- }
-
- protected function findCommand ($expectedType) {
- return self::findInstanceOf($expectedType, Artisan::all());
- }
-
}
diff --git a/tests/Console/KernelTest.php b/tests/Console/KernelTest.php
index 9399da0..cd129d2 100644
--- a/tests/Console/KernelTest.php
+++ b/tests/Console/KernelTest.php
@@ -1,83 +1,118 @@
<?php
namespace Nasqueron\Notifications\Tests\Console;
use Nasqueron\Notifications\Tests\TestCase;
+use Nasqueron\Notifications\Console\Kernel;
+use Illuminate\Contracts\Console\Kernel as BaseKernel;
+
use Artisan;
use File;
class KernelTest extends TestCase {
+ /**
+ * @var \Nasqueron\Notifications\Console\Kernel
+ */
+ private $kernel;
+
/**
* The actual list of services providers
*
* @var string[]
*/
private $commands;
/**
* The service providers' namespace
*
* @var string
*/
private $namespace;
public function setUp () {
parent::setUp();
- $this->commands = Artisan::all();
+ $this->kernel = $this->app->make(BaseKernel::class);
+ $this->commands = $this->kernel->all();
$this->namespace = $this->app->getInstance()->getNamespace()
. 'Console\\Commands\\';
}
public function testOmittedFiles () {
$files = File::allFiles(app_path('Console/Commands'));
foreach ($files as $file) {
$class = $this->namespace . $file->getBasename('.php');
$this->assertArrayContainsInstanceOf(
$class,
$this->commands,
"The class $class should be added to app/Console/Kernel.php."
);
}
}
+ public function testGet () {
+ $this->assertInstanceOf(
+ \Nasqueron\Notifications\Console\Commands\Inspire::class,
+ $this->kernel->get('inspire')
+ );
+ }
+
+ /**
+ * @expectedException \RuntimeException
+ */
+ public function testGetWhenCommandDoesNotExist () {
+ $this->kernel->get('notexisting');
+ }
+
+ public function testGetByClass () {
+ $class = \Nasqueron\Notifications\Console\Commands\Inspire::class;
+ $this->assertInstanceOf($class, $this->kernel->getByClass($class));
+ }
+
+ /**
+ * @expectedException \RuntimeException
+ */
+ public function testGetByClassWhenCommandDoesNotExist () {
+ $this->kernel->getByClass('notexisting');
+ }
+
///
/// Custom assertions
///
/**
* Asserts the specified array contains an element of an expected type.
*
* @param mixed $expectedType The type to find among the array elements
* @param array $haystack The array where to find
* @param string $message The test message
*/
public static function assertArrayContainsInstanceOf ($expectedType, $haystack, $message = '') {
self::assertThat(
self::arrayContainsInstanceOf($expectedType, $haystack),
self::isTrue(),
$message
);
}
/**
* Determines if the specified array contains at least one instance of the
* specified type.
*
* @param mixed $expectedType The type to find among the array elements
* @param array $haystack The array where to find
* @return bool
*/
protected static function arrayContainsInstanceOf ($expectedType, $haystack) {
foreach ($haystack as $item) {
if ($item instanceof $expectedType) {
return true;
}
}
return false;
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Aug 18, 23:09 (1 d, 5 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4006823
Default Alt Text
(7 KB)
Attached To
Mode
rNOTIF Notifications center
Attached
Detach File
Event Timeline
Log In to Comment