firefly-iii/tests/TestCase.php

174 lines
3.8 KiB
PHP
Raw Normal View History

2015-07-02 02:44:56 -05:00
<?php
2016-01-16 02:15:24 -06:00
use Carbon\Carbon;
use FireflyIII\Models\Preference;
2016-01-17 00:18:35 -06:00
use FireflyIII\User;
2015-07-02 02:44:56 -05:00
2016-01-16 00:14:36 -06:00
/**
* Class TestCase
*/
2016-02-07 00:56:58 -06:00
2016-02-10 05:01:45 -06:00
2015-07-02 02:44:56 -05:00
class TestCase extends Illuminate\Foundation\Testing\TestCase
{
/**
* The base URL to use while testing the application.
*
* @var string
*/
2015-07-02 02:44:56 -05:00
protected $baseUrl = 'http://localhost';
/**
* @param User $user
* @param string $range
*/
public function changeDateRange(User $user, $range)
{
$valid = ['1D', '1W', '1M', '3M', '6M', '1Y', 'custom'];
if (in_array($range, $valid)) {
Preference::where('user_id', $user->id)->where('name', 'viewRange')->delete();
Preference::create(
[
'user_id' => $user->id,
'name' => 'viewRange',
'data' => $range,
]
);
2016-02-05 08:01:33 -06:00
// set period to match?
}
2016-02-04 23:37:08 -06:00
// if selected "custom", change the session to a weird custom range:
// (20 days):
2016-02-05 08:01:33 -06:00
if ($range === "custom") {
2016-02-04 23:37:08 -06:00
$this->session(
[
'start' => Carbon::now(),
2016-02-05 08:01:33 -06:00
'end' => Carbon::now()->subDays(20),
]
2016-02-04 23:37:08 -06:00
);
}
}
2015-07-02 02:44:56 -05:00
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
2016-01-16 02:15:24 -06:00
$app = require __DIR__ . '/../bootstrap/app.php';
2015-07-02 02:44:56 -05:00
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
2015-07-02 02:44:56 -05:00
return $app;
}
2016-01-16 02:15:24 -06:00
2016-01-17 00:18:35 -06:00
/**
* @return array
2016-01-17 00:18:35 -06:00
*/
public function dateRangeProvider()
2016-01-17 00:18:35 -06:00
{
return [
'one day' => ['1D'],
'one week' => ['1W'],
'one month' => ['1M'],
'three months' => ['3M'],
'six months' => ['6M'],
'one year' => ['1Y'],
'custom range' => ['custom'],
];
2016-01-17 00:18:35 -06:00
}
2016-02-04 00:30:48 -06:00
/**
* @return User
*/
public function emptyUser()
2016-02-04 00:30:48 -06:00
{
return User::find(2);
2016-02-04 00:30:48 -06:00
}
2016-01-16 02:15:24 -06:00
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
public function setUp()
{
parent::setUp();
// if the database copy does not exist, call migrate.
$copy = storage_path('database') . '/testing-copy.db';
$original = storage_path('database') . '/testing.db';
// move .env file over?
if (!file_exists($copy)) {
2016-01-17 00:30:14 -06:00
// maybe original does?
if (!file_exists($original)) {
touch($original);
Artisan::call('migrate', ['--seed' => true]);
}
2016-01-22 23:59:22 -06:00
2016-01-16 02:15:24 -06:00
copy($original, $copy);
} else {
if (file_exists($copy)) {
copy($copy, $original);
}
}
// if the database copy does exists, copy back as original.
2016-01-16 02:15:24 -06:00
$this->session(
[
'start' => Carbon::now()->startOfMonth(),
'end' => Carbon::now()->endOfMonth(),
'first' => Carbon::now()->startOfYear(),
]
);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
public function tearDown()
{
parent::tearDown();
}
/**
* @return User
*/
public function toBeDeletedUser()
{
return User::find(3);
}
2016-01-22 23:59:22 -06:00
/**
* @return User
*/
public function user()
{
$user = User::find(1);
return $user;
2016-01-22 23:59:22 -06:00
}
/**
* @param string $class
*
* @return \Mockery\MockInterface
*/
protected function mock($class)
{
$object = Mockery::mock($class);
$this->app->instance($class, $object);
return $object;
}
2016-01-16 02:15:24 -06:00
2015-07-02 02:44:56 -05:00
}