1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Unit; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Modulate\Artisan\Interceptor\OptionBuilder; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
|
9
|
|
|
use Symfony\Component\Console\Exception\InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
class OptionBuilderTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
protected OptionBuilder $builder; |
14
|
|
|
public function setUp(): void |
15
|
|
|
{ |
16
|
|
|
parent::setUp(); |
17
|
|
|
$this->builder = new OptionBuilder(); |
18
|
|
|
} |
19
|
|
|
/** |
20
|
|
|
* A basic unit test example. |
21
|
|
|
*/ |
22
|
|
|
public function test_create(): void |
23
|
|
|
{ |
24
|
|
|
$inputOption = $this->builder |
25
|
|
|
->name('foo') |
26
|
|
|
->required() |
27
|
|
|
->description('my new option') |
28
|
|
|
->default('bar') |
29
|
|
|
->get(); |
30
|
|
|
$this->assertInstanceOf(InputOption::class, $inputOption); |
31
|
|
|
$this->assertEquals($inputOption->getName(), 'foo'); |
32
|
|
|
$this->assertEquals($inputOption->getDescription(), 'my new option'); |
33
|
|
|
$this->assertEquals($inputOption->getDefault(), 'bar'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function test_flag(): void |
37
|
|
|
{ |
38
|
|
|
$inputOption = $this->builder |
39
|
|
|
->name('foo') |
40
|
|
|
->flag() |
41
|
|
|
->get(); |
42
|
|
|
$this->assertFalse($inputOption->acceptValue()); |
43
|
|
|
|
44
|
|
|
} |
45
|
|
|
public function test_optional(): void |
46
|
|
|
{ |
47
|
|
|
$inputOption = $this->builder |
48
|
|
|
->name('foo') |
49
|
|
|
->optional(); |
50
|
|
|
$this->assertTrue($inputOption->hasMode(InputOption::VALUE_OPTIONAL)); |
51
|
|
|
$inputOption = $inputOption->get(); |
52
|
|
|
$this->assertFalse($inputOption->isValueRequired()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function test_required(): void |
56
|
|
|
{ |
57
|
|
|
$inputOption = $this->builder |
58
|
|
|
->name('foo') |
59
|
|
|
->required() |
60
|
|
|
->get(); |
61
|
|
|
$this->assertTrue($inputOption->isValueRequired()); |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function test_negatable(): void |
66
|
|
|
{ |
67
|
|
|
$inputOption = $this->builder |
68
|
|
|
->name('foo') |
69
|
|
|
->negatable() |
70
|
|
|
->get(); |
71
|
|
|
$this->assertTrue($inputOption->isNegatable()); |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function test_accepts_array(): void |
76
|
|
|
{ |
77
|
|
|
$inputOption = $this->builder |
78
|
|
|
->name('foo') |
79
|
|
|
->required() |
80
|
|
|
->array() |
81
|
|
|
->get(); |
82
|
|
|
$this->assertTrue($inputOption->isArray()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function test_throws_exception(): void |
86
|
|
|
{ |
87
|
|
|
$this->expectException(InvalidArgumentException::class); |
88
|
|
|
$this->builder |
89
|
|
|
->name('foo') |
90
|
|
|
->array() |
91
|
|
|
->get(); |
92
|
|
|
|
93
|
|
|
$this->expectException(InvalidArgumentException::class); |
94
|
|
|
$this->builder |
95
|
|
|
->name('foo') |
96
|
|
|
->flag() |
97
|
|
|
->required() |
98
|
|
|
->get(); |
99
|
|
|
|
100
|
|
|
$this->expectException(InvalidArgumentException::class); |
101
|
|
|
$this->builder |
102
|
|
|
->name('foo') |
103
|
|
|
->negatable() |
104
|
|
|
->required() |
105
|
|
|
->get(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
} |