Skip to content

Commit

Permalink
Laravel 10 support; fixed cmd event mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
pecuchet committed Apr 13, 2023
1 parent 8f76516 commit 3555ccc
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Expand Up @@ -11,7 +11,7 @@ jobs:
fail-fast: true
matrix:
php: [ 8.0 ]
laravel: [ "^9.0" ]
laravel: [ "^10.6", "^9.0" ]
stability: [prefer-lowest, prefer-stable]

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }}
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Expand Up @@ -17,18 +17,18 @@
],
"require": {
"php": "^8.0",
"illuminate/config": "^9.0",
"illuminate/console": "^9.0",
"illuminate/contracts": "^9.0",
"illuminate/routing": "^9.0",
"illuminate/support": "^9.0",
"nesbot/carbon": "^1 || ^2"
"illuminate/config": "^9.0 || ^10.6",
"illuminate/console": "^9.0 || ^10.6",
"illuminate/contracts": "^9.0 || ^10.6",
"illuminate/routing": "^9.0 || ^10.6",
"illuminate/support": "^9.0 || ^10.6",
"nesbot/carbon": "^2"
},
"require-dev": {
"brianium/paratest": "^6.2",
"friendsofphp/php-cs-fixer": "^3.16",
"nunomaduro/collision": "^6.2",
"orchestra/testbench": "^7.24",
"orchestra/testbench": "^7.24 || ^8.3",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.3",
"vimeo/psalm": "^4.4"
Expand Down
24 changes: 17 additions & 7 deletions tests/FeatureTest.php
Expand Up @@ -5,10 +5,14 @@
use Brightfish\HealthChecks\Exceptions\BaseHealthException;
use Brightfish\HealthChecks\Exceptions\NonCheckException;
use Brightfish\HealthChecks\Services\HealthService;
use Brightfish\HealthChecks\Tests\Mocks\MockCommand;
use Brightfish\HealthChecks\Tests\TestChecks\InValidCheck;
use Brightfish\HealthChecks\Tests\TestChecks\NonCheck;
use Brightfish\HealthChecks\Tests\TestChecks\ValidCheck;
use Illuminate\Console\Events\CommandFinished;
use Illuminate\Contracts\Console\Kernel;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\ConsoleOutput;

class FeatureTest extends TestCase
{
Expand Down Expand Up @@ -64,18 +68,24 @@ public function multiple_checks_throw_error()
/** @test */
public function cmds_in_namespace_should_be_logged()
{
$this->app->config->set('health.artisan.cmd_namespace', 'Illuminate\Foundation\Console\ClosureCommand');
// Change the namespace to the mocked commands one.
$this->app['config']->set('health.artisan.cmd_namespace', 'Brightfish\HealthChecks\Tests\Mocks');

$artisan = $this->app->make(Kernel::class);
// From Laravel 10 Console commands aren't triggered during testing, so we trigger the event manually
// and we only test the listener in the HealthServiceProvider, instead of the whole feature.

$artisan->command('time-test', function () {
return true;
});
$input = new StringInput('');

$artisan->call('time-test');
$output = new ConsoleOutput();

event(new CommandFinished('health:mock', $input, $output, 0));

//$artisan = $this->app->make(Kernel::class);

//$artisan->call(MockCommand::class);

$this->assertIsInt(
$this->app[HealthService::class]->getTime('time-test')
$this->app[HealthService::class]->getTime('health:mock')
);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/Mocks/MockCommand.php
@@ -0,0 +1,25 @@
<?php

namespace Brightfish\HealthChecks\Tests\Mocks;

use Brightfish\HealthChecks\Services\HealthService;
use Illuminate\Console\Command;

class MockCommand extends Command
{
/** @inheritdoc */
public $signature = 'health:mock';

/** @inheritdoc */
public $description = 'Mocking';

/**
* Run the checks.
* @param HealthService $healthService
* @return int
*/
public function handle(HealthService $healthService): int
{
return 0;
}
}
6 changes: 6 additions & 0 deletions tests/TestCase.php
Expand Up @@ -3,12 +3,18 @@
namespace Brightfish\HealthChecks\Tests;

use Brightfish\HealthChecks\HealthServiceProvider;
use Brightfish\HealthChecks\Tests\Mocks\MockCommand;
use Illuminate\Console\Application as Artisan;
use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
{
public function setUp(): void
{
Artisan::starting(function ($artisan) {
$artisan->resolveCommands([MockCommand::class]);
});

parent::setUp();
}

Expand Down

0 comments on commit 3555ccc

Please sign in to comment.