mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-18 15:39:50 +00:00
Improve test coverage for API.
This commit is contained in:
@@ -25,7 +25,9 @@ namespace Tests\Api\V1\Controllers;
|
||||
|
||||
|
||||
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
||||
use FireflyIII\Transformers\UserTransformer;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Laravel\Passport\Passport;
|
||||
use Log;
|
||||
use Mockery;
|
||||
@@ -95,6 +97,7 @@ class UserControllerTest extends TestCase
|
||||
{
|
||||
$userRepository = $this->mock(UserRepositoryInterface::class);
|
||||
$userRepository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
|
||||
|
||||
// create a user first:
|
||||
// call API
|
||||
$response = $this->delete('/api/v1/users/' . $this->user()->id, [], ['Accept' => 'application/json']);
|
||||
@@ -109,20 +112,20 @@ class UserControllerTest extends TestCase
|
||||
*/
|
||||
public function testIndex(): void
|
||||
{
|
||||
// create stuff
|
||||
$users = factory(User::class, 10)->create();
|
||||
// mock stuff:
|
||||
$repository = $this->mock(UserRepositoryInterface::class);
|
||||
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true);
|
||||
$repository = $this->mock(UserRepositoryInterface::class);
|
||||
$transformer = $this->mock(UserTransformer::class);
|
||||
|
||||
// mock calls:
|
||||
$repository->shouldReceive('all')->withAnyArgs()->andReturn($users)->once();
|
||||
|
||||
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true);
|
||||
$repository->shouldReceive('all')->withAnyArgs()->andReturn(new Collection)->once();
|
||||
$transformer->shouldReceive('setParameters')->atLeast()->once();
|
||||
|
||||
// test API
|
||||
$response = $this->get('/api/v1/users', ['Accept' => 'application/json']);
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['data' => [],]);
|
||||
$response->assertJson(['meta' => ['pagination' => ['total' => 10, 'count' => 10, 'current_page' => 1, 'total_pages' => 1]],]);
|
||||
$response->assertJson(['meta' => ['pagination' => ['total' => 0, 'count' => 0, 'current_page' => 1, 'total_pages' => 1]],]);
|
||||
$response->assertJson(['links' => ['self' => true, 'first' => true, 'last' => true,],]);
|
||||
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||
}
|
||||
@@ -136,12 +139,19 @@ class UserControllerTest extends TestCase
|
||||
{
|
||||
$user = User::first();
|
||||
$repository = $this->mock(UserRepositoryInterface::class);
|
||||
$transformer = $this->mock(UserTransformer::class);
|
||||
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true);
|
||||
|
||||
// mock transformer
|
||||
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
|
||||
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
|
||||
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
|
||||
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
|
||||
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
|
||||
|
||||
// test API
|
||||
$response = $this->get('/api/v1/users/' . $user->id, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee($user->email);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,13 +168,20 @@ class UserControllerTest extends TestCase
|
||||
|
||||
// mock
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$transformer = $this->mock(UserTransformer::class);
|
||||
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);
|
||||
$userRepos->shouldReceive('store')->once()->andReturn($this->user());
|
||||
|
||||
// mock transformer
|
||||
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
|
||||
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
|
||||
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
|
||||
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
|
||||
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
|
||||
|
||||
// test API
|
||||
$response = $this->post('/api/v1/users', $data, ['Content-Type' => 'application/x-www-form-urlencoded']);
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee($this->user()->email);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,13 +200,20 @@ class UserControllerTest extends TestCase
|
||||
|
||||
// mock
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$transformer= $this->mock(UserTransformer::class);
|
||||
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);
|
||||
$userRepos->shouldReceive('store')->once()->andReturn($this->user());
|
||||
|
||||
// mock transformer
|
||||
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
|
||||
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
|
||||
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
|
||||
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
|
||||
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
|
||||
|
||||
// test API
|
||||
$response = $this->postJson('/api/v1/users', $data, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee($this->user()->email);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -273,9 +297,17 @@ class UserControllerTest extends TestCase
|
||||
|
||||
// mock
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$transformer = $this->mock(UserTransformer::class);
|
||||
$userRepos->shouldReceive('update')->once()->andReturn($user);
|
||||
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);
|
||||
|
||||
// mock transformer
|
||||
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
|
||||
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
|
||||
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
|
||||
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
|
||||
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
|
||||
|
||||
// call API
|
||||
$response = $this->put('/api/v1/users/' . $user->id, $data, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(200);
|
||||
@@ -300,9 +332,17 @@ class UserControllerTest extends TestCase
|
||||
|
||||
// mock
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$transformer = $this->mock(UserTransformer::class);
|
||||
$userRepos->shouldReceive('update')->once()->andReturn($user);
|
||||
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);
|
||||
|
||||
// mock transformer
|
||||
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
|
||||
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
|
||||
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
|
||||
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
|
||||
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
|
||||
|
||||
// call API
|
||||
$response = $this->putJson('/api/v1/users/' . $user->id, $data, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(200);
|
||||
|
||||
Reference in New Issue
Block a user