1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests; |
4
|
|
|
|
5
|
|
|
use LaravelZero\Framework\Commands\Command; |
6
|
|
|
use NunoMaduro\LaravelDesktopNotifier\Contracts\Notifier; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
|
9
|
|
|
class BaseCommandTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
/** @test */ |
12
|
|
|
public function it_has_a_container_getter(): void |
13
|
|
|
{ |
14
|
|
|
$command = $this->makeCommand(); |
15
|
|
|
|
16
|
|
|
$this->assertSame($command->getContainer(), app()); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** @test */ |
20
|
|
|
public function it_allows_notifications(): void |
21
|
|
|
{ |
22
|
|
|
$command = $this->makeCommand(); |
23
|
|
|
|
24
|
|
|
$notifierMock = $this->createMock(Notifier::class); |
25
|
|
|
|
26
|
|
|
$notifierMock->expects($this->once())->method('send')->with( |
27
|
|
|
$this->callback( |
28
|
|
|
function ($notification) { |
29
|
|
|
return $notification->getTitle() === 'foo' && $notification->getBody() === 'bar'; |
30
|
|
|
} |
31
|
|
|
) |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
app()->instance(Notifier::class, $notifierMock); |
35
|
|
|
|
36
|
|
|
$command->notify('foo', 'bar'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** @test */ |
40
|
|
View Code Duplication |
public function it_allows_success_tasks(): void |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$command = $this->makeCommand(); |
43
|
|
|
|
44
|
|
|
$outputMock = $this->createMock(OutputInterface::class); |
45
|
|
|
$outputMock->expects($this->once())->method('writeln')->with( |
46
|
|
|
'foo: <info>✔</info>' |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$command->setOutput($outputMock); |
50
|
|
|
|
51
|
|
|
$command->task( |
52
|
|
|
'foo', function () { |
53
|
|
|
return true; |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @test */ |
58
|
|
View Code Duplication |
public function it_allows_fail_tasks(): void |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$command = $this->makeCommand(); |
61
|
|
|
|
62
|
|
|
$outputMock = $this->createMock(OutputInterface::class); |
63
|
|
|
$outputMock->expects($this->once())->method('writeln')->with( |
64
|
|
|
'bar: <error>failed</error>' |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$command->setOutput($outputMock); |
68
|
|
|
|
69
|
|
|
$command->task( |
70
|
|
|
'bar', function () { |
71
|
|
|
return false; |
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function makeCommand() |
76
|
|
|
{ |
77
|
|
|
$command = new class extends Command |
78
|
|
|
{ |
79
|
|
|
protected $name = 'foo:bar'; |
80
|
|
|
|
81
|
|
|
public function handle(): void |
82
|
|
|
{ |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function setOutput($output) |
86
|
|
|
{ |
87
|
|
|
$this->output = $output; |
88
|
|
|
} |
89
|
|
|
}; |
90
|
|
|
|
91
|
|
|
$this->app->add($command); |
92
|
|
|
|
93
|
|
|
return $command; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.