Created
November 15, 2012 10:04
-
-
Save aranw/4077795 to your computer and use it in GitHub Desktop.
Laravel 4 Tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
class NoPostsException extends Exception {} | |
class PostController extends Controller | |
{ | |
/** | |
* Blog Posts | |
* | |
* @var array | |
**/ | |
protected $posts; | |
/** | |
* undocumented function | |
* | |
* @return void | |
* @author | |
**/ | |
public function __construct(PostRepository $posts) | |
{ | |
$this->posts = $posts; | |
} | |
public function showIndex() | |
{ | |
$view = View::make('blog.index'); | |
$view->with('title', "Aran Wilkinson - Blog"); | |
$posts = $this->posts->getLatestPosts(); | |
if ($posts) | |
{ | |
// Perhaps switch this out for a Log error rather than a exception? | |
throw new NoPostsException('Unable to load blog posts from the database.'); | |
} | |
// | |
// Need to get the 5 latest blog posts | |
$view->with('posts', $posts); | |
// Also pagination needs to be taken care of? | |
// | |
return $view; | |
} | |
public function showPost($post_slug = null) | |
{ | |
$view = View::make('blog.post'); | |
$view->with('title', "Aran Wilkinson - "); // We need to get the post title | |
$post = $this->posts->getPostBySlug($post_slug); | |
if ($post) | |
{ | |
throw new NotFoundHttpException("Unable to find that blog post"); | |
} | |
$view->with('post', $post); | |
// | |
// Get the requested blog post | |
// If it doesn't exist give 404 error | |
// | |
return $view; | |
} | |
public function resolvePost($post_id) | |
{ | |
$post = $this->posts->getPostById($post_id); | |
if ($post) | |
{ | |
return Redirect::to('blog/'.$post->slug); | |
} | |
else | |
{ | |
// Trigger 404 | |
throw new NotFoundHttpException('Unable to find that blog post'); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
class PostTest extends TestCase { | |
/** | |
* Tear down the testing environment | |
* | |
* @return void | |
**/ | |
public function tearDown() | |
{ | |
Mockery::close(); | |
} | |
/** | |
* Test Post Repository | |
* | |
* @return void | |
**/ | |
public function testIndexPage() | |
{ | |
$mockRepository = Mockery::mock('PostRepository'); | |
$mockRepository->shouldReceive('getLatestPosts')->once()->andReturn('foo'); | |
$this->app->instance('PostRepository', $mockRepository); | |
$response = $this->call('GET', '/blog'); | |
$view = $response->getOriginalContent(); | |
$this->assertTrue($response->isOk()); | |
$this->assertEquals('foo', $view['posts']); | |
} | |
/** | |
* Test Single Post | |
* | |
* @return void | |
**/ | |
public function testBlogPage() | |
{ | |
$mockRepository = Mockery::mock('PostRepository'); | |
$mockRepository->shouldReceive('getPostBySlug')->with("mockery")->once()->andReturn('foobar'); | |
$this->app->instance('PostRepository', $mockRepository); | |
$response = $this->call('GET', '/blog/mockery'); | |
$view = $response->getOriginalContent(); | |
$this->assertTrue($response->isOk()); | |
$this->assertEquals('foobar', $view['post']); | |
} | |
/** | |
* Test Post ID Query | |
* | |
* @return void | |
**/ | |
public function testBlogResolveRedirect() | |
{ | |
// For the PostById query we need a $post object with a slug | |
$post = new stdClass; | |
$post->id = 1; | |
$post->slug = 'foo'; | |
$mockRepository = Mockery::mock('PostRepository'); | |
$this->app->instance('PostRepository', $mockRepository); | |
$controller = new PostController; | |
// Return the $post object to the function giving our $post->slug | |
$mockRepository->shouldReceive('getPostById')->with(1)->once()->andReturn($post); | |
// This is testing route which is not necessary here, can be tested elsewhere | |
//$response = $this->call('GET', '/1'); | |
//$this->assertTrue($response->isOk()); | |
$response = $controller->resolvePost($post->id); | |
$this->assertInstanceOf('Illuminate\Http\RedirectResponse', $response); | |
} | |
public function testBlogResolveRedirectUnkown() | |
{ | |
try | |
{ | |
$mockRepository = Mockery::mock('PostRepository'); | |
$mockRepository->shouldReceive('getPostById')->with(100)->once()->andReturn(null); | |
$this->app->instance('PostRepository', $mockRepository); | |
$response = $this->call('GET', '/100'); | |
} | |
catch (NotFoundHttpException $expected) | |
{ | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment