22
33namespace Lorisleiva \Actions \Tests ;
44
5- use ArgumentCountError ;
6- use Closure ;
75use Illuminate \Support \Facades \Pipeline ;
86use Lorisleiva \Actions \Concerns \AsAction ;
9- use Lorisleiva \Actions \Concerns \AsPipeline ;
107
11- class AsPipelinePassable
12- {
13- public function __construct (public int $ count = 0 )
14- {
15- //
16- }
17-
18- public function increment ()
19- {
20- $ this ->count ++;
21- }
22- }
23-
24- class AsPipelineExplicitTest
25- {
26- use AsPipeline;
27-
28- public function handle (AsPipelinePassable $ passable ): void
29- {
30- $ passable ->increment ();
31- }
32-
33- public function asPipeline (AsPipelinePassable $ passable ): AsPipelinePassable
34- {
35- $ this ->handle ($ passable );
36-
37- return $ passable ;
38- }
39- }
40-
41- class AsPipelineImplicitTest
8+ class AsPipelineTest
429{
4310 use AsAction;
4411
@@ -55,156 +22,31 @@ public function asPipeline(AsPipelinePassable $passable): AsPipelinePassable
5522 }
5623}
5724
58- class AsPipelineMultipleParamTest
59- {
60- use AsAction;
61-
62- public function handle (AsPipelinePassable $ passable ): void
63- {
64- $ passable ->increment ();
65- }
66-
67- public function asPipeline (AsPipelinePassable $ passable , int $ foo ): AsPipelinePassable
68- {
69- $ this ->handle ($ passable );
70-
71- return $ passable ;
72- }
73- }
74-
75- class AsPipelineSingleParamHandleOnlyTest
76- {
77- use AsAction;
78-
79- public function handle (AsPipelinePassable $ passable ): void
80- {
81- $ passable ->increment ();
82- }
83- }
84-
85- class AsPipelineMultipleParamHandleOnlyTest
86- {
87- use AsAction;
88-
89- public function handle (AsPipelinePassable $ passable , int $ foo ): void
90- {
91- $ passable ->increment ();
92- }
93- }
94-
95- class AsPipelineWithoutHandleOrAsPipeline
96- {
97- use AsAction;
98- }
99-
100- function getAnonymous () {
101- return function (AsPipelinePassable $ p , $ next ) {
102- $ p ->increment ();
103-
104- return $ next ($ p );
105- };
106- }
107-
108- function getPassable () {
109- return new AsPipelinePassable ;
110- }
111-
112- it ('can run as a pipe in a pipeline, with explicit trait ' , function () {
113- $ anonymous = getAnonymous ();
114- $ passable = Pipeline::send (getPassable ())
115- ->through ([
116- AsPipelineExplicitTest::class,
117- $ anonymous ,
118- AsPipelineExplicitTest::class,
119- $ anonymous ,
120- ])
121- ->thenReturn ();
122-
123- expect (is_object ($ passable ))->toBe (true );
124- expect ($ passable ->count )->toBe (4 );
125- });
126-
127- it ('can run as a pipe in a pipeline, with implicit trait ' , function () {
128- $ anonymous = getAnonymous ();
129- $ passable = Pipeline::send (getPassable ())
130- ->through ([
131- AsPipelineImplicitTest::class,
132- $ anonymous ,
133- AsPipelineImplicitTest::class,
134- $ anonymous ,
135- ])
136- ->thenReturn ();
137-
138- expect (is_object ($ passable ))->toBe (true );
139- expect ($ passable ->count )->toBe (4 );
140- });
141-
142- it ('can run as a pipe in a pipeline, without an explicit asPipeline method ' , function () {
143- $ anonymous = getAnonymous ();
144- $ passable = Pipeline::send (getPassable ())
25+ it ('can run as a pipe in a pipeline, with an explicit asPipeline method ' , function () {
26+ $ passable = Pipeline::send (new AsPipelinePassable )
14527 ->through ([
146- AsPipelineSingleParamHandleOnlyTest ::class,
147- $ anonymous ,
148- AsPipelineSingleParamHandleOnlyTest ::class,
149- $ anonymous ,
28+ AsPipelineTest ::class,
29+ AsPipelineTest::class ,
30+ AsPipelineTest ::class,
31+ AsPipelineTest::class ,
15032 ])
15133 ->thenReturn ();
15234
153- expect (is_object ($ passable ))->toBe (true );
35+ expect (is_a ($ passable, AsPipelinePassable::class ))->toBe (true );
15436 expect ($ passable ->count )->toBe (4 );
15537});
15638
157- it ('can run as a noop/passthrough pipe in a pipeline, without a handle or asPipeline method ' , function () {
158- $ anonymous = getAnonymous ();
159- $ passable = Pipeline::send (getPassable ())
160- ->through ([
161- AsPipelineWithoutHandleOrAsPipeline::class,
162- $ anonymous ,
163- AsPipelineWithoutHandleOrAsPipeline::class,
164- $ anonymous ,
165- ])
166- ->thenReturn ();
167-
168- expect (is_object ($ passable ))->toBe (true );
169- expect ($ passable ->count )->toBe (2 );
170- });
171-
17239it ('can run with an arbitrary via method configured on Pipeline ' , function () {
173- $ anonymous = getAnonymous ();
174- $ passable = Pipeline::send (getPassable ())
175- ->via ('foobar ' )
40+ $ passable = Pipeline::send (new AsPipelinePassable )
41+ ->via ('arbitraryMethodThatDoesNotExistOnTheAction ' )
17642 ->through ([
177- AsPipelineImplicitTest ::class,
178- $ anonymous ,
179- AsPipelineImplicitTest ::class,
180- $ anonymous ,
43+ AsPipelineTest ::class,
44+ AsPipelineTest::class ,
45+ AsPipelineTest ::class,
46+ AsPipelineTest::class ,
18147 ])
18248 ->thenReturn ();
18349
184- expect (is_object ($ passable ))->toBe (true );
50+ expect (is_a ($ passable, AsPipelinePassable::class ))->toBe (true );
18551 expect ($ passable ->count )->toBe (4 );
18652});
187-
188- it ('cannot run as a pipe in a pipeline, with an explicit asPipeline method expecting multiple non-optional params ' , function () {
189- $ anonymous = getAnonymous ();
190- $ passable = Pipeline::send (getPassable ())
191- ->through ([
192- AsPipelineMultipleParamTest::class,
193- $ anonymous ,
194- AsPipelineMultipleParamTest::class,
195- $ anonymous ,
196- ])
197- ->thenReturn ();
198- })->throws (ArgumentCountError::class, 'Too few arguments to function Lorisleiva\Actions\Tests\AsPipelineMultipleParamTest::asPipeline(), 1 passed and exactly 2 expected ' );
199-
200- it ('cannot run as a pipe in a pipeline, without an explicit asPipeline method and multiple non-optional handle params ' , function () {
201- $ anonymous = getAnonymous ();
202- $ passable = Pipeline::send (getPassable ())
203- ->through ([
204- AsPipelineMultipleParamHandleOnlyTest::class,
205- $ anonymous ,
206- AsPipelineMultipleParamHandleOnlyTest::class,
207- $ anonymous ,
208- ])
209- ->thenReturn ();
210- })->throws (ArgumentCountError::class, 'Too few arguments to function Lorisleiva\Actions\Tests\AsPipelineMultipleParamHandleOnlyTest::handle(), 1 passed and exactly 2 expected ' );
0 commit comments