|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Commands; |
| 4 | + |
| 5 | +use Illuminate\Testing\PendingCommand; |
| 6 | +use Mockery; |
| 7 | +use Mockery\MockInterface; |
| 8 | +use Tests\TestCase; |
| 9 | +use UKFast\HealthCheck\Checks\DatabaseHealthCheck; |
| 10 | +use UKFast\HealthCheck\Checks\LogHealthCheck; |
| 11 | +use UKFast\HealthCheck\HealthCheckServiceProvider; |
| 12 | +use UKFast\HealthCheck\Status; |
| 13 | + |
| 14 | +class StatusCommandTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @test |
| 18 | + */ |
| 19 | + public function running_command_status() |
| 20 | + { |
| 21 | + $this->app->register(HealthCheckServiceProvider::class); |
| 22 | + config(['healthcheck.checks' => [LogHealthCheck::class]]); |
| 23 | + |
| 24 | + $status = new Status(); |
| 25 | + $status->okay(); |
| 26 | + $this->mockLogHealthCheck($status); |
| 27 | + |
| 28 | + $result = $this->artisan('health-check:status'); |
| 29 | + |
| 30 | + if ($result instanceof PendingCommand) { |
| 31 | + $result->assertExitCode(0); |
| 32 | + } else { |
| 33 | + $this->assertTrue(true); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @test |
| 39 | + */ |
| 40 | + public function running_command_status_with_only_option() |
| 41 | + { |
| 42 | + $this->app->register(HealthCheckServiceProvider::class); |
| 43 | + |
| 44 | + $status = new Status(); |
| 45 | + $status->okay(); |
| 46 | + $this->mockLogHealthCheck($status); |
| 47 | + |
| 48 | + $result = $this->artisan('health-check:status', ['--only' => 'log']); |
| 49 | + |
| 50 | + if ($result instanceof PendingCommand) { |
| 51 | + $result->assertExitCode(0); |
| 52 | + } else { |
| 53 | + $this->assertTrue(true); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @test |
| 59 | + */ |
| 60 | + public function running_command_status_with_except_option() |
| 61 | + { |
| 62 | + $this->app->register(HealthCheckServiceProvider::class); |
| 63 | + config(['healthcheck.checks' => [LogHealthCheck::class, DatabaseHealthCheck::class]]); |
| 64 | + |
| 65 | + $status = new Status(); |
| 66 | + $status->okay(); |
| 67 | + $this->mockLogHealthCheck($status); |
| 68 | + |
| 69 | + $result = $this->artisan('health-check:status', ['--except' => 'database']); |
| 70 | + |
| 71 | + if ($result instanceof PendingCommand) { |
| 72 | + $result->assertExitCode(0); |
| 73 | + } else { |
| 74 | + $this->assertTrue(true); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @test |
| 80 | + */ |
| 81 | + public function running_command_status_with_only_and_except_option() |
| 82 | + { |
| 83 | + $this->app->register(HealthCheckServiceProvider::class); |
| 84 | + |
| 85 | + $result = $this->artisan('health-check:status', ['--only' => 'log', '--except' => 'log']); |
| 86 | + |
| 87 | + if ($result instanceof PendingCommand) { |
| 88 | + $result |
| 89 | + ->assertExitCode(1) |
| 90 | + ->expectsOutput('Pass --only OR --except, but not both!'); |
| 91 | + } else { |
| 92 | + $this->assertTrue(true); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @test |
| 98 | + */ |
| 99 | + public function running_command_status_with_failure_condition() |
| 100 | + { |
| 101 | + $this->app->register(HealthCheckServiceProvider::class); |
| 102 | + config(['healthcheck.checks' => [LogHealthCheck::class]]); |
| 103 | + $status = new Status(); |
| 104 | + $status->withName('statusName')->problem('statusMessage'); |
| 105 | + $this->mockLogHealthCheck($status); |
| 106 | + |
| 107 | + $result = $this->artisan('health-check:status'); |
| 108 | + |
| 109 | + if ($result instanceof PendingCommand) { |
| 110 | + $result->assertExitCode(1); |
| 111 | + //for laravel 5.* |
| 112 | + if (method_exists($result, 'expectsTable')) { |
| 113 | + $result->expectsTable(['name', 'status', 'message'], [['log', 'statusName', 'statusMessage']]); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private function mockLogHealthCheck(Status $status) |
| 119 | + { |
| 120 | + $this->instance( |
| 121 | + LogHealthCheck::class, |
| 122 | + Mockery::mock(LogHealthCheck::class, function (MockInterface $mock) use ($status) { |
| 123 | + $mock->shouldReceive('name')->andReturn('log'); |
| 124 | + $mock->shouldReceive('status')->andReturn($status); |
| 125 | + }) |
| 126 | + ); |
| 127 | + } |
| 128 | +} |
0 commit comments