Files
firefly-iii/tests/Api/V1/Controllers/AttachmentControllerTest.php

177 lines
6.7 KiB
PHP
Raw Normal View History

2018-07-01 18:31:02 +02:00
<?php
/**
* AttachmentControllerTest.php
2020-02-16 13:59:55 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-07-01 18:31:02 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-07-01 18:31:02 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-07-01 18:31:02 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-07-01 18:31:02 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-07-01 18:31:02 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-07-01 18:31:02 +02:00
*/
declare(strict_types=1);
namespace Tests\Api\V1\Controllers;
use Exception;
2018-07-01 18:31:02 +02:00
use FireflyIII\Models\Attachment;
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2018-12-16 13:55:19 +01:00
use FireflyIII\Transformers\AttachmentTransformer;
2018-07-01 18:31:02 +02:00
use Laravel\Passport\Passport;
use Log;
use Tests\TestCase;
/**
*
* Class AttachmentControllerTest
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2018-07-01 18:31:02 +02:00
*/
class AttachmentControllerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Passport::actingAs($this->user());
2020-03-15 15:32:09 +01:00
$this->mockDefaultConfiguration();
2019-04-09 20:05:20 +02:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-07-01 18:31:02 +02:00
}
2019-06-08 06:19:21 +02:00
/**
* 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);
}
2018-07-01 18:31:02 +02:00
/**
* Store a new attachment.
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
* @throws Exception
2018-07-01 18:31:02 +02:00
*/
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);
2018-12-16 13:55:19 +01:00
// 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]);
2018-07-01 18:31:02 +02:00
// mock calls:
$journal = $this->getRandomWithdrawal();
2018-09-27 13:11:31 +02:00
$repository->shouldReceive('setUser')->atLeast()->once();
2018-07-01 18:31:02 +02:00
$repository->shouldReceive('store')->once()->andReturn($attachment);
2018-09-27 13:11:31 +02:00
$repository->shouldReceive('getNoteText')->andReturn('Hi There');
2018-07-01 18:31:02 +02:00
$journalRepos->shouldReceive('setUser')->once();
2018-12-21 16:38:10 +01:00
$journalRepos->shouldReceive('findNull')->once()->andReturn($journal);
2018-07-01 18:31:02 +02:00
// data to submit
$data = [
2020-03-15 15:32:09 +01:00
'filename' => 'Some new att',
'description' => sprintf('Attempt #%d', $this->randomInt()),
'attachable_type' => 'TransactionJournal',
'attachable_id' => $journal->id,
2018-07-01 18:31:02 +02:00
];
// test API
2018-12-21 16:38:10 +01:00
$response = $this->post(route('api.v1.attachments.store'), $data, ['accept' => 'application/json']);
2018-07-01 18:31:02 +02:00
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
2018-12-21 16:38:10 +01:00
2018-07-01 18:31:02 +02:00
}
/**
* Update an attachment.
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
* @throws Exception
2018-07-01 18:31:02 +02:00
*/
public function testUpdate(): void
{
// mock repositories
$repository = $this->mock(AttachmentRepositoryInterface::class);
$transformer = $this->mock(AttachmentTransformer::class);
2018-12-16 13:55:19 +01:00
// 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]);
2018-07-01 18:31:02 +02:00
/** @var Attachment $attachment */
$attachment = $this->user()->attachments()->first();
// mock calls:
2018-09-27 13:11:31 +02:00
$repository->shouldReceive('setUser')->atLeast()->once();
2018-07-01 18:31:02 +02:00
$repository->shouldReceive('update')->once()->andReturn($attachment);
2018-09-27 13:11:31 +02:00
$repository->shouldReceive('getNoteText')->andReturn('Hi There');
2018-07-01 18:31:02 +02:00
// data to submit
$data = [
2020-03-15 15:32:09 +01:00
'filename' => $attachment->filename,
'description' => sprintf('Attempt #%d', $this->randomInt()),
'attachable_type' => 'TransactionJournal',
'attachable_id' => 1,
2018-07-01 18:31:02 +02:00
];
// test API
2018-12-16 13:55:19 +01:00
$response = $this->put(route('api.v1.attachments.update', [$attachment->id]), $data, ['Accept' => 'application/json']);
2018-07-01 18:31:02 +02:00
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
2018-07-22 20:33:17 +02:00
}