2626
2727final class PageRedirectControllerTest extends TestCase
2828{
29+ private const QR_ORDER_ID_SESSION_KEY = 'sylius_mollie_qr_order_id ' ;
30+
2931 private MockObject &RouterInterface $ router ;
3032
3133 private MockObject &OrderRepositoryInterface $ orderRepository ;
@@ -37,97 +39,107 @@ protected function setUp(): void
3739 $ this ->router = $ this ->createMock (RouterInterface::class);
3840 $ this ->orderRepository = $ this ->createMock (OrderRepositoryInterface::class);
3941 $ this ->session = $ this ->createMock (SessionInterface::class);
42+
43+ $ this ->router ->method ('generate ' )->willReturnCallback (
44+ static fn (string $ name , array $ parameters = []): string => match ($ name ) {
45+ 'sylius_shop_order_thank_you ' => '/en_US/order/thank-you ' ,
46+ 'sylius_shop_order_show ' => '/en_US/order/ ' . ($ parameters ['tokenValue ' ] ?? '' ),
47+ default => '/ ' ,
48+ },
49+ );
4050 }
4151
42- public function testItThrowsNotFoundWhenOrderTokenIsMissing (): void
52+ public function testItThrowsNotFoundWhenOrderIdIsMissing (): void
4353 {
4454 $ this ->expectException (NotFoundHttpException::class);
4555
46- $ this ->orderRepository ->expects (self ::never ())->method ('findOneByTokenValue ' );
56+ $ this ->orderRepository ->expects (self ::never ())->method ('findOneBy ' );
4757
4858 $ controller = $ this ->createController ();
4959 $ controller ->thankYouAction (new Request (), $ this ->session );
5060 }
5161
52- public function testItThrowsNotFoundWhenOrderTokenIsEmpty (): void
62+ public function testItThrowsNotFoundWhenOrderIdIsEmpty (): void
5363 {
5464 $ this ->expectException (NotFoundHttpException::class);
5565
56- $ this ->orderRepository ->expects (self ::never ())->method ('findOneByTokenValue ' );
66+ $ this ->orderRepository ->expects (self ::never ())->method ('findOneBy ' );
5767
5868 $ controller = $ this ->createController ();
59- $ controller ->thankYouAction (new Request (['orderToken ' => '' ]), $ this ->session );
69+ $ controller ->thankYouAction (new Request (['orderId ' => '' ]), $ this ->session );
6070 }
6171
62- public function testItThrowsNotFoundWhenOrderTokenIsUnknown (): void
72+ public function testItThrowsNotFoundWhenOrderIdDoesNotMatchSession (): void
6373 {
6474 $ this ->expectException (NotFoundHttpException::class);
6575
66- $ this ->orderRepository
67- ->expects (self ::once ())
68- ->method ('findOneByTokenValue ' )
69- ->with ('unknown-token ' )
70- ->willReturn (null );
76+ $ this ->session ->method ('get ' )->with (self ::QR_ORDER_ID_SESSION_KEY )->willReturn (99 );
77+ $ this ->orderRepository ->expects (self ::never ())->method ('findOneBy ' );
7178
7279 $ controller = $ this ->createController ();
73- $ controller ->thankYouAction (new Request (['orderToken ' => ' unknown-token ' ]), $ this ->session );
80+ $ controller ->thankYouAction (new Request (['orderId ' => 42 ]), $ this ->session );
7481 }
7582
76- public function testItRedirectsToThankYouPageWhenPaymentIsCompleted (): void
83+ public function testItThrowsNotFoundWhenOrderDoesNotExist (): void
7784 {
78- $ order = $ this ->createOrderMock (42 , 'abc123 ' , 'completed ' );
85+ $ this ->expectException (NotFoundHttpException::class);
86+
87+ $ this ->session ->method ('get ' )->with (self ::QR_ORDER_ID_SESSION_KEY )->willReturn (42 );
88+ $ this ->orderRepository ->method ('findOneBy ' )->with (['id ' => 42 ])->willReturn (null );
89+
90+ $ controller = $ this ->createController ();
91+ $ controller ->thankYouAction (new Request (['orderId ' => 42 ]), $ this ->session );
92+ }
7993
80- $ this ->orderRepository ->method ('findOneByTokenValue ' )->with ('abc123 ' )->willReturn ($ order );
94+ public function testItRedirectsToThankYouPageWhenPaymentIsCompleted (): void
95+ {
96+ $ this ->session ->method ('get ' )->with (self ::QR_ORDER_ID_SESSION_KEY )->willReturn (42 );
8197 $ this ->session ->expects (self ::once ())->method ('set ' )->with ('sylius_order_id ' , 42 );
82- $ this ->router ->method ('generate ' )->with ('sylius_shop_order_thank_you ' )->willReturn ('/en_US/order/thank-you ' );
98+
99+ $ order = $ this ->createOrderMock (42 , 'abc123 ' , 'completed ' );
100+ $ this ->orderRepository ->method ('findOneBy ' )->with (['id ' => 42 ])->willReturn ($ order );
83101
84102 $ controller = $ this ->createController ();
85- $ response = $ controller ->thankYouAction (new Request (['orderToken ' => ' abc123 ' ]), $ this ->session );
103+ $ response = $ controller ->thankYouAction (new Request (['orderId ' => 42 ]), $ this ->session );
86104
87105 self ::assertSame (302 , $ response ->getStatusCode ());
88106 self ::assertSame ('/en_US/order/thank-you ' , $ response ->getTargetUrl ());
89107 }
90108
91109 public function testItRedirectsToOrderShowWhenPaymentIsNotCompleted (): void
92110 {
93- $ order = $ this ->createOrderMock (42 , 'abc123 ' , 'new ' );
94-
95- $ this ->orderRepository ->method ('findOneByTokenValue ' )->with ('abc123 ' )->willReturn ($ order );
111+ $ this ->session ->method ('get ' )->with (self ::QR_ORDER_ID_SESSION_KEY )->willReturn (42 );
96112 $ this ->session ->expects (self ::once ())->method ('set ' )->with ('sylius_order_id ' , 42 );
97- $ this ->router ->method ('generate ' )->willReturnMap ([
98- ['sylius_shop_order_thank_you ' , [], 1 , '/en_US/order/thank-you ' ],
99- ['sylius_shop_order_show ' , ['tokenValue ' => 'abc123 ' ], 1 , '/en_US/order/abc123 ' ],
100- ]);
113+
114+ $ order = $ this ->createOrderMock (42 , 'abc123 ' , 'new ' );
115+ $ this ->orderRepository ->method ('findOneBy ' )->with (['id ' => 42 ])->willReturn ($ order );
101116
102117 $ controller = $ this ->createController ();
103- $ response = $ controller ->thankYouAction (new Request (['orderToken ' => ' abc123 ' ]), $ this ->session );
118+ $ response = $ controller ->thankYouAction (new Request (['orderId ' => 42 ]), $ this ->session );
104119
105120 self ::assertSame (302 , $ response ->getStatusCode ());
106121 self ::assertSame ('/en_US/order/abc123 ' , $ response ->getTargetUrl ());
107122 }
108123
109- public function testItSetsSessionFromOrderIdNotFromRequestParameter (): void
124+ public function testItThrowsNotFoundWhenOrderHasNoTokenValue (): void
110125 {
111- $ order = $ this ->createOrderMock ( 99 , ' abc123 ' , ' completed ' );
126+ $ this ->expectException (NotFoundHttpException::class );
112127
113- $ this ->orderRepository ->method ('findOneByTokenValue ' )->willReturn ($ order );
114- $ this ->router ->method ('generate ' )->willReturn ('/en_US/order/thank-you ' );
128+ $ this ->session ->method ('get ' )->with (self ::QR_ORDER_ID_SESSION_KEY )->willReturn (42 );
115129
116- $ this ->session
117- ->expects (self ::once ())
118- ->method ('set ' )
119- ->with ('sylius_order_id ' , 99 );
130+ $ order = $ this ->createOrderMock (42 , null , 'new ' );
131+ $ this ->orderRepository ->method ('findOneBy ' )->with (['id ' => 42 ])->willReturn ($ order );
120132
121133 $ controller = $ this ->createController ();
122- $ controller ->thankYouAction (new Request (['orderToken ' => ' abc123 ' ]), $ this ->session );
134+ $ controller ->thankYouAction (new Request (['orderId ' => 42 ]), $ this ->session );
123135 }
124136
125137 private function createController (): PageRedirectController
126138 {
127139 return new PageRedirectController ($ this ->router , $ this ->orderRepository );
128140 }
129141
130- private function createOrderMock (int $ id , string $ tokenValue , string $ paymentState ): MockObject &OrderInterface
142+ private function createOrderMock (int $ id , ? string $ tokenValue , string $ paymentState ): MockObject &OrderInterface
131143 {
132144 $ payment = $ this ->createMock (PaymentInterface::class);
133145 $ payment ->method ('getState ' )->willReturn ($ paymentState );
0 commit comments