Files
firefly-iii/tests/Feature/Controllers/System/CronControllerTest.php

122 lines
4.3 KiB
PHP
Raw Normal View History

2018-09-02 20:13:25 +02:00
<?php
/**
* CronControllerTest.php
2020-02-16 13:59:55 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-09-02 20:13:25 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-09-02 20:13:25 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-09-02 20:13:25 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-09-02 20:13:25 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-09-02 20:13:25 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-09-02 20:13:25 +02:00
*/
declare(strict_types=1);
namespace Tests\Feature\Controllers\System;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Preference;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\Support\Cronjobs\RecurringCronjob;
use Illuminate\Support\Collection;
use Log;
use Mockery;
2019-07-25 14:19:49 +02:00
use Preferences;
2018-09-02 20:13:25 +02:00
use Tests\TestCase;
/**
*
* Class CronControllerTest
2019-08-17 10:48:28 +02:00
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2018-09-02 20:13:25 +02:00
*/
class CronControllerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
2019-04-09 20:05:20 +02:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-09-02 20:13:25 +02:00
}
/**
* @covers \FireflyIII\Http\Controllers\System\CronController
* @covers \FireflyIII\Support\Binder\CLIToken
*/
public function testCron(): void
{
$users = new Collection([$this->user()]);
$job = $this->mock(RecurringCronjob::class);
$preference = new Preference();
$preference->data = 'token';
$job->shouldReceive('fire')->once()->andReturn(true);
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('all')->andReturn($users);
2019-07-25 14:19:49 +02:00
Preferences::shouldReceive('getForUser')
2018-09-02 20:13:25 +02:00
->withArgs([Mockery::any(), 'access_token', null])
->andReturn($preference)->once();
$response = $this->get(route('cron.cron', ['token']));
$response->assertStatus(200);
$response->assertSee('The recurring transaction cron job fired successfully.');
}
/**
* @covers \FireflyIII\Http\Controllers\System\CronController
* @covers \FireflyIII\Support\Binder\CLIToken
*/
public function testCronException(): void
{
$users = new Collection([$this->user()]);
$job = $this->mock(RecurringCronjob::class);
$preference = new Preference();
$preference->data = 'token';
$job->shouldReceive('fire')->once()->andThrow(new FireflyException('Exception noted.'));
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('all')->andReturn($users);
2019-07-25 14:19:49 +02:00
Preferences::shouldReceive('getForUser')
2018-09-02 20:13:25 +02:00
->withArgs([Mockery::any(), 'access_token', null])
->andReturn($preference)->once();
$response = $this->get(route('cron.cron', ['token']));
$response->assertStatus(200);
$response->assertSee('Exception noted.');
}
/**
* @covers \FireflyIII\Http\Controllers\System\CronController
* @covers \FireflyIII\Support\Binder\CLIToken
*/
public function testCronFalse(): void
{
$users = new Collection([$this->user()]);
$job = $this->mock(RecurringCronjob::class);
$preference = new Preference();
$preference->data = 'token';
$job->shouldReceive('fire')->once()->andReturn(false);
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('all')->andReturn($users);
2019-07-25 14:19:49 +02:00
Preferences::shouldReceive('getForUser')
2018-09-02 20:13:25 +02:00
->withArgs([Mockery::any(), 'access_token', null])
->andReturn($preference)->once();
$response = $this->get(route('cron.cron', ['token']));
$response->assertStatus(200);
$response->assertSee('The recurring transaction cron job did not fire.');
}
2018-12-31 07:48:23 +01:00
}