Skip to content

Commit 0b2948f

Browse files
committed
feature #17111 [HttpKernel] added a setter for the headers property in the HttpException (smatyas)
This PR was submitted for the 2.8 branch but it was merged into the 3.1-dev branch instead (closes #17111). Discussion ---------- [HttpKernel] added a setter for the headers property in the HttpException | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | symfony/symfony-docs#6076 Added this setter, because it's now impossible to use those handy, meaningful, already implemented exceptions (like NotFound...) if one wants to set some custom headers. This PR solves this problem. It's a backward compatible solution in this way, but maybe it would be better if the `HttpExceptionInterface` would contain the `setHeaders` function as well. Of course this would be a BC break, that's why it is missing now. TODO: - [x] submit changes to the documentation Commits ------- 6a1080f [HttpKernel] added a setter for the headers property in the HttpException
2 parents e9e4869 + 6a1080f commit 0b2948f

17 files changed

+623
-0
lines changed

src/Symfony/Component/HttpKernel/Exception/HttpException.php

+10
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,14 @@ public function getHeaders()
3838
{
3939
return $this->headers;
4040
}
41+
42+
/**
43+
* Set response headers.
44+
*
45+
* @param array $headers Response headers.
46+
*/
47+
public function setHeaders(array $headers)
48+
{
49+
$this->headers = $headers;
50+
}
4151
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Exception;
4+
5+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
6+
7+
/**
8+
* Test the AccessDeniedHttpException class.
9+
*/
10+
class AccessDeniedHttpExceptionTest extends HttpExceptionTest
11+
{
12+
/**
13+
* Test that the default headers is an empty array.
14+
*/
15+
public function testHeadersDefault()
16+
{
17+
$exception = new AccessDeniedHttpException();
18+
$this->assertSame(array(), $exception->getHeaders());
19+
}
20+
21+
/**
22+
* Test that setting the headers using the setter function
23+
* is working as expected.
24+
*
25+
* @param array $headers The headers to set.
26+
*
27+
* @dataProvider headerDataProvider
28+
*/
29+
public function testHeadersSetter($headers)
30+
{
31+
$exception = new AccessDeniedHttpException();
32+
$exception->setHeaders($headers);
33+
$this->assertSame($headers, $exception->getHeaders());
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Exception;
4+
5+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
6+
7+
/**
8+
* Test the BadRequestHttpException class.
9+
*/
10+
class BadRequestHttpExceptionTest extends HttpExceptionTest
11+
{
12+
/**
13+
* Test that the default headers is an empty array.
14+
*/
15+
public function testHeadersDefault()
16+
{
17+
$exception = new BadRequestHttpException();
18+
$this->assertSame(array(), $exception->getHeaders());
19+
}
20+
21+
/**
22+
* Test that setting the headers using the setter function
23+
* is working as expected.
24+
*
25+
* @param array $headers The headers to set.
26+
*
27+
* @dataProvider headerDataProvider
28+
*/
29+
public function testHeadersSetter($headers)
30+
{
31+
$exception = new BadRequestHttpException();
32+
$exception->setHeaders($headers);
33+
$this->assertSame($headers, $exception->getHeaders());
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Exception;
4+
5+
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
6+
7+
/**
8+
* Test the ConflictHttpException class.
9+
*/
10+
class ConflictHttpExceptionTest extends HttpExceptionTest
11+
{
12+
/**
13+
* Test that the default headers is an empty array.
14+
*/
15+
public function testHeadersDefault()
16+
{
17+
$exception = new ConflictHttpException();
18+
$this->assertSame(array(), $exception->getHeaders());
19+
}
20+
21+
/**
22+
* Test that setting the headers using the setter function
23+
* is working as expected.
24+
*
25+
* @param array $headers The headers to set.
26+
*
27+
* @dataProvider headerDataProvider
28+
*/
29+
public function testHeadersSetter($headers)
30+
{
31+
$exception = new ConflictHttpException();
32+
$exception->setHeaders($headers);
33+
$this->assertSame($headers, $exception->getHeaders());
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Exception;
4+
5+
use Symfony\Component\HttpKernel\Exception\GoneHttpException;
6+
7+
/**
8+
* Test the GoneHttpException class.
9+
*/
10+
class GoneHttpExceptionTest extends HttpExceptionTest
11+
{
12+
/**
13+
* Test that the default headers is an empty array.
14+
*/
15+
public function testHeadersDefault()
16+
{
17+
$exception = new GoneHttpException();
18+
$this->assertSame(array(), $exception->getHeaders());
19+
}
20+
21+
/**
22+
* Test that setting the headers using the setter function
23+
* is working as expected.
24+
*
25+
* @param array $headers The headers to set.
26+
*
27+
* @dataProvider headerDataProvider
28+
*/
29+
public function testHeadersSetter($headers)
30+
{
31+
$exception = new GoneHttpException();
32+
$exception->setHeaders($headers);
33+
$this->assertSame($headers, $exception->getHeaders());
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Exception;
4+
5+
use Symfony\Component\HttpKernel\Exception\HttpException;
6+
7+
/**
8+
* Test the HttpException class.
9+
*/
10+
class HttpExceptionTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* Provides header data for the tests.
14+
*
15+
* @return array
16+
*/
17+
public function headerDataProvider()
18+
{
19+
return array(
20+
array(array('X-Test' => 'Test')),
21+
array(array('X-Test' => 1)),
22+
array(
23+
array(
24+
array('X-Test' => 'Test'),
25+
array('X-Test-2' => 'Test-2'),
26+
),
27+
),
28+
);
29+
}
30+
31+
/**
32+
* Test that the default headers is an empty array.
33+
*/
34+
public function testHeadersDefault()
35+
{
36+
$exception = new HttpException(200);
37+
$this->assertSame(array(), $exception->getHeaders());
38+
}
39+
40+
/**
41+
* Test that setting the headers using the constructor parameter
42+
* is working as expected.
43+
*
44+
* @param array $headers The headers to set.
45+
*
46+
* @dataProvider headerDataProvider
47+
*/
48+
public function testHeadersConstructor($headers)
49+
{
50+
$exception = new HttpException(200, null, null, $headers);
51+
$this->assertSame($headers, $exception->getHeaders());
52+
}
53+
54+
/**
55+
* Test that setting the headers using the setter function
56+
* is working as expected.
57+
*
58+
* @param array $headers The headers to set.
59+
*
60+
* @dataProvider headerDataProvider
61+
*/
62+
public function testHeadersSetter($headers)
63+
{
64+
$exception = new HttpException(200);
65+
$exception->setHeaders($headers);
66+
$this->assertSame($headers, $exception->getHeaders());
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Exception;
4+
5+
use Symfony\Component\HttpKernel\Exception\LengthRequiredHttpException;
6+
7+
/**
8+
* Test the LengthRequiredHttpException class.
9+
*/
10+
class LengthRequiredHttpExceptionTest extends HttpExceptionTest
11+
{
12+
/**
13+
* Test that the default headers is an empty array.
14+
*/
15+
public function testHeadersDefault()
16+
{
17+
$exception = new LengthRequiredHttpException();
18+
$this->assertSame(array(), $exception->getHeaders());
19+
}
20+
21+
/**
22+
* Test that setting the headers using the setter function
23+
* is working as expected.
24+
*
25+
* @param array $headers The headers to set.
26+
*
27+
* @dataProvider headerDataProvider
28+
*/
29+
public function testHeadersSetter($headers)
30+
{
31+
$exception = new LengthRequiredHttpException();
32+
$exception->setHeaders($headers);
33+
$this->assertSame($headers, $exception->getHeaders());
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Exception;
4+
5+
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
6+
7+
/**
8+
* Test the MethodNotAllowedHttpException class.
9+
*/
10+
class MethodNotAllowedHttpExceptionTest extends HttpExceptionTest
11+
{
12+
/**
13+
* Test that the default headers is set as expected.
14+
*/
15+
public function testHeadersDefault()
16+
{
17+
$exception = new MethodNotAllowedHttpException(array('GET', 'PUT'));
18+
$this->assertSame(array('Allow' => 'GET, PUT'), $exception->getHeaders());
19+
}
20+
21+
/**
22+
* Test that setting the headers using the setter function
23+
* is working as expected.
24+
*
25+
* @param array $headers The headers to set.
26+
*
27+
* @dataProvider headerDataProvider
28+
*/
29+
public function testHeadersSetter($headers)
30+
{
31+
$exception = new MethodNotAllowedHttpException(array('GET'));
32+
$exception->setHeaders($headers);
33+
$this->assertSame($headers, $exception->getHeaders());
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Exception;
4+
5+
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
6+
7+
/**
8+
* Test the NotAcceptableHttpException class.
9+
*/
10+
class NotAcceptableHttpExceptionTest extends HttpExceptionTest
11+
{
12+
/**
13+
* Test that the default headers is an empty array.
14+
*/
15+
public function testHeadersDefault()
16+
{
17+
$exception = new NotAcceptableHttpException();
18+
$this->assertSame(array(), $exception->getHeaders());
19+
}
20+
21+
/**
22+
* Test that setting the headers using the setter function
23+
* is working as expected.
24+
*
25+
* @param array $headers The headers to set.
26+
*
27+
* @dataProvider headerDataProvider
28+
*/
29+
public function testHeadersSetter($headers)
30+
{
31+
$exception = new NotAcceptableHttpException();
32+
$exception->setHeaders($headers);
33+
$this->assertSame($headers, $exception->getHeaders());
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Exception;
4+
5+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
6+
7+
/**
8+
* Test the NotFoundHttpException class.
9+
*/
10+
class NotFoundHttpExceptionTest extends HttpExceptionTest
11+
{
12+
/**
13+
* Test that the default headers is an empty array.
14+
*/
15+
public function testHeadersDefault()
16+
{
17+
$exception = new NotFoundHttpException();
18+
$this->assertSame(array(), $exception->getHeaders());
19+
}
20+
21+
/**
22+
* Test that setting the headers using the setter function
23+
* is working as expected.
24+
*
25+
* @param array $headers The headers to set.
26+
*
27+
* @dataProvider headerDataProvider
28+
*/
29+
public function testHeadersSetter($headers)
30+
{
31+
$exception = new NotFoundHttpException();
32+
$exception->setHeaders($headers);
33+
$this->assertSame($headers, $exception->getHeaders());
34+
}
35+
}

0 commit comments

Comments
 (0)