Skip to content

Instantly share code, notes, and snippets.

@aranw
Created November 13, 2012 13:16
Show Gist options
  • Save aranw/4065718 to your computer and use it in GitHub Desktop.
Save aranw/4065718 to your computer and use it in GitHub Desktop.
Bug with phpunit Laravel 4
public function showPost($post = null)
{
$view = View::make('blog.post');
$view->with('title', "Aran Wilkinson - "); // We need to get the post title
$view->with('post', $this->posts->getPostBySlug($post));
//
// Get the requested blog post
// If it doesn't exist give 404 error
//
return $view;
}
<?php
class PostTest extends TestCase {
/**
* Tear down the testing environment
*
* @return void
**/
public function tearDown()
{
Mockery::close();
}
/**
* Test Post Repository
*
* @return void
**/
public function testGetLatestPosts()
{
$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 testPostBySlug()
{
$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']);
}
}
Route::get('/blog/{slug}', 'PostController@showPost');
<?php
class TestCase extends Illuminate\Foundation\Testing\TestCase {
/**
* Creates the application.
*
* @return Symfony\Component\HttpKernel\HttpKernelInterface
*/
public function createApplication()
{
$unitTesting = true;
$testEnvironment = 'testing';
return require __DIR__.'/../../start.php';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment