| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Majora\Framework\Form\Extension\Json\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Majora\Framework\Form\Extension\Json\JsonRequestHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Form\FormInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Form\Exception\UnexpectedTypeException; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 11 |  |  | class JsonRequestHandlerTest extends \PHPUnit_Framework_TestCase | 
            
                                                        
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |      * @var JsonRequestHandler | 
            
                                                        
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 16 |  |  |     private $jsonRequestHandler; | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                        
            
                                    
            
            
                | 19 |  |  |      * @var HttpFoundationRequestHandler | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |     private $httpFoundationRequestHandler; | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |      * @var FormInterface | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |     private $form; | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |      * @var Request | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |     private $request; | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |     public function setUp() | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |         $this->httpFoundationRequestHandler = \Phake::mock(HttpFoundationRequestHandler::class); | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |         $this->jsonRequestHandler = new JsonRequestHandler($this->httpFoundationRequestHandler); | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |         $this->form = \Phake::mock(FormInterface::class); | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |         $this->request = \Phake::mock(Request::class); | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |     public function testRequestWithJsonContentShouldSubmitJson() | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |         \Phake::when($this->request) | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |             ->getContentType() | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |             ->thenReturn('json'); | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |         \Phake::when($this->request) | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |             ->getContent() | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |             ->thenReturn('{ "json": "test" }'); | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |         $this->jsonRequestHandler->handleRequest($this->form, $this->request); | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |         \Phake::verify($this->form, \Phake::times(1))->submit('{ "json": "test" }'); | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |     public function testRequestWithoutJsonContentShouldSubmitProxy() | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         \Phake::when($this->request) | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |             ->getContentType() | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |             ->thenReturn('txt'); | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         $this->jsonRequestHandler->handleRequest($this->form, $this->request); | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         \Phake::verify($this->httpFoundationRequestHandler, \Phake::times(1))->handleRequest($this->form, $this->request); | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |     public function testInvalidRequestTypeShouldThrowException() | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |         $this->setExpectedException(UnexpectedTypeException::class); | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |         $this->jsonRequestHandler->handleRequest($this->form, ['request']); | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  |