Skip to content
This repository
Browse code

some changes

  • Loading branch information...
commit 1afad471c0726d8b6badcd82fa80fc07b9784462 1 parent 6c25637
Jordan Stout authored January 10, 2013
4  DependencyInjection/Configuration.php
@@ -130,8 +130,8 @@ private function addViewSection(ArrayNodeDefinition $rootNode)
130 130
                             ->end()
131 131
                         ->end()
132 132
                         ->scalarNode('failed_validation')->defaultValue(Codes::HTTP_BAD_REQUEST)->end()
133  
-                        ->booleanNode('force_no_content_code')->defaultFalse()->end()
134  
-                        ->booleanNode('should_serialize_null')->defaultFalse()->end()
  133
+                        ->scalarNode('empty_content')->defaultValue(Codes::HTTP_NO_CONTENT)->end()
  134
+                        ->booleanNode('serialize_null')->defaultFalse()->end()
135 135
                     ->end()
136 136
                 ->end()
137 137
             ->end();
7  DependencyInjection/FOSRestExtension.php
@@ -79,9 +79,12 @@ public function load(array $configs, ContainerBuilder $container)
79 79
         }
80 80
         $container->setParameter($this->getAlias().'.failed_validation', $config['view']['failed_validation']);
81 81
 
82  
-        $container->setParameter($this->getAlias().'.force_no_content_code', $config['view']['force_no_content_code']);
  82
+        if (!is_numeric($config['view']['empty_content'])) {
  83
+            $config['view']['empty_content'] = constant('\FOS\Rest\Util\Codes::'.$config['view']['empty_content']);
  84
+        }
  85
+        $container->setParameter($this->getAlias().'.empty_content', $config['view']['empty_content']);
83 86
 
84  
-        $container->setParameter($this->getAlias().'.should_serialize_null', $config['view']['should_serialize_null']);
  87
+        $container->setParameter($this->getAlias().'.serialize_null', $config['view']['serialize_null']);
85 88
 
86 89
         if (!empty($config['view']['view_response_listener'])) {
87 90
             $loader->load('view_response_listener.xml');
4  Resources/config/view.xml
@@ -14,8 +14,8 @@
14 14
         <service id="fos_rest.view_handler.default" class="FOS\RestBundle\View\ViewHandler" public="false">
15 15
             <argument>%fos_rest.formats%</argument>
16 16
             <argument>%fos_rest.failed_validation%</argument>
17  
-            <argument>%fos_rest.force_no_content_code%</argument>
18  
-            <argument>%fos_rest.should_serialize_null%</argument>
  17
+            <argument>%fos_rest.empty_content%</argument>
  18
+            <argument>%fos_rest.serialize_null%</argument>
19 19
             <argument>%fos_rest.force_redirects%</argument>
20 20
             <argument>%fos_rest.default_engine%</argument>
21 21
             <call method="setContainer">
20  Tests/DependencyInjection/FOSRestExtensionTest.php
@@ -109,30 +109,30 @@ public function testDisableViewResponseListener()
109 109
         $this->assertFalse($this->container->hasDefinition('fos_rest.view_response_listener'));
110 110
     }
111 111
 
112  
-    public function testViewForceNoContentCodeDefault()
  112
+    public function testForceEmptyContentDefault()
113 113
     {
114 114
         $this->extension->load(array(), $this->container);
115  
-        $this->assertFalse($this->container->getParameter('fos_rest.force_no_content_code'));
  115
+        $this->assertEquals(204, $this->container->getParameter('fos_rest.empty_content'));
116 116
     }
117 117
 
118  
-    public function testViewForceNoContentCodeIsTrue()
  118
+    public function testForceEmptyContentIs200()
119 119
     {
120  
-        $config = array('fos_rest' => array('view' => array('force_no_content_code' => true)));
  120
+        $config = array('fos_rest' => array('view' => array('empty_content' => 200)));
121 121
         $this->extension->load($config, $this->container);
122  
-        $this->assertTrue($this->container->getParameter('fos_rest.force_no_content_code'));
  122
+        $this->assertEquals(200, $this->container->getParameter('fos_rest.empty_content'));
123 123
     }
124 124
 
125  
-    public function testViewShouldSerializeNullDefault()
  125
+    public function testViewSerializeNullDefault()
126 126
     {
127 127
         $this->extension->load(array(), $this->container);
128  
-        $this->assertFalse($this->container->getParameter('fos_rest.should_serialize_null'));
  128
+        $this->assertFalse($this->container->getParameter('fos_rest.serialize_null'));
129 129
     }
130 130
 
131  
-    public function testViewShouldSerializeNullIsTrue()
  131
+    public function testViewSerializeNullIsTrue()
132 132
     {
133  
-        $config = array('fos_rest' => array('view' => array('should_serialize_null' => true)));
  133
+        $config = array('fos_rest' => array('view' => array('serialize_null' => true)));
134 134
         $this->extension->load($config, $this->container);
135  
-        $this->assertTrue($this->container->getParameter('fos_rest.should_serialize_null'));
  135
+        $this->assertTrue($this->container->getParameter('fos_rest.serialize_null'));
136 136
     }
137 137
 
138 138
     /**
40  Tests/View/ViewHandlerTest.php
@@ -67,7 +67,7 @@ public function testRegisterHandleExpectsException()
67 67
     /**
68 68
      * @dataProvider getStatusCodeDataProvider
69 69
      */
70  
-    public function testGetStatusCode($expected, $data, $isBound, $isValid, $isBoundCalled, $isValidCalled, $forceNoContentCode)
  70
+    public function testGetStatusCode($expected, $data, $isBound, $isValid, $isBoundCalled, $isValidCalled, $noContentCode)
71 71
     {
72 72
         $reflectionMethod = new \ReflectionMethod('\FOS\RestBundle\View\ViewHandler', 'getStatusCode');
73 73
         $reflectionMethod->setAccessible(true);
@@ -87,28 +87,28 @@ public function testGetStatusCode($expected, $data, $isBound, $isValid, $isBound
87 87
         }
88 88
         $view =  new View($data ? $data : null);
89 89
 
90  
-        $viewHandler = new ViewHandler(array(), $expected, $forceNoContentCode);
  90
+        $viewHandler = new ViewHandler(array(), $expected, $noContentCode);
91 91
         $this->assertEquals($expected, $reflectionMethod->invoke($viewHandler, $view));
92 92
     }
93 93
 
94 94
     public static function getStatusCodeDataProvider()
95 95
     {
96 96
         return array(
97  
-            'no data' => array(Codes::HTTP_OK, false, false, false, 0, 0, 0),
98  
-            'no data with 204' => array(Codes::HTTP_NO_CONTENT, false, false, false, 0, 0, 1),
99  
-            'form key form not bound' => array(Codes::HTTP_OK, true, false, true, 1, 0, 0),
100  
-            'form key form is bound and invalid' => array(403, true, true, false, 1, 1, 0),
101  
-            'form key form bound and valid' => array(Codes::HTTP_OK, true, true, true, 1, 1, 0),
102  
-            'form key null form bound and valid' => array(Codes::HTTP_OK, true, true, true, 1, 1, 0)
  97
+            'no data' => array(Codes::HTTP_OK, false, false, false, 0, 0, Codes::HTTP_OK),
  98
+            'no data with 204' => array(Codes::HTTP_NO_CONTENT, false, false, false, 0, 0, Codes::HTTP_NO_CONTENT),
  99
+            'form key form not bound' => array(Codes::HTTP_OK, true, false, true, 1, 0, Codes::HTTP_OK),
  100
+            'form key form is bound and invalid' => array(403, true, true, false, 1, 1, Codes::HTTP_OK),
  101
+            'form key form bound and valid' => array(Codes::HTTP_OK, true, true, true, 1, 1, Codes::HTTP_OK),
  102
+            'form key null form bound and valid' => array(Codes::HTTP_OK, true, true, true, 1, 1, Codes::HTTP_OK)
103 103
         );
104 104
     }
105 105
 
106 106
     /**
107 107
      * @dataProvider createResponseWithLocationDataProvider
108 108
      */
109  
-    public function testCreateResponseWithLocation($expected, $format, $forceRedirects, $forceNoContentCode)
  109
+    public function testCreateResponseWithLocation($expected, $format, $forceRedirects, $noContentCode)
110 110
     {
111  
-        $viewHandler = new ViewHandler(array('html' => true, 'json' => false, 'xml' => false), Codes::HTTP_BAD_REQUEST, $forceNoContentCode, false, $forceRedirects);
  111
+        $viewHandler = new ViewHandler(array('html' => true, 'json' => false, 'xml' => false), Codes::HTTP_BAD_REQUEST, $noContentCode, false, $forceRedirects);
112 112
         $view = new View();
113 113
         $view->setLocation('foo');
114 114
         $returnedResponse = $viewHandler->createResponse($view, new Request(), $format);
@@ -120,11 +120,11 @@ public function testCreateResponseWithLocation($expected, $format, $forceRedirec
120 120
     public static function createResponseWithLocationDataProvider()
121 121
     {
122 122
         return array(
123  
-            'empty force redirects' => array(200, 'xml', array('json' => 403), 0),
124  
-            'empty force redirects with 204' => array(204, 'xml', array('json' => 403), 1),
125  
-            'force redirects response is redirect' => array(200, 'json', array(), 0),
126  
-            'force redirects response not redirect' => array(403, 'json', array('json' => 403), 0),
127  
-            'html and redirect' => array(301, 'html', array('html' => 301), 0),
  123
+            'empty force redirects' => array(200, 'xml', array('json' => 403), Codes::HTTP_OK),
  124
+            'empty force redirects with 204' => array(204, 'xml', array('json' => 403), Codes::HTTP_NO_CONTENT),
  125
+            'force redirects response is redirect' => array(200, 'json', array(), Codes::HTTP_OK),
  126
+            'force redirects response not redirect' => array(403, 'json', array('json' => 403), Codes::HTTP_OK),
  127
+            'html and redirect' => array(301, 'html', array('html' => 301), Codes::HTTP_OK),
128 128
         );
129 129
     }
130 130
 
@@ -213,11 +213,11 @@ public static function createResponseWithoutLocationDataProvider()
213 213
         );
214 214
     }
215 215
     /**
216  
-     * @dataProvider createShouldSerializeNullDataProvider
  216
+     * @dataProvider createSerializeNullDataProvider
217 217
      */
218  
-    public function testShouldSerializeNull($expected, $shouldSerializeNull)
  218
+    public function testSerializeNull($expected, $serializeNull)
219 219
     {
220  
-        $viewHandler = new ViewHandler(array('json' => false), 404, 200, $shouldSerializeNull);
  220
+        $viewHandler = new ViewHandler(array('json' => false), 404, 200, $serializeNull);
221 221
         $container = $this->getMock('\Symfony\Component\DependencyInjection\Container', array('get', 'getParameter'));
222 222
 
223 223
         $viewHandler->setContainer($container);
@@ -227,7 +227,7 @@ public function testShouldSerializeNull($expected, $shouldSerializeNull)
227 227
             ->disableOriginalConstructor()
228 228
             ->getMock();
229 229
 
230  
-        if ($shouldSerializeNull) {
  230
+        if ($serializeNull) {
231 231
             $serializer
232 232
                 ->expects($this->once())
233 233
                 ->method('serialize')
@@ -252,7 +252,7 @@ public function testShouldSerializeNull($expected, $shouldSerializeNull)
252 252
         $this->assertEquals($expected, $response->getContent());
253 253
     }
254 254
 
255  
-    public static function createShouldSerializeNullDataProvider()
  255
+    public static function createSerializeNullDataProvider()
256 256
     {
257 257
         return array(
258 258
             'should serialize null'     => array("null", true),
22  View/ViewHandler.php
@@ -48,14 +48,14 @@ class ViewHandler extends ContainerAware implements ViewHandlerInterface
48 48
     protected $failedValidationCode;
49 49
 
50 50
     /**
51  
-     * @param int Forces a 204 HTTP response status code when the view data is empty
  51
+     * @param int HTTP response status code when the view data is null
52 52
      */
53  
-    protected $forceNoContentCode;
  53
+    protected $emptyContentCode;
54 54
 
55 55
     /**
56 56
      * @param int Whether or not to serialize null view data
57 57
      */
58  
-    protected $shouldSerializeNull;
  58
+    protected $serializeNull;
59 59
 
60 60
     /**
61 61
      * @var array if to force a redirect for the given key format, with value being the status code to use
@@ -72,23 +72,23 @@ class ViewHandler extends ContainerAware implements ViewHandlerInterface
72 72
      *
73 73
      * @param array   $formats              the supported formats as keys and if the given formats uses templating is denoted by a true value
74 74
      * @param int     $failedValidationCode The HTTP response status code for a failed validation
75  
-     * @param Boolean $forceNoContentCode   Forces a 204 HTTP response status code when the view data is empty
76  
-     * @param Boolean $shouldSerializeNull  Whether or not to serialize null view data
  75
+     * @param int     $emptyContentCode     HTTP response status code when the view data is null
  76
+     * @param Boolean $serializeNull        Whether or not to serialize null view data
77 77
      * @param array   $forceRedirects       If to force a redirect for the given key format, with value being the status code to use
78 78
      * @param string  $defaultEngine        default engine (twig, php ..)
79 79
      */
80 80
     public function __construct(
81 81
         array $formats = null,
82 82
         $failedValidationCode = Codes::HTTP_BAD_REQUEST,
83  
-        $forceNoContentCode = false,
84  
-        $shouldSerializeNull = false,
  83
+        $emptyContentCode = Codes::HTTP_NO_CONTENT,
  84
+        $serializeNull = false,
85 85
         array $forceRedirects = null,
86 86
         $defaultEngine = 'twig'
87 87
     ) {
88 88
         $this->formats = (array) $formats;
89 89
         $this->failedValidationCode = $failedValidationCode;
90  
-        $this->forceNoContentCode = $forceNoContentCode;
91  
-        $this->shouldSerializeNull = $shouldSerializeNull;
  90
+        $this->emptyContentCode = $emptyContentCode;
  91
+        $this->serializeNull = $serializeNull;
92 92
         $this->forceRedirects = (array) $forceRedirects;
93 93
         $this->defaultEngine = $defaultEngine;
94 94
     }
@@ -154,7 +154,7 @@ private function getStatusCode(View $view)
154 154
             return $this->failedValidationCode;
155 155
         }
156 156
 
157  
-        return null !== $data ? Codes::HTTP_OK : ($this->forceNoContentCode ? Codes::HTTP_NO_CONTENT : Codes::HTTP_OK);
  157
+        return null !== $data ? Codes::HTTP_OK : $this->emptyContentCode;
158 158
     }
159 159
 
160 160
     /**
@@ -351,7 +351,7 @@ public function createResponse(View $view, Request $request, $format)
351 351
         $content = null;
352 352
         if ($this->isFormatTemplating($format)) {
353 353
             $content = $this->renderTemplate($view, $format);
354  
-        } elseif ($this->shouldSerializeNull || null !== $view->getData()) {
  354
+        } elseif ($this->serializeNull || null !== $view->getData()) {
355 355
             $serializer = $this->getSerializer($view);
356 356
             $content = $serializer->serialize($view->getData(), $format);
357 357
         }

0 notes on commit 1afad47

Please sign in to comment.
Something went wrong with that request. Please try again.