Page MenuHomeDevCentral

D795.diff
No OneTemporary

D795.diff

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/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

Mime Type
text/plain
Expires
Sun, Jan 19, 19:40 (19 h, 18 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2361329
Default Alt Text
D795.diff (4 KB)

Event Timeline