Skip to content

Commit

Permalink
[HttpFoundation] Add create method to Json & Redirect responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Mar 15, 2012
1 parent 1c86ad7 commit ff13528
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Symfony/Component/HttpFoundation/JsonResponse.php
Expand Up @@ -38,4 +38,12 @@ public function __construct($data = array(), $status = 200, $headers = array())
array_merge(array('Content-Type' => 'application/json'), $headers)
);
}

/**
* {@inheritDoc}
*/
public static function create($data = array(), $status = 200, $headers = array())
{
return new static($data, $status, $headers);
}
}
8 changes: 8 additions & 0 deletions src/Symfony/Component/HttpFoundation/RedirectResponse.php
Expand Up @@ -63,6 +63,14 @@ public function __construct($url, $status = 302, $headers = array())
}
}

/**
* {@inheritDoc}
*/
public static function create($url = '', $status = 302, $headers = array())
{
return new static($url, $status, $headers);
}

/**
* Returns the target URL.
*
Expand Down
Expand Up @@ -79,4 +79,13 @@ public function testConstructorWithCustomContentType()
$response = new JsonResponse(array(), 200, $headers);
$this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
}

public function testCreate()
{
$response = JsonResponse::create(array('foo' => 'bar'), 204);

$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertEquals('{"foo":"bar"}', $response->getContent());
$this->assertEquals(204, $response->getStatusCode());
}
}
Expand Up @@ -40,4 +40,11 @@ public function testGetTargetUrl()
$this->assertEquals('foo.bar', $response->getTargetUrl());
}

public function testCreate()
{
$response = RedirectResponse::create('foo', 301);

$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertEquals(301, $response->getStatusCode());
}
}

0 comments on commit ff13528

Please sign in to comment.