. */ declare(strict_types=1); namespace Tests\Unit\Console\Commands\Upgrade; use FireflyConfig; use FireflyIII\Models\Attachment; use FireflyIII\Models\Configuration; use FireflyIII\Models\Note; use FireflyIII\Models\TransactionJournal; use Log; use Tests\TestCase; /** * Class MigrateAttachmentsTest * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @SuppressWarnings(PHPMD.TooManyPublicMethods) */ class MigrateAttachmentsTest extends TestCase { /** * */ public function setUp(): void { parent::setUp(); Log::info(sprintf('Now in %s.', get_class($this))); } /** * @covers \FireflyIII\Console\Commands\Upgrade\MigrateAttachments */ public function testHandle(): void { $false = new Configuration; $false->data = false; FireflyConfig::shouldReceive('get')->withArgs(['480_migrate_attachments', false])->andReturn($false); FireflyConfig::shouldReceive('set')->withArgs(['480_migrate_attachments', true]); // assume all is well. $this->artisan('firefly-iii:migrate-attachments') ->expectsOutput('All attachments are OK.') ->assertExitCode(0); } /** * @covers \FireflyIII\Console\Commands\Upgrade\MigrateAttachments */ public function testHandleMigrate(): void { $false = new Configuration; $false->data = false; FireflyConfig::shouldReceive('get')->withArgs(['480_migrate_attachments', false])->andReturn($false); FireflyConfig::shouldReceive('set')->withArgs(['480_migrate_attachments', true]); $attachment = Attachment::create( [ 'user_id' => 1, 'attachable_id' => 1, 'attachable_type' => TransactionJournal::class, 'description' => 'Hello', 'md5' => md5('hello'), 'filename' => 'test.pdf', 'mime' => 'text/plain', 'size' => 1, ]); // assume all is well. $this->artisan('firefly-iii:migrate-attachments') ->expectsOutput('Updated 1 attachment(s).') ->assertExitCode(0); $this->assertCount(0, Attachment::where('id', $attachment->id)->where('description', '!=', '')->get()); $this->assertCount(1, Attachment::where('id', $attachment->id)->where('description', '=', '')->get()); $this->assertCount(1, Note::where('noteable_id', $attachment->id)->where('noteable_type', Attachment::class)->get()); $attachment->forceDelete(); } }