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

144 lines
5.3 KiB
PHP
Raw Normal View History

2018-07-01 18:31:02 +02:00
<?php
/**
* AttachmentControllerTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
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
*/
class AttachmentControllerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Passport::actingAs($this->user());
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
}
/**
* 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 = [
'filename' => 'Some new att',
'description' => sprintf('Attempt #%d', random_int(1, 10000)),
2018-12-21 16:38:10 +01:00
'model' => 'TransactionJournal',
'model_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 = [
'filename' => $attachment->filename,
'description' => sprintf('Attempt #%d', random_int(1, 10000)),
2018-12-21 16:38:10 +01:00
'model' => 'TransactionJournal',
2018-07-01 18:31:02 +02:00
'model_id' => 1,
];
// 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
}