File tree Expand file tree Collapse file tree 3 files changed +109
-0
lines changed Expand file tree Collapse file tree 3 files changed +109
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Yokai \Batch \Bridge \Symfony \Framework ;
6
+
7
+ use Symfony \Component \DependencyInjection \ContainerInterface ;
8
+ use Symfony \Component \DependencyInjection \Exception \InvalidArgumentException ;
9
+ use Yokai \Batch \Exception \CannotAccessParameterException ;
10
+ use Yokai \Batch \Job \Parameters \JobParameterAccessorInterface ;
11
+ use Yokai \Batch \JobExecution ;
12
+
13
+ /**
14
+ * This job parameter accessor implementation returns container parameter value.
15
+ */
16
+ final class ContainerParameterAccessor implements JobParameterAccessorInterface
17
+ {
18
+ private ContainerInterface $ container ;
19
+ private string $ name ;
20
+
21
+ public function __construct (ContainerInterface $ container , string $ name )
22
+ {
23
+ $ this ->container = $ container ;
24
+ $ this ->name = $ name ;
25
+ }
26
+
27
+ /**
28
+ * @inheritdoc
29
+ */
30
+ public function get (JobExecution $ execution )
31
+ {
32
+ try {
33
+ return $ this ->container ->getParameter ($ this ->name );
34
+ } catch (InvalidArgumentException $ exception ) {
35
+ throw new CannotAccessParameterException (
36
+ \sprintf ('Cannot access "%s" parameter from container parameters ' , $ this ->name ),
37
+ $ exception
38
+ );
39
+ }
40
+ }
41
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Yokai \Batch \Tests \Bridge \Symfony \Framework ;
6
+
7
+ use Symfony \Component \DependencyInjection \Container ;
8
+ use Yokai \Batch \Bridge \Symfony \Framework \ContainerParameterAccessor ;
9
+ use PHPUnit \Framework \TestCase ;
10
+ use Yokai \Batch \Exception \CannotAccessParameterException ;
11
+ use Yokai \Batch \JobExecution ;
12
+
13
+ class ContainerParameterAccessorTest extends TestCase
14
+ {
15
+ public function test (): void
16
+ {
17
+ $ container = new Container ();
18
+ $ container ->setParameter ('some.parameter ' , 'foo ' );
19
+ $ accessor = new ContainerParameterAccessor ($ container , 'some.parameter ' );
20
+
21
+ self ::assertSame ('foo ' , $ accessor ->get (JobExecution::createRoot ('123 ' , 'testing ' )));
22
+ }
23
+
24
+ public function testParameterNotFound (): void
25
+ {
26
+ $ this ->expectException (CannotAccessParameterException::class);
27
+ $ container = new Container ();
28
+ $ container ->setParameter ('some.parameter ' , 'foo ' );
29
+ $ accessor = new ContainerParameterAccessor ($ container , 'undefined.parameter ' );
30
+ $ accessor ->get (JobExecution::createRoot ('123 ' , 'testing ' ));
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Yokai \Batch \Tests \Bridge \Symfony \Framework \Job \Parameters ;
6
+
7
+ use Yokai \Batch \Exception \CannotAccessParameterException ;
8
+ use Yokai \Batch \Job \Parameters \JobExecutionSummaryAccessor ;
9
+ use PHPUnit \Framework \TestCase ;
10
+ use Yokai \Batch \JobExecution ;
11
+
12
+ class JobExecutionSummaryAccessorTest extends TestCase
13
+ {
14
+ public function test (): void
15
+ {
16
+ $ accessor = new JobExecutionSummaryAccessor ('report ' );
17
+
18
+ $ execution = JobExecution::createRoot ('123 ' , 'testing ' );
19
+ $ execution ->getSummary ()->set ('report ' , 42 );
20
+ self ::assertSame (42 , $ accessor ->get ($ execution ));
21
+
22
+ $ execution = JobExecution::createRoot ('123 ' , 'testing ' );
23
+ $ execution ->getSummary ()->set ('anything.else ' , 0 );
24
+ $ execution ->getSummary ()->set ('report ' , 1042 );
25
+ self ::assertSame (1042 , $ accessor ->get ($ execution ));
26
+ }
27
+
28
+ public function testNotFound (): void
29
+ {
30
+ $ this ->expectException (CannotAccessParameterException::class);
31
+
32
+ $ accessor = new JobExecutionSummaryAccessor ('an.undefined.summary.var ' );
33
+
34
+ $ accessor ->get (JobExecution::createRoot ('123 ' , 'testing ' ));
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments