Better mocking of objects.

This commit is contained in:
James Cole 2016-01-23 06:59:22 +01:00
parent bf9c1c1875
commit 0d5efb8d27
3 changed files with 31 additions and 33 deletions

View File

@ -28,14 +28,6 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
return $app; return $app;
} }
/**
* @return User
*/
public function user()
{
return User::find(1);
}
/** /**
* @return User * @return User
*/ */
@ -44,7 +36,6 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
return User::find(2); return User::find(2);
} }
/** /**
* Sets up the fixture, for example, opens a network connection. * Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed. * This method is called before a test is executed.
@ -65,7 +56,7 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
touch($original); touch($original);
Artisan::call('migrate', ['--seed' => true]); Artisan::call('migrate', ['--seed' => true]);
} }
copy($original, $copy); copy($original, $copy);
} else { } else {
if (file_exists($copy)) { if (file_exists($copy)) {
@ -99,5 +90,28 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
} }
/**
* @return User
*/
public function user()
{
return User::find(1);
}
/**
* @param string $class
*
* @return \Mockery\MockInterface
*/
protected function mock($class)
{
$object = Mockery::mock($class);
$this->app->instance($class, $object);
return $object;
}
} }

View File

@ -22,12 +22,10 @@ class ChartBudgetControllerTest extends TestCase
public function testBudget() public function testBudget()
{ {
$repository = Mockery::mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
$repository->shouldReceive('getExpensesPerMonth')->once()->andReturn(new Collection([new Budget])); $repository->shouldReceive('getExpensesPerMonth')->once()->andReturn(new Collection([new Budget]));
$repository->shouldReceive('getFirstBudgetLimitDate')->once()->andReturn(new Carbon); $repository->shouldReceive('getFirstBudgetLimitDate')->once()->andReturn(new Carbon);
$this->app->instance('FireflyIII\Repositories\Budget\BudgetRepositoryInterface', $repository);
$this->be($this->user()); $this->be($this->user());
$response = $this->call('GET', '/chart/budget/1'); $response = $this->call('GET', '/chart/budget/1');
$this->assertEquals(200, $response->status()); $this->assertEquals(200, $response->status());
@ -67,13 +65,10 @@ class ChartBudgetControllerTest extends TestCase
$budget->dateFormatted = '2015'; $budget->dateFormatted = '2015';
$budget->budgeted = 120; $budget->budgeted = 120;
$repository = Mockery::mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
$repository->shouldReceive('getBudgetedPerYear')->once()->andReturn(new Collection([$budget])); $repository->shouldReceive('getBudgetedPerYear')->once()->andReturn(new Collection([$budget]));
$repository->shouldReceive('getBudgetsAndExpensesPerYear')->once()->andReturn([]); $repository->shouldReceive('getBudgetsAndExpensesPerYear')->once()->andReturn([]);
$this->app->instance('FireflyIII\Repositories\Budget\BudgetRepositoryInterface', $repository);
$this->be($this->user()); $this->be($this->user());
$response = $this->call('GET', '/chart/budget/multi-year/default/20150101/20160101/1/1'); $response = $this->call('GET', '/chart/budget/multi-year/default/20150101/20160101/1/1');
$this->assertEquals(200, $response->status()); $this->assertEquals(200, $response->status());
@ -85,11 +80,9 @@ class ChartBudgetControllerTest extends TestCase
*/ */
public function testYear() public function testYear()
{ {
$repository = Mockery::mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
$repository->shouldReceive('getBudgetsAndExpensesPerMonth')->once()->andReturn([]); $repository->shouldReceive('getBudgetsAndExpensesPerMonth')->once()->andReturn([]);
$this->app->instance('FireflyIII\Repositories\Budget\BudgetRepositoryInterface', $repository);
$this->be($this->user()); $this->be($this->user());
$response = $this->call('GET', '/chart/budget/year/default/20150101/20151231/1'); $response = $this->call('GET', '/chart/budget/year/default/20150101/20151231/1');
$this->assertEquals(200, $response->status()); $this->assertEquals(200, $response->status());

View File

@ -41,11 +41,9 @@ class ChartCategoryControllerTest extends TestCase
*/ */
public function testEarnedInPeriod() public function testEarnedInPeriod()
{ {
$repository = Mockery::mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface');
$repository->shouldReceive('earnedForAccountsPerMonth')->once()->andReturn(new Collection); $repository->shouldReceive('earnedForAccountsPerMonth')->once()->andReturn(new Collection);
$this->app->instance('FireflyIII\Repositories\Category\CategoryRepositoryInterface', $repository);
$this->be($this->user()); $this->be($this->user());
$response = $this->call('GET', '/chart/category/earned-in-period/default/20150101/20151231/1'); $response = $this->call('GET', '/chart/category/earned-in-period/default/20150101/20151231/1');
$this->assertEquals(200, $response->status()); $this->assertEquals(200, $response->status());
@ -56,13 +54,10 @@ class ChartCategoryControllerTest extends TestCase
*/ */
public function testFrontpage() public function testFrontpage()
{ {
$repository = Mockery::mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface');
$repository->shouldReceive('spentForAccountsPerMonth')->once()->andReturn(new Collection); $repository->shouldReceive('spentForAccountsPerMonth')->once()->andReturn(new Collection);
$repository->shouldReceive('sumSpentNoCategory')->once()->andReturn('120'); $repository->shouldReceive('sumSpentNoCategory')->once()->andReturn('120');
$this->app->instance('FireflyIII\Repositories\Category\CategoryRepositoryInterface', $repository);
$this->be($this->user()); $this->be($this->user());
$response = $this->call('GET', '/chart/category/frontpage'); $response = $this->call('GET', '/chart/category/frontpage');
$this->assertEquals(200, $response->status()); $this->assertEquals(200, $response->status());
@ -73,11 +68,9 @@ class ChartCategoryControllerTest extends TestCase
*/ */
public function testMultiYear() public function testMultiYear()
{ {
$repository = Mockery::mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface');
$repository->shouldReceive('listMultiYear')->once()->andReturn(new Collection); $repository->shouldReceive('listMultiYear')->once()->andReturn(new Collection);
$this->app->instance('FireflyIII\Repositories\Category\CategoryRepositoryInterface', $repository);
$this->be($this->user()); $this->be($this->user());
$response = $this->call('GET', '/chart/category/multi-year/default/20150101/20151231/1/1'); $response = $this->call('GET', '/chart/category/multi-year/default/20150101/20151231/1/1');
$this->assertEquals(200, $response->status()); $this->assertEquals(200, $response->status());
@ -101,11 +94,9 @@ class ChartCategoryControllerTest extends TestCase
{ {
$repository = Mockery::mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface');
$repository->shouldReceive('spentForAccountsPerMonth')->once()->andReturn(new Collection); $repository->shouldReceive('spentForAccountsPerMonth')->once()->andReturn(new Collection);
$this->app->instance('FireflyIII\Repositories\Category\CategoryRepositoryInterface', $repository);
$this->be($this->user()); $this->be($this->user());
$response = $this->call('GET', '/chart/category/spent-in-period/default/20150101/20151231/1'); $response = $this->call('GET', '/chart/category/spent-in-period/default/20150101/20151231/1');
$this->assertEquals(200, $response->status()); $this->assertEquals(200, $response->status());