Files
firefly-iii/tests/TestCase.php

157 lines
3.7 KiB
PHP
Raw Normal View History

2015-07-02 09:44:56 +02:00
<?php
2017-08-11 05:36:05 +02:00
/**
* TestCase.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* 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
2017-12-17 14:42:21 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-08-11 05:36:05 +02:00
*/
2017-08-18 21:08:51 +02:00
declare(strict_types=1);
2017-02-05 15:42:00 +01:00
namespace Tests;
2017-02-12 12:21:44 +01:00
use Carbon\Carbon;
2018-02-28 15:50:00 +01:00
use Exception;
2017-02-12 12:21:44 +01:00
use FireflyIII\Models\Preference;
2018-02-28 15:50:00 +01:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\User;
2017-02-05 15:42:00 +01:00
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Log;
use Mockery;
2017-02-12 12:21:44 +01:00
2016-10-09 07:58:27 +02:00
/**
* Class TestCase
2017-02-05 15:42:00 +01:00
*
2017-08-12 10:27:45 +02:00
* @SuppressWarnings(PHPMD.NumberOfChildren)
2016-10-09 07:58:27 +02:00
*/
2017-02-05 15:42:00 +01:00
abstract class TestCase extends BaseTestCase
2015-07-02 09:44:56 +02:00
{
2018-03-03 08:12:18 +01:00
/**
* @param User $user
* @param string $range
*/
2018-05-11 19:58:10 +02:00
public function changeDateRange(User $user, $range): void
{
$valid = ['1D', '1W', '1M', '3M', '6M', '1Y', 'custom'];
if (\in_array($range, $valid)) {
2018-02-28 15:50:00 +01:00
try {
Preference::where('user_id', $user->id)->where('name', 'viewRange')->delete();
} catch (Exception $e) {
// don't care.
2018-05-11 19:58:10 +02:00
$e->getMessage();
2018-02-28 15:50:00 +01:00
}
Preference::create(
[
'user_id' => $user->id,
'name' => 'viewRange',
'data' => $range,
]
);
// set period to match?
}
2017-11-22 21:12:27 +01:00
if ('custom' === $range) {
$this->session(
[
'start' => Carbon::now()->subDays(20),
'end' => Carbon::now(),
]
);
}
}
2018-02-28 15:50:00 +01:00
use CreatesApplication;
/**
* @return array
*/
2018-05-11 19:58:10 +02:00
public function dateRangeProvider(): array
{
return [
'one day' => ['1D'],
'one week' => ['1W'],
'one month' => ['1M'],
'three months' => ['3M'],
'six months' => ['6M'],
'one year' => ['1Y'],
'custom range' => ['custom'],
];
}
2017-02-12 12:21:44 +01:00
/**
* @return User
*/
2017-12-26 17:33:53 +01:00
public function demoUser(): User
{
2018-04-02 14:17:11 +02:00
return User::find(4);
2017-12-26 17:33:53 +01:00
}
/**
* @return User
*/
public function emptyUser(): User
2017-02-12 12:21:44 +01:00
{
2018-04-02 14:17:11 +02:00
return User::find(2);
2017-02-12 12:21:44 +01:00
}
/**
* @return User
*/
2017-12-26 17:33:53 +01:00
public function user(): User
2017-02-12 12:21:44 +01:00
{
2018-04-02 14:17:11 +02:00
return User::find(1);
2017-02-12 12:21:44 +01:00
}
/**
* @param string $class
*
* @return \Mockery\MockInterface
*/
2018-05-11 19:58:10 +02:00
protected function mock($class): \Mockery\MockInterface
2017-02-12 12:21:44 +01:00
{
Log::debug(sprintf('Will now mock %s', $class));
$object = Mockery::mock($class);
$this->app->instance($class, $object);
return $object;
}
2018-02-16 22:14:34 +01:00
/**
* @param string $class
*
* @return Mockery\MockInterface
*/
2018-05-11 19:58:10 +02:00
protected function overload(string $class): \Mockery\MockInterface
2018-02-16 22:14:34 +01:00
{
//$this->app->instance($class, $externalMock);
2018-04-02 14:17:11 +02:00
return Mockery::mock('overload:' . $class);
2018-02-16 22:14:34 +01:00
}
2018-02-28 15:50:00 +01:00
/**
*
*/
protected function setUp()
{
parent::setUp();
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
}
}