Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F4020167
D795.id2015.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
D795.id2015.diff
View Options
diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -2,6 +2,7 @@
namespace Nasqueron\Notifications\Console;
+use Illuminate\Console\Command;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@@ -29,4 +30,40 @@
$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/ConfigShowTest.php b/tests/Console/Commands/ConfigShowTest.php
--- a/tests/Console/Commands/ConfigShowTest.php
+++ b/tests/Console/Commands/ConfigShowTest.php
@@ -20,9 +20,11 @@
private $servicesMock;
public function setUp () {
+ $that = $this;
+ $this->afterApplicationCreated(function () use ($that) {
+ $that->servicesMock = $that->mockServices();
+ });
parent::setUp();
-
- $this->servicesMock = $this->mockServices();
}
public function testRegularExecute () {
diff --git a/tests/Console/Commands/TestCase.php b/tests/Console/Commands/TestCase.php
--- a/tests/Console/Commands/TestCase.php
+++ b/tests/Console/Commands/TestCase.php
@@ -2,12 +2,12 @@
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 {
@@ -29,33 +29,9 @@
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
--- a/tests/Console/KernelTest.php
+++ b/tests/Console/KernelTest.php
@@ -4,11 +4,19 @@
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[]
@@ -25,7 +33,8 @@
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\\';
}
@@ -43,6 +52,32 @@
}
}
+ 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
///
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Jan 20, 00:21 (17 h, 54 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2361455
Default Alt Text
D795.id2015.diff (5 KB)
Attached To
Mode
D795: Allow to get a specified command from console Kernel
Attached
Detach File
Event Timeline
Log In to Comment