| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the Sonata Project package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * (c) Thomas Rabaix <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Sonata\NotificationBundle\Tests\Controller\Api; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use FOS\RestBundle\Request\ParamFetcherInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Sonata\NotificationBundle\Controller\Api\MessageController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Sonata\NotificationBundle\Model\MessageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Sonata\NotificationBundle\Model\MessageManagerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Sonata\PageBundle\Model\SiteManagerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\Form\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Symfony\Component\Form\FormFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Symfony\Component\Form\FormInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @author Hugo Briand <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | class MessageControllerTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function testGetMessagesAction(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $messageManager = $this->createMock(MessageManagerInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $messageManager->expects($this->once())->method('getPager')->willReturn([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $paramFetcher = $this->createMock(ParamFetcherInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $paramFetcher->expects($this->exactly(3))->method('get'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $paramFetcher->expects($this->once())->method('all')->willReturn([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->assertSame([], $this->createMessageController(null, $messageManager)->getMessagesAction($paramFetcher)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function testPostMessageAction(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $message = $this->createMock(MessageInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $messageManager = $this->createMock(MessageManagerInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $messageManager->expects($this->once())->method('save')->willReturn($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $form = $this->createMock(Form::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $form->expects($this->once())->method('handleRequest'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $form->expects($this->once())->method('isValid')->willReturn(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $form->expects($this->once())->method('getData')->willReturn($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $formFactory = $this->createMock(FormFactoryInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $formFactory->expects($this->once())->method('createNamed')->willReturn($form); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $message = $this->createMessageController(null, $messageManager, $formFactory)->postMessageAction(new Request()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->assertInstanceOf(MessageInterface::class, $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function testPostMessageInvalidAction(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $message = $this->createMock(MessageInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $messageManager = $this->createMock(MessageManagerInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $messageManager->expects($this->never())->method('save')->willReturn($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $form = $this->createMock(Form::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $form->expects($this->once())->method('handleRequest'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $form->expects($this->once())->method('isValid')->willReturn(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $formFactory = $this->createMock(FormFactoryInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $formFactory->expects($this->once())->method('createNamed')->willReturn($form); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $form = $this->createMessageController(null, $messageManager, $formFactory)->postMessageAction(new Request()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $this->assertInstanceOf(FormInterface::class, $form); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param $messageManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param $formFactory | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |     public function createMessageController($message = null, $messageManager = null, $formFactory = null): MessageController | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |         if (null === $messageManager) { | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |             $messageManager = $this->createMock(SiteManagerInterface::class); | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         if (null !== $message) { | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |             $messageManager->expects($this->once())->method('findOneBy')->willReturn($message); | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |         if (null === $formFactory) { | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |             $formFactory = $this->createMock(FormFactoryInterface::class); | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |         return new MessageController($messageManager, $formFactory); | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 102 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |  |