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

302 lines
10 KiB
PHP
Raw Normal View History

2018-07-01 11:31:02 -05: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 FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Models\Attachment;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
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());
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* Destroy account over API.
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
*/
public function testDelete(): void
{
// mock stuff:
$repository = $this->mock(AttachmentRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('destroy')->once()->andReturn(true);
// get attachment:
$attachment = $this->user()->attachments()->first();
// call API
$response = $this->delete('/api/v1/attachments/' . $attachment->id);
$response->assertStatus(204);
}
/**
* Download attachment
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
*/
public function testDownload(): void
{
// mock stuff:
$repository = $this->mock(AttachmentRepositoryInterface::class);
$content = 'Attachment content ' . random_int(100, 1000);
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('exists')->andReturn(true)->once();
$repository->shouldReceive('getContent')->andReturn($content)->once();
// get attachment:
$attachment = $this->user()->attachments()->first();
// call API
$response = $this->get('/api/v1/attachments/' . $attachment->id . '/download');
$response->assertStatus(200);
$response->assertSee($content);
}
/**
* Download attachment but file doesn't exist.
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
* @expectedExceptionMessage Some error message
*/
public function testDownloadNotExisting(): void
{
// mock stuff:
$repository = $this->mock(AttachmentRepositoryInterface::class);
$content = 'Attachment content ' . random_int(100, 1000);
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('exists')->andReturn(false)->once();
// get attachment:
$attachment = $this->user()->attachments()->first();
// call API
$response = $this->get('/api/v1/attachments/' . $attachment->id . '/download');
$response->assertStatus(500);
$response->assertSee('Could not find the indicated attachment. The file is no longer there.');
}
/**
2018-07-01 11:58:41 -05:00
* Download attachment but no file uploaded
2018-07-01 11:31:02 -05:00
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
*/
public function testDownloadNotUploaded(): void
{
// mock stuff:
$repository = $this->mock(AttachmentRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
// create attachment
$attachment = Attachment::create(
[
'user_id' => $this->user()->id,
'attachable_id' => 1,
'attachable_type' => TransactionJournal::class,
'md5' => md5('Hello' . random_int(1, 10000)),
2018-07-01 11:31:02 -05:00
'filename' => 'some name',
'mime' => 'text/plain',
'size' => 5,
'uploaded' => false,
]
);
// call API
$response = $this->get('/api/v1/attachments/' . $attachment->id . '/download');
$response->assertStatus(500);
$response->assertSee('No file has been uploaded for this attachment (yet).');
}
/**
* List all attachments.
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
*/
public function testIndex(): void
{
// create stuff
$attachments = factory(Attachment::class, 10)->create();
// mock stuff:
$repository = $this->mock(AttachmentRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('get')->once()->andReturn($attachments);
// test API
$response = $this->get('/api/v1/attachments');
$response->assertStatus(200);
$response->assertJson(['data' => [],]);
$response->assertJson(['meta' => ['pagination' => ['total' => 10, 'count' => 10, 'per_page' => true, 'current_page' => 1, 'total_pages' => 1]],]);
$response->assertJson(['links' => ['self' => true, 'first' => true, 'last' => true,],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* List one attachment.
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
*/
public function testShow(): void
{
/** @var Attachment $attachment */
$attachment = $this->user()->attachments()->first();
// mock stuff:
$repository = $this->mock(AttachmentRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
// test API
$response = $this->get('/api/v1/attachments/' . $attachment->id);
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]);
$response->assertSee($attachment->filename); // attachment file name
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Store a new attachment.
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
* @covers \FireflyIII\Api\V1\Requests\AttachmentRequest
2018-07-01 11:31:02 -05: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);
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('store')->once()->andReturn($attachment);
$journalRepos->shouldReceive('setUser')->once();
$journalRepos->shouldReceive('findNull')->once()->andReturn($this->user()->transactionJournals()->find(1));
// data to submit
$data = [
'filename' => 'Some new att',
'description' => sprintf('Attempt #%d', random_int(1, 10000)),
2018-07-01 11:31:02 -05:00
'model' => TransactionJournal::class,
'model_id' => 1,
];
// test API
$response = $this->post('/api/v1/attachments', $data);
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]);
$response->assertSee($attachment->filename); // the file name.
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Update an attachment.
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
* @covers \FireflyIII\Api\V1\Requests\AttachmentRequest
*/
public function testUpdate(): void
{
// mock repositories
$repository = $this->mock(AttachmentRepositoryInterface::class);
/** @var Attachment $attachment */
$attachment = $this->user()->attachments()->first();
// mock calls:
$repository->shouldReceive('setUser');
$repository->shouldReceive('update')->once()->andReturn($attachment);
// data to submit
$data = [
'filename' => $attachment->filename,
'description' => sprintf('Attempt #%d', random_int(1, 10000)),
2018-07-01 11:31:02 -05:00
'model' => TransactionJournal::class,
'model_id' => 1,
];
// test API
$response = $this->put('/api/v1/attachments/' . $attachment->id, $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
$response->assertSee($attachment->description);
}
2018-07-01 11:58:41 -05:00
/**
* Upload file for attachment.
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
*
*/
2018-07-01 11:31:02 -05:00
public function testUpload(): void
{
/** @var Attachment $attachment */
$attachment = $this->user()->attachments()->first();
2018-07-01 11:58:41 -05:00
$content = 'Hello there';
2018-07-01 11:31:02 -05:00
// mock helper:
2018-07-01 11:58:41 -05:00
$helper = $this->mock(AttachmentHelperInterface::class);
2018-07-01 11:31:02 -05:00
$helper->shouldReceive('saveAttachmentFromApi')->once();
2018-07-01 11:58:41 -05:00
$response = $this->call('POST', '/api/v1/attachments/' . $attachment->id . '/upload', [], [], [], [], $content);
2018-07-01 11:31:02 -05:00
//$response = $this->post('/api/v1/attachments/' . $attachment->id . '/upload',$content, ['Accept' => 'application/json']);
$response->assertStatus(204);
}
2018-07-22 13:33:17 -05:00
}