Improve test coverage and efficiency for accounts and budgets.

This commit is contained in:
James Cole
2019-06-23 11:13:36 +02:00
parent 8f25562923
commit 43d753e5bd
47 changed files with 919 additions and 985 deletions

View File

@@ -29,6 +29,7 @@ use Illuminate\Support\Collection;
use Log;
use Mockery;
use Tests\TestCase;
use Preferences;
/**
* Class LinkControllerTest
@@ -50,6 +51,11 @@ class LinkControllerTest extends TestCase
public function testCreate(): void
{
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository = $this->mock(LinkTypeRepositoryInterface::class);
// mock default session stuff
$this->mockDefaultSession();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
@@ -66,17 +72,23 @@ class LinkControllerTest extends TestCase
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository = $this->mock(LinkTypeRepositoryInterface::class);
// create editable link type just in case:
LinkType::create(['editable' => 1, 'inward' => 'hello', 'outward' => 'bye', 'name' => 'Test type']);
$newType = LinkType::create(['editable' => 1, 'inward' => 'hello', 'outward' => 'bye', 'name' => 'Test type']);
// mock default session stuff
$this->mockDefaultSession();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
$linkType = LinkType::where('editable', 1)->first();
$repository->shouldReceive('get')->once()->andReturn(new Collection([$linkType]));
$another= LinkType::where('editable', 0)->first();
$repository->shouldReceive('get')->once()->andReturn(new Collection([$linkType, $another]));
$repository->shouldReceive('countJournals')->andReturn(2);
$this->be($this->user());
$response = $this->get(route('admin.links.delete', [$linkType->id]));
$response->assertStatus(200);
$newType->forceDelete();
}
/**
@@ -88,6 +100,9 @@ class LinkControllerTest extends TestCase
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$linkType = LinkType::where('editable', 0)->first();
// mock default session stuff
$this->mockDefaultSession();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
$this->be($this->user());
@@ -107,6 +122,10 @@ class LinkControllerTest extends TestCase
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
// mock default session stuff
$this->mockDefaultSession();
Preferences::shouldReceive('mark')->atLeast()->once();
// create editable link type just in case:
LinkType::create(['editable' => 1, 'inward' => 'hellox', 'outward' => 'byex', 'name' => 'Test typeX']);
@@ -126,10 +145,14 @@ class LinkControllerTest extends TestCase
public function testEditEditable(): void
{
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
// mock default session stuff
$this->mockDefaultSession();
// create editable link type just in case:
LinkType::create(['editable' => 1, 'inward' => 'hello Y', 'outward' => 'bye Y', 'name' => 'Test type Y']);
@@ -145,10 +168,15 @@ class LinkControllerTest extends TestCase
public function testEditNonEditable(): void
{
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
// mock default session stuff
$this->mockDefaultSession();
$linkType = LinkType::where('editable', 0)->first();
$this->be($this->user());
$response = $this->get(route('admin.links.edit', [$linkType->id]));
@@ -162,9 +190,13 @@ class LinkControllerTest extends TestCase
public function testIndex(): void
{
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
// mock default session stuff
$this->mockDefaultSession();
//Preferences::shouldReceive('mark')->atLeast()->once();
$linkTypes = LinkType::inRandomOrder()->take(3)->get();
$repository = $this->mock(LinkTypeRepositoryInterface::class);
@@ -181,8 +213,11 @@ class LinkControllerTest extends TestCase
public function testShow(): void
{
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$repository->shouldReceive('getJournalLinks')->andReturn(new Collection);
$this->mockDefaultSession();
$linkType = LinkType::first();
@@ -203,10 +238,13 @@ class LinkControllerTest extends TestCase
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
// mock default session stuff
$this->mockDefaultSession();
$data = [
'name' => 'test ' . random_int(1, 10000),
'inward' => 'test inward' . random_int(1, 10000),
'outward' => 'test outward' . random_int(1, 10000),
'name' => sprintf('test %d', $this->randomInt()),
'inward' => sprintf('test inward %d', $this->randomInt()),
'outward' => sprintf('test outward %d', $this->randomInt()),
];
$repository->shouldReceive('store')->once()->andReturn(LinkType::first());
$repository->shouldReceive('findNull')->andReturn(LinkType::first());
@@ -230,10 +268,13 @@ class LinkControllerTest extends TestCase
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
// mock default session stuff
$this->mockDefaultSession();
$data = [
'name' => 'test ' . random_int(1, 10000),
'inward' => 'test inward' . random_int(1, 10000),
'outward' => 'test outward' . random_int(1, 10000),
'name' => sprintf('test %d', $this->randomInt()),
'inward' => sprintf('test inward %d', $this->randomInt()),
'outward' => sprintf('test outward %d', $this->randomInt()),
'create_another' => '1',
];
$repository->shouldReceive('store')->once()->andReturn(new LinkType);
@@ -260,10 +301,14 @@ class LinkControllerTest extends TestCase
$linkType = LinkType::create(['editable' => 1, 'inward' => 'helloxz', 'outward' => 'bzyex', 'name' => 'Test tyzpeX']);
$repository->shouldReceive('update')->once()->andReturn(new $linkType);
// mock default session stuff
$this->mockDefaultSession();
Preferences::shouldReceive('mark')->atLeast()->once();
$data = [
'name' => 'test ' . random_int(1, 10000),
'inward' => 'test inward' . random_int(1, 10000),
'outward' => 'test outward' . random_int(1, 10000),
'name' => sprintf('test %d', $this->randomInt()),
'inward' => sprintf('test inward %d', $this->randomInt()),
'outward' => sprintf('test outward %d', $this->randomInt()),
];
$this->session(['link_types.edit.uri' => 'http://localhost']);
$this->be($this->user());
@@ -284,12 +329,14 @@ class LinkControllerTest extends TestCase
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
// mock default session stuff
$this->mockDefaultSession();
$linkType = LinkType::where('editable', 0)->first();
$data = [
'name' => 'test ' . random_int(1, 10000),
'inward' => 'test inward' . random_int(1, 10000),
'outward' => 'test outward' . random_int(1, 10000),
'name' => sprintf('test %d', $this->randomInt()),
'inward' => sprintf('test inward %d', $this->randomInt()),
'outward' => sprintf('test outward %d', $this->randomInt()),
'return_to_edit' => '1',
];
$this->session(['link_types.edit.uri' => 'http://localhost']);
@@ -314,10 +361,14 @@ class LinkControllerTest extends TestCase
// create editable link type just in case:
$linkType = LinkType::create(['editable' => 1, 'inward' => 'healox', 'outward' => 'byaex', 'name' => 'Test tyapeX']);
// mock default session stuff
$this->mockDefaultSession();
Preferences::shouldReceive('mark')->atLeast()->once();
$data = [
'name' => 'test ' . random_int(1, 10000),
'inward' => 'test inward' . random_int(1, 10000),
'outward' => 'test outward' . random_int(1, 10000),
'name' => sprintf('test %d', $this->randomInt()),
'inward' => sprintf('test inward %d', $this->randomInt()),
'outward' => sprintf('test outward %d', $this->randomInt()),
'return_to_edit' => '1',
];
$repository->shouldReceive('update')->once()->andReturn(new $linkType);