Files
firefly-iii/tests/Unit/Support/Import/Routine/File/CurrencyMapperTest.php

182 lines
6.2 KiB
PHP
Raw Normal View History

2018-05-12 10:46:18 +02:00
<?php
/**
* CurrencyMapperTest.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\Unit\Support\Import\Routine\File;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Import\Routine\File\CurrencyMapper;
use Log;
2019-01-27 07:48:57 +01:00
use Tests\TestCase;
2018-05-12 10:46:18 +02:00
/**
* Class CurrencyMapperTest
*/
class CurrencyMapperTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', \get_class($this)));
}
2018-05-12 10:46:18 +02:00
/**
* @covers \FireflyIII\Support\Import\Routine\File\CurrencyMapper
*/
public function testBasic(): void
{
$currency = TransactionCurrency::inRandomOrder()->first();
// mock data
$repository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('findNull')->once()->withArgs([$currency->id])->andReturn($currency);
$mapper = new CurrencyMapper();
$mapper->setUser($this->user());
$result = $mapper->map($currency->id, []);
$this->assertEquals($currency->id, $result->id);
}
/**
* @covers \FireflyIII\Support\Import\Routine\File\CurrencyMapper
*/
public function testBasicNotFound(): void
{
$currency = TransactionCurrency::inRandomOrder()->first();
// mock data
$repository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('findNull')->once()->withArgs([$currency->id])->andReturn(null);
2018-07-01 09:27:22 +02:00
2018-05-12 10:46:18 +02:00
$mapper = new CurrencyMapper();
$mapper->setUser($this->user());
$result = $mapper->map($currency->id, []);
$this->assertNull($result);
}
/**
* @covers \FireflyIII\Support\Import\Routine\File\CurrencyMapper
*/
2018-08-06 19:14:30 +02:00
public function testEmpty(): void
2018-05-12 10:46:18 +02:00
{
2018-08-06 19:14:30 +02:00
2018-05-12 10:46:18 +02:00
// mock data
$repository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
$mapper = new CurrencyMapper();
$mapper->setUser($this->user());
2018-08-06 19:14:30 +02:00
$result = $mapper->map(null, []);
$this->assertNull($result);
2018-05-12 10:46:18 +02:00
}
/**
* @covers \FireflyIII\Support\Import\Routine\File\CurrencyMapper
*/
2018-08-06 19:14:30 +02:00
public function testFindAndCreate(): void
2018-05-12 10:46:18 +02:00
{
$currency = TransactionCurrency::inRandomOrder()->first();
// mock data
$repository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
2018-08-06 19:14:30 +02:00
$repository->shouldReceive('findBySymbolNull')->withArgs([$currency->symbol])->andReturn(null)->once();
$repository->shouldReceive('findByCodeNull')->withArgs([$currency->code])->andReturn(null)->once();
$repository->shouldReceive('findByNameNull')->withArgs([$currency->name])->andReturn(null)->once();
// nothing found, mapper will try to create it.
$repository->shouldReceive('store')
2019-01-27 07:48:57 +01:00
->withArgs([['code' => $currency->code, 'name' => $currency->name, 'symbol' => $currency->symbol, 'enabled' => true, 'decimal_places' => 2]])
2018-08-06 19:14:30 +02:00
->once()->andReturn($currency);
2018-05-12 10:46:18 +02:00
$mapper = new CurrencyMapper();
$mapper->setUser($this->user());
2019-01-27 07:48:57 +01:00
$result = $mapper->map(null, ['name' => $currency->name, 'code' => $currency->code, 'enabled' => true, 'symbol' => $currency->symbol]);
2018-05-12 10:46:18 +02:00
$this->assertEquals($currency->id, $result->id);
}
/**
* @covers \FireflyIII\Support\Import\Routine\File\CurrencyMapper
*/
2018-08-06 19:14:30 +02:00
public function testFindByCode(): void
2018-05-12 10:46:18 +02:00
{
$currency = TransactionCurrency::inRandomOrder()->first();
// mock data
$repository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
2018-08-06 19:14:30 +02:00
$repository->shouldReceive('findByCodeNull')->withArgs([$currency->code])
2018-05-12 10:46:18 +02:00
->andReturn($currency)->once();
$mapper = new CurrencyMapper();
$mapper->setUser($this->user());
2018-08-06 19:14:30 +02:00
$result = $mapper->map(null, ['code' => $currency->code]);
2018-05-12 10:46:18 +02:00
$this->assertEquals($currency->id, $result->id);
}
/**
* @covers \FireflyIII\Support\Import\Routine\File\CurrencyMapper
*/
2018-08-06 19:14:30 +02:00
public function testFindByName(): void
2018-05-12 10:46:18 +02:00
{
$currency = TransactionCurrency::inRandomOrder()->first();
// mock data
$repository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
2018-08-06 19:14:30 +02:00
$repository->shouldReceive('findByNameNull')->withArgs([$currency->name])
->andReturn($currency)->once();
2018-05-12 10:46:18 +02:00
$mapper = new CurrencyMapper();
$mapper->setUser($this->user());
2018-08-06 19:14:30 +02:00
$result = $mapper->map(null, ['name' => $currency->name]);
2018-05-12 10:46:18 +02:00
$this->assertEquals($currency->id, $result->id);
}
/**
* @covers \FireflyIII\Support\Import\Routine\File\CurrencyMapper
*/
2018-08-06 19:14:30 +02:00
public function testFindBySymbol(): void
2018-05-12 10:46:18 +02:00
{
2018-08-06 19:14:30 +02:00
$currency = TransactionCurrency::inRandomOrder()->first();
2018-05-12 10:46:18 +02:00
// mock data
$repository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
2018-08-06 19:14:30 +02:00
$repository->shouldReceive('findBySymbolNull')->withArgs([$currency->symbol])
->andReturn($currency)->once();
2018-05-12 10:46:18 +02:00
$mapper = new CurrencyMapper();
$mapper->setUser($this->user());
2018-08-06 19:14:30 +02:00
$result = $mapper->map(null, ['symbol' => $currency->symbol]);
$this->assertEquals($currency->id, $result->id);
2018-05-12 10:46:18 +02:00
}
2018-07-22 20:33:17 +02:00
}