. */ declare(strict_types=1); namespace Tests\Api\V1\Controllers; use Exception; use FireflyIII\Models\Attachment; use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Transformers\AttachmentTransformer; use Laravel\Passport\Passport; use Log; use Tests\TestCase; /** * * Class AttachmentControllerTest * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @SuppressWarnings(PHPMD.TooManyPublicMethods) */ class AttachmentControllerTest extends TestCase { /** * */ public function setUp(): void { parent::setUp(); Passport::actingAs($this->user()); $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } /** * Test show attachment. * @covers \FireflyIII\Api\V1\Controllers\AttachmentController */ public function testShow(): void { $transformer = $this->mock(AttachmentTransformer::class); $repository = $this->mock(AttachmentRepositoryInterface::class); // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); $transformer->shouldReceive('setParameters')->atLeast()->once(); $transformer->shouldReceive('transform')->atLeast()->once()->andReturn( [ 'id' => 1, 'attributes' => [ 'file' => 'Test.pdf', ], ]); $transformer->shouldReceive('setCurrentScope')->atLeast()->once(); $transformer->shouldReceive('getDefaultIncludes')->atLeast()->once()->andReturn([]); $transformer->shouldReceive('getAvailableIncludes')->atLeast()->once()->andReturn([]); $attachment = $this->user()->attachments()->inRandomOrder()->first(); // test API $response = $this->get(route('api.v1.attachments.show', [$attachment->id])); $response->assertStatus(200); } /** * Store a new attachment. * * @covers \FireflyIII\Api\V1\Controllers\AttachmentController * @throws Exception */ public function testStore(): void { /** @var Attachment $attachment */ $attachment = $this->user()->attachments()->first(); // mock stuff: $repository = $this->mock(AttachmentRepositoryInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); $transformer = $this->mock(AttachmentTransformer::class); // 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]); // mock calls: $journal = $this->getRandomWithdrawal(); $repository->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('store')->once()->andReturn($attachment); $repository->shouldReceive('getNoteText')->andReturn('Hi There'); $journalRepos->shouldReceive('setUser')->once(); $journalRepos->shouldReceive('findNull')->once()->andReturn($journal); // data to submit $data = [ 'filename' => 'Some new att', 'description' => sprintf('Attempt #%d', $this->randomInt()), 'attachable_type' => 'TransactionJournal', 'attachable_id' => $journal->id, ]; // test API $response = $this->post(route('api.v1.attachments.store'), $data, ['accept' => 'application/json']); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } /** * Update an attachment. * * @covers \FireflyIII\Api\V1\Controllers\AttachmentController * @throws Exception */ public function testUpdate(): void { // mock repositories $repository = $this->mock(AttachmentRepositoryInterface::class); $transformer = $this->mock(AttachmentTransformer::class); // 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]); /** @var Attachment $attachment */ $attachment = $this->user()->attachments()->first(); // mock calls: $repository->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('update')->once()->andReturn($attachment); $repository->shouldReceive('getNoteText')->andReturn('Hi There'); // data to submit $data = [ 'filename' => $attachment->filename, 'description' => sprintf('Attempt #%d', $this->randomInt()), 'attachable_type' => 'TransactionJournal', 'attachable_id' => 1, ]; // test API $response = $this->put(route('api.v1.attachments.update', [$attachment->id]), $data, ['Accept' => 'application/json']); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } }