Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Re-implemented basic account controller tests.
James Cole5528663727d8
diff --git a/phpunit.coverage.xml b/phpunit.coverage.xmlindex 75e3afd143..d643a5d6ec 100755--- a/phpunit.coverage.xml+++ b/phpunit.coverage.xml@@ -10,11 +10,12 @@beStrictAboutOutputDuringTests="true"stopOnFailure="true"><testsuites>- <testsuite name="acceptance">- <directory suffix="Test.php">./tests/acceptance</directory>+ <testsuite name="Feature Tests">+ <directory suffix="Test.php">./tests/Feature</directory></testsuite>- <testsuite name="unit">- <directory suffix="Test.php">./tests/unit</directory>++ <testsuite name="Unit Tests">+ <directory suffix="Test.php">./tests/Unit</directory></testsuite></testsuites>diff --git a/phpunit.xml b/phpunit.xmlindex c096c7a945..b0e315533c 100755--- a/phpunit.xml+++ b/phpunit.xml@@ -10,11 +10,12 @@beStrictAboutOutputDuringTests="true"stopOnFailure="true"><testsuites>- <testsuite name="acceptance">- <directory suffix="Test.php">./tests/acceptance</directory>+ <testsuite name="Feature Tests">+ <directory suffix="Test.php">./tests/Feature</directory></testsuite>- <testsuite name="unit">- <directory suffix="Test.php">./tests/unit</directory>++ <testsuite name="Unit Tests">+ <directory suffix="Test.php">./tests/Unit</directory></testsuite></testsuites><filter>diff --git a/tests/Feature/Controllers/AccountControllerTest.php b/tests/Feature/Controllers/AccountControllerTest.phpnew file mode 100644index 0000000000..37c9b79740--- /dev/null+++ b/tests/Feature/Controllers/AccountControllerTest.php@@ -0,0 +1,213 @@+<?php+/**+ * AccountControllerTest.php+ * Copyright (c) 2017 [email protected]+ * This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */++declare(strict_types = 1);++namespace Feature\Controllers;++use Carbon\Carbon;+use FireflyIII\Helpers\Collector\JournalCollectorInterface;+use FireflyIII\Models\Account;+use FireflyIII\Repositories\Account\AccountRepositoryInterface;+use FireflyIII\Repositories\Account\AccountTaskerInterface;+use Illuminate\Pagination\LengthAwarePaginator;+use Illuminate\Support\Collection;+use Tests\TestCase;++class AccountControllerTest extends TestCase+{+ /**+ * @covers \FireflyIII\Http\Controllers\AccountController::create+ */+ public function testCreate()+ {+ $this->be($this->user());+ $response = $this->get(route('accounts.create', ['asset']));+ $response->assertStatus(200);+ // has bread crumb+ $response->assertSee('<ol class="breadcrumb">');+ }++ /**+ * @covers \FireflyIII\Http\Controllers\AccountController::delete+ */+ public function testDelete()+ {+ $this->be($this->user());+ $response = $this->get(route('accounts.delete', [1]));+ $response->assertStatus(200);+ // has bread crumb+ $response->assertSee('<ol class="breadcrumb">');+ }++ /**+ * @covers \FireflyIII\Http\Controllers\AccountController::destroy+ */+ public function testDestroy()+ {+ $this->session(['accounts.delete.url' => 'http://localhost/accounts/show/1']);++ $repository = $this->mock(AccountRepositoryInterface::class);+ $repository->shouldReceive('find')->withArgs([0])->once()->andReturn(new Account);+ $repository->shouldReceive('destroy')->andReturn(true);++ $this->be($this->user());+ $response = $this->post(route('accounts.destroy', [1]));+ $response->assertStatus(302);+ $response->assertSessionHas('success');+ }++ /**+ * @covers \FireflyIII\Http\Controllers\AccountController::edit+ */+ public function testEdit()+ {+ $this->be($this->user());+ $response = $this->get(route('accounts.edit', [1]));+ $response->assertStatus(200);+ // has bread crumb+ $response->assertSee('<ol class="breadcrumb">');+ }++ /**+ * @covers \FireflyIII\Http\Controllers\AccountController::index+ * @covers \FireflyIII\Http\Controllers\AccountController::isInArray+ * @dataProvider dateRangeProvider+ *+ * @param string $range+ */+ public function testIndex(string $range)+ {+ $this->be($this->user());+ $this->changeDateRange($this->user(), $range);+ $response = $this->get(route('accounts.index', ['asset']));+ $response->assertStatus(200);+ // has bread crumb+ $response->assertSee('<ol class="breadcrumb">');+ }++ /**+ * @covers \FireflyIII\Http\Controllers\AccountController::show+ * @covers \FireflyIII\Http\Controllers\AccountController::periodEntries+ * @dataProvider dateRangeProvider+ *+ * @param string $range+ */+ public function testShow(string $range)+ {+ $date = new Carbon;+ $this->session(['start' => $date, 'end' => clone $date]);++ $tasker = $this->mock(AccountTaskerInterface::class);+ $tasker->shouldReceive('amountOutInPeriod')->withAnyArgs()->andReturn('-1');+ $tasker->shouldReceive('amountInInPeriod')->withAnyArgs()->andReturn('1');++ // mock repository:+ $repository = $this->mock(AccountRepositoryInterface::class);+ $repository->shouldReceive('oldestJournalDate')->andReturn(clone $date);+ $repository->shouldReceive('getAccountsByType')->andReturn(new Collection);+++ $collector = $this->mock(JournalCollectorInterface::class);+ $collector->shouldReceive('setAccounts')->andReturnSelf();+ $collector->shouldReceive('setRange')->andReturnSelf();+ $collector->shouldReceive('setLimit')->andReturnSelf();+ $collector->shouldReceive('setPage')->andReturnSelf();+ $collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10));+++ $this->be($this->user());+ $this->changeDateRange($this->user(), $range);+ $response = $this->get(route('accounts.show', [1]));+ $response->assertStatus(200);+ // has bread crumb+ $response->assertSee('<ol class="breadcrumb">');+ }++ /**+ * @covers \FireflyIII\Http\Controllers\AccountController::showAll+ * @dataProvider dateRangeProvider+ *+ * @param string $range+ */+ public function testShowAll(string $range)+ {+ $this->be($this->user());+ $this->changeDateRange($this->user(), $range);+ $response = $this->get(route('accounts.show.all', [1]));+ $response->assertStatus(200);+ // has bread crumb+ $response->assertSee('<ol class="breadcrumb">');+ }++ /**+ * @covers \FireflyIII\Http\Controllers\AccountController::showByDate+ * @dataProvider dateRangeProvider+ *+ * @param string $range+ */+ public function testShowByDate(string $range)+ {+ $this->be($this->user());+ $this->changeDateRange($this->user(), $range);+ $response = $this->get(route('accounts.show.date', [1, '2016-01-01']));+ $response->assertStatus(200);+ // has bread crumb+ $response->assertSee('<ol class="breadcrumb">');+ }++ /**+ * @covers \FireflyIII\Http\Controllers\AccountController::store+ */+ public function testStore()+ {+ $this->session(['accounts.create.url' => 'http://localhost']);+ $this->be($this->user());+ $data = [+ 'name' => 'new account ' . rand(1000, 9999),+ 'what' => 'asset',+ ];++ $response = $this->post(route('accounts.store', ['asset']), $data);+ $response->assertStatus(302);+ $response->assertSessionHas('success');++ // list should have this new account.+ $response = $this->get(route('accounts.index', ['asset']));+ $response->assertStatus(200);+ // has bread crumb+ $response->assertSee('<ol class="breadcrumb">');+ $response->assertSee($data['name']);+ }++ /**+ * @covers \FireflyIII\Http\Controllers\AccountController::update+ */+ public function testUpdate()+ {+ $this->session(['accounts.edit.url' => 'http://localhost']);+ $this->be($this->user());+ $data = [+ 'name' => 'updated account ' . rand(1000, 9999),+ 'active' => 1,+ 'what' => 'asset',+ ];++ $response = $this->post(route('accounts.update', [1]), $data);+ $response->assertStatus(302);+ $response->assertSessionHas('success');++ // list should have this new account.+ $response = $this->get(route('accounts.index', ['asset']));+ $response->assertStatus(200);+ // has bread crumb+ $response->assertSee('<ol class="breadcrumb">');+ $response->assertSee($data['name']);+ }+}\ No newline at end of filediff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.phpindex 486dc271a3..a8a339029f 100644--- a/tests/Feature/ExampleTest.php+++ b/tests/Feature/ExampleTest.php@@ -1,4 +1,11 @@<?php+/**+ * ExampleTest.php+ * Copyright (c) 2017 [email protected]+ * This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */namespace Tests\Feature;@@ -16,8 +23,20 @@ class ExampleTest extends TestCase*/public function testBasicTest(){- $response = $this->get('/');+ $response = $this->get('/login');$response->assertStatus(200);}++ /**+ * A basic test example.+ *+ * @return void+ */+ public function testAnotherBasicTest()+ {+ $response = $this->get('/');++ $response->assertStatus(302);+ }}diff --git a/tests/TestCase.php b/tests/TestCase.phpindex 51b7cf0735..83bc278de8 100644--- a/tests/TestCase.php+++ b/tests/TestCase.php@@ -2,8 +2,12 @@namespace Tests;+use FireflyIII\User;use Illuminate\Foundation\Testing\TestCase as BaseTestCase;-+use Log;+use Mockery;+use FireflyIII\Models\Preference;+use Carbon\Carbon;/*** Class TestCase*@@ -12,4 +16,75 @@ use Illuminate\Foundation\Testing\TestCase as BaseTestCase;abstract class TestCase extends BaseTestCase{use CreatesApplication;++ /**+ * @return User+ */+ public function user()+ {+ $user = User::find(1);++ return $user;+ }++ /**+ * @param User $user+ * @param string $range+ */+ public function changeDateRange(User $user, $range)+ {+ $valid = ['1D', '1W', '1M', '3M', '6M', '1Y', 'custom'];+ if (in_array($range, $valid)) {+ Preference::where('user_id', $user->id)->where('name', 'viewRange')->delete();+ Preference::create(+ [+ 'user_id' => $user->id,+ 'name' => 'viewRange',+ 'data' => $range,+ ]+ );+ // set period to match?++ }+ if ($range === 'custom') {+ $this->session(+ [+ 'start' => Carbon::now()->subDays(20),+ 'end' => Carbon::now(),+ ]+ );+ }+ }+++ /**+ * @param string $class+ *+ * @return \Mockery\MockInterface+ */+ protected function mock($class)+ {+ Log::debug(sprintf('Will now mock %s', $class));+ $object = Mockery::mock($class);+ $this->app->instance($class, $object);++ return $object;+ }++ /**+ * @return array+ */+ public function dateRangeProvider()+ {+ return [+ 'one day' => ['1D'],+ 'one week' => ['1W'],+ 'one month' => ['1M'],+ 'three months' => ['3M'],+ 'six months' => ['6M'],+ 'one year' => ['1Y'],+ 'custom range' => ['custom'],+ ];+ }+}