6
6
use Illuminate \Console \Command ;
7
7
use Illuminate \Console \OutputStyle ;
8
8
use Illuminate \Console \View \Components \Factory ;
9
+ use Laravel \Prompts \Prompt ;
9
10
use Mockery as m ;
10
11
use PHPUnit \Framework \Attributes \DataProvider ;
11
12
use PHPUnit \Framework \TestCase ;
@@ -23,8 +24,10 @@ protected function tearDown(): void
23
24
}
24
25
25
26
#[DataProvider('selectDataProvider ' )]
26
- public function testSelectFallback ($ prompt , $ expectedDefault , $ selection , $ expectedReturn )
27
+ public function testSelectFallback ($ prompt , $ expectedOptions , $ expectedDefault , $ return , $ expectedReturn )
27
28
{
29
+ Prompt::fallbackWhen (true );
30
+
28
31
$ command = new class ($ prompt ) extends Command
29
32
{
30
33
public $ answer ;
@@ -42,8 +45,8 @@ public function handle()
42
45
43
46
$ this ->runCommand ($ command , fn ($ components ) => $ components
44
47
->expects ('choice ' )
45
- ->withArgs ( fn ( $ question , $ options , $ default ) => $ default === $ expectedDefault )
46
- ->andReturnUsing ( fn ( $ question , $ options , $ default ) => $ options [ $ selection ] )
48
+ ->with ( ' Test ' , $ expectedOptions , $ expectedDefault )
49
+ ->andReturn ( $ return )
47
50
);
48
51
49
52
$ this ->assertSame ($ expectedReturn , $ command ->answer );
@@ -52,18 +55,20 @@ public function handle()
52
55
public static function selectDataProvider ()
53
56
{
54
57
return [
55
- 'list with no default ' => [fn () => select ('foo ' , ['a ' , 'b ' , 'c ' ]), null , 1 , 'b ' ],
56
- 'numeric keys with no default ' => [fn () => select ('foo ' , [1 => 'a ' , 2 => 'b ' , 3 => 'c ' ]), null , 1 , 2 ],
57
- 'assoc with no default ' => [fn () => select ('foo ' , ['a ' => 'A ' , 'b ' => 'B ' , 'c ' => 'C ' ]), null , 1 , 'b ' ],
58
- 'list with default ' => [fn () => select ('foo ' , ['a ' , 'b ' , 'c ' ], 'b ' ), 1 , 1 , 'b ' ],
59
- 'numeric keys with default ' => [fn () => select ('foo ' , [1 => 'a ' , 2 => 'b ' , 3 => 'c ' ], 2 ), 1 , 1 , 2 ],
60
- 'assoc with default ' => [fn () => select ('foo ' , ['a ' => 'A ' , 'b ' => 'B ' , 'c ' => 'C ' ], 'b ' ), 1 , 1 , 'b ' ],
58
+ 'list with no default ' => [fn () => select ('Test ' , ['a ' , 'b ' , 'c ' ]), [ ' a ' , ' b ' , ' c ' ], null , ' b ' , 'b ' ],
59
+ 'numeric keys with no default ' => [fn () => select ('Test ' , [1 => 'a ' , 2 => 'b ' , 3 => 'c ' ]), [ 1 => ' a ' , 2 => ' b ' , 3 => ' c ' ], null , ' 2 ' , 2 ],
60
+ 'assoc with no default ' => [fn () => select ('Test ' , ['a ' => 'A ' , 'b ' => 'B ' , 'c ' => 'C ' ]), [ ' a ' => ' A ' , ' b ' => ' B ' , ' c ' => ' C ' ], null , ' b ' , 'b ' ],
61
+ 'list with default ' => [fn () => select ('Test ' , ['a ' , 'b ' , 'c ' ], 'b ' ), [ ' a ' , ' b ' , ' c ' ], ' b ' , ' b ' , 'b ' ],
62
+ 'numeric keys with default ' => [fn () => select ('Test ' , [1 => 'a ' , 2 => 'b ' , 3 => 'c ' ], 2 ), [ 1 => ' a ' , 2 => ' b ' , 3 => ' c ' ], 2 , ' 2 ' , 2 ],
63
+ 'assoc with default ' => [fn () => select ('Test ' , ['a ' => 'A ' , 'b ' => 'B ' , 'c ' => 'C ' ], 'b ' ), [ ' a ' => ' A ' , ' b ' => ' B ' , ' c ' => ' C ' ], ' b ' , ' b ' , 'b ' ],
61
64
];
62
65
}
63
66
64
67
#[DataProvider('multiselectDataProvider ' )]
65
- public function testMultiselectFallback ($ prompt , $ expectedDefault , $ selection , $ expectedReturn )
68
+ public function testMultiselectFallback ($ prompt , $ expectedOptions , $ expectedDefault , $ return , $ expectedReturn )
66
69
{
70
+ Prompt::fallbackWhen (true );
71
+
67
72
$ command = new class ($ prompt ) extends Command
68
73
{
69
74
public $ answer ;
@@ -81,8 +86,8 @@ public function handle()
81
86
82
87
$ this ->runCommand ($ command , fn ($ components ) => $ components
83
88
->expects ('choice ' )
84
- ->withArgs ( fn ( $ question , $ options , $ default , $ multiple ) => $ default === $ expectedDefault && $ multiple === true )
85
- ->andReturnUsing ( fn ( $ question , $ options , $ default , $ multiple ) => array_values ( array_filter ( $ options , fn ( $ index ) => in_array ( $ index , $ selection ), ARRAY_FILTER_USE_KEY )) )
89
+ ->with ( ' Test ' , $ expectedOptions , $ expectedDefault , null , true )
90
+ ->andReturn ( $ return )
86
91
);
87
92
88
93
$ this ->assertSame ($ expectedReturn , $ command ->answer );
@@ -91,18 +96,18 @@ public function handle()
91
96
public static function multiselectDataProvider ()
92
97
{
93
98
return [
94
- 'list with no default ' => [fn () => multiselect ('foo ' , ['a ' , 'b ' , 'c ' ]), ' 0 ' , [ 2 , 3 ], [ ' b ' , ' c ' ]],
95
- 'numeric keys with no default ' => [fn () => multiselect ('foo ' , [1 => 'a ' , 2 => 'b ' , 3 => 'c ' ]), ' 0 ' , [ 2 , 3 ], [ 2 , 3 ]],
96
- 'assoc with no default ' => [fn () => multiselect ('foo ' , ['a ' => 'A ' , 'b ' => 'B ' , 'c ' => 'C ' ]), ' 0 ' , [ 2 , 3 ], [ 'b ' , 'c ' ]],
97
- 'list with default ' => [fn () => multiselect ('foo ' , ['a ' , 'b ' , 'c ' ], ['b ' , 'c ' ]), ' 2,3 ' , [ 2 , 3 ], ['b ' , 'c ' ]],
98
- 'numeric keys with default ' => [fn () => multiselect ('foo ' , [1 => 'a ' , 2 => 'b ' , 3 => 'c ' ], [2 , 3 ]), ' 2,3 ' , [ 2 , 3 ], [2 , 3 ]],
99
- 'assoc with default ' => [fn () => multiselect ('foo ' , ['a ' => 'A ' , 'b ' => 'B ' , 'c ' => 'C ' ], ['b ' , 'c ' ]), ' 2,3 ' , [ 2 , 3 ], ['b ' , 'c ' ]],
100
- 'required list with no default ' => [fn () => multiselect ('foo ' , ['a ' , 'b ' , 'c ' ], required: true ), null , [1 , 2 ], ['b ' , 'c ' ]],
101
- 'required numeric keys with no default ' => [fn () => multiselect ('foo ' , [1 => 'a ' , 2 => 'b ' , 3 => 'c ' ], required: true ), null , [1 , 2 ], [2 , 3 ]],
102
- 'required assoc with no default ' => [fn () => multiselect ('foo ' , ['a ' => 'A ' , 'b ' => 'B ' , 'c ' => 'C ' ], required: true ), null , [1 , 2 ], ['b ' , 'c ' ]],
103
- 'required list with default ' => [fn () => multiselect ('foo ' , ['a ' , 'b ' , 'c ' ], ['b ' , 'c ' ], required: true ), ' 1,2 ' , [ 1 , 2 ], ['b ' , 'c ' ]],
104
- 'required numeric keys with default ' => [fn () => multiselect ('foo ' , [1 => 'a ' , 2 => 'b ' , 3 => 'c ' ], [2 , 3 ], required: true ), ' 1,2 ' , [ 1 , 2 ], [2 , 3 ]],
105
- 'required assoc with default ' => [fn () => multiselect ('foo ' , ['a ' => 'A ' , 'b ' => 'B ' , 'c ' => 'C ' ], ['b ' , 'c ' ], required: true ), ' 1,2 ' , [ 1 , 2 ], ['b ' , 'c ' ]],
99
+ 'list with no default ' => [fn () => multiselect ('Test ' , ['a ' , 'b ' , 'c ' ]), [ ' None ' , ' a ' , ' b ' , ' c ' ], ' None ' , [ ' None ' ], [ ]],
100
+ 'numeric keys with no default ' => [fn () => multiselect ('Test ' , [1 => 'a ' , 2 => 'b ' , 3 => 'c ' ]), [ '' => ' None ' , 1 => ' a ' , 2 => ' b ' , 3 => ' c ' ], ' None ' , [ '' ], [ ]],
101
+ 'assoc with no default ' => [fn () => multiselect ('Test ' , ['a ' => 'A ' , 'b ' => 'B ' , 'c ' => 'C ' ]), [ '' => ' None ' , ' a ' => ' A ' , 'b ' => ' B ' , 'c ' => ' C ' ], ' None ' , [ '' ], [ ]],
102
+ 'list with default ' => [fn () => multiselect ('Test ' , ['a ' , 'b ' , 'c ' ], ['b ' , 'c ' ]), [ ' None ' , ' a ' , ' b ' , ' c ' ], ' b,c ' , [ ' b ' , ' c ' ], ['b ' , 'c ' ]],
103
+ 'numeric keys with default ' => [fn () => multiselect ('Test ' , [1 => 'a ' , 2 => 'b ' , 3 => 'c ' ], [2 , 3 ]), [ '' => ' None ' , 1 => ' a ' , 2 => ' b ' , 3 => ' c ' ], ' 2,3 ' , [ ' 2 ' , ' 3 ' ], [2 , 3 ]],
104
+ 'assoc with default ' => [fn () => multiselect ('Test ' , ['a ' => 'A ' , 'b ' => 'B ' , 'c ' => 'C ' ], ['b ' , 'c ' ]), [ '' => ' None ' , ' a ' => ' A ' , ' b ' => ' B ' , ' c ' => ' C ' ], ' b,c ' , [ ' b ' , ' c ' ], ['b ' , 'c ' ]],
105
+ 'required list with no default ' => [fn () => multiselect ('Test ' , ['a ' , 'b ' , 'c ' ], required: true ), [ ' a ' , ' b ' , ' c ' ], null , [' b ' , ' c ' ], ['b ' , 'c ' ]],
106
+ 'required numeric keys with no default ' => [fn () => multiselect ('Test ' , [1 => 'a ' , 2 => 'b ' , 3 => 'c ' ], required: true ), [ 1 => ' a ' , 2 => ' b ' , 3 => ' c ' ], null , [' 2 ' , ' 3 ' ], [2 , 3 ]],
107
+ 'required assoc with no default ' => [fn () => multiselect ('Test ' , ['a ' => 'A ' , 'b ' => 'B ' , 'c ' => 'C ' ], required: true ), [ ' a ' => ' A ' , ' b ' => ' B ' , ' c ' => ' C ' ], null , [' b ' , ' c ' ], ['b ' , 'c ' ]],
108
+ 'required list with default ' => [fn () => multiselect ('Test ' , ['a ' , 'b ' , 'c ' ], ['b ' , 'c ' ], required: true ), [ ' a ' , ' b ' , ' c ' ], ' b,c ' , [ ' b ' , ' c ' ], ['b ' , 'c ' ]],
109
+ 'required numeric keys with default ' => [fn () => multiselect ('Test ' , [1 => 'a ' , 2 => 'b ' , 3 => 'c ' ], [2 , 3 ], required: true ), [ 1 => ' a ' , 2 => ' b ' , 3 => ' c ' ], ' 2,3 ' , [ ' 2 ' , ' 3 ' ], [2 , 3 ]],
110
+ 'required assoc with default ' => [fn () => multiselect ('Test ' , ['a ' => 'A ' , 'b ' => 'B ' , 'c ' => 'C ' ], ['b ' , 'c ' ], required: true ), [ ' a ' => ' A ' , ' b ' => ' B ' , ' c ' => ' C ' ], ' b,c ' , [ ' b ' , ' c ' ], ['b ' , 'c ' ]],
106
111
];
107
112
}
108
113
@@ -112,7 +117,7 @@ protected function runCommand($command, $expectations)
112
117
113
118
$ application ->shouldReceive ('make ' )->withArgs (fn ($ abstract ) => $ abstract === OutputStyle::class)->andReturn ($ outputStyle = m::mock (OutputStyle::class));
114
119
$ application ->shouldReceive ('make ' )->withArgs (fn ($ abstract ) => $ abstract === Factory::class)->andReturn ($ factory = m::mock (Factory::class));
115
- $ application ->shouldReceive ('runningUnitTests ' )->andReturn (true );
120
+ $ application ->shouldReceive ('runningUnitTests ' )->andReturn (false );
116
121
$ application ->shouldReceive ('call ' )->with ([$ command , 'handle ' ])->andReturnUsing (fn ($ callback ) => call_user_func ($ callback ));
117
122
$ outputStyle ->shouldReceive ('newLinesWritten ' )->andReturn (1 );
118
123
0 commit comments