Passed
Push — master ( 18c542...46352d )
by Kevin
02:34
created

SelfHandlingExtensionTest.php$0 ➔ createBuilder()   A

Complexity

Conditions 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
rs 9.472
cc 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A SelfHandlingExtensionTest.php$0 ➔ __construct() 0 3 1
A SelfHandlingExtensionTest.php$0 ➔ buildSchedule() 0 16 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Tests\Functional;
4
5
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Zenstruck\ScheduleBundle\Schedule;
7
use Zenstruck\ScheduleBundle\Schedule\ScheduleBuilder;
8
use Zenstruck\ScheduleBundle\Schedule\Task;
9
use Zenstruck\ScheduleBundle\Tests\Fixture\MockScheduleBuilder;
10
use Zenstruck\ScheduleBundle\Tests\Fixture\MockTask;
11
12
/**
13
 * @author Kevin Bond <[email protected]>
14
 */
15
final class SelfHandlingExtensionTest extends TestCase
16
{
17
    /**
18
     * @test
19
     */
20
    public function success_hooks_are_called()
21
    {
22
        $builder = $this->createBuilder(MockTask::success());
23
24
        (new MockScheduleBuilder())
25
            ->addBuilder($builder)
26
            ->run()
27
        ;
28
29
        $this->assertSame([
30
            'scheduleFilter',
31
            'scheduleBefore',
32
            'taskFilter',
33
            'taskBefore',
34
            'taskAfter',
35
            'taskSuccess',
36
            'scheduleAfter',
37
            'scheduleSuccess',
38
        ], $builder->calls);
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function failure_hooks_are_called()
45
    {
46
        $builder = $this->createBuilder(MockTask::exception(new \Exception()));
47
48
        (new MockScheduleBuilder())
49
            ->addBuilder($builder)
50
            ->run()
51
        ;
52
53
        $this->assertSame([
54
            'scheduleFilter',
55
            'scheduleBefore',
56
            'taskFilter',
57
            'taskBefore',
58
            'taskAfter',
59
            'taskFailure',
60
            'scheduleAfter',
61
            'scheduleFailure',
62
        ], $builder->calls);
63
    }
64
65
    private function createBuilder(callable $callback): ScheduleBuilder
66
    {
67
        return new class($callback) implements ScheduleBuilder {
0 ignored issues
show
Bug introduced by
$callback of type callable is incompatible with the type Zenstruck\ScheduleBundle\Schedule\Task expected by parameter $task of anonymous//tests/Functio...st.php$0::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        return new class(/** @scrutinizer ignore-type */ $callback) implements ScheduleBuilder {
Loading history...
68
            public $calls = [];
69
70
            private $task;
71
72
            public function __construct(Task $task)
73
            {
74
                $this->task = $task;
75
            }
76
77
            public function buildSchedule(Schedule $schedule): void
78
            {
79
                $schedule
80
                    ->filter(function () { $this->calls[] = 'scheduleFilter'; })
81
                    ->before(function () { $this->calls[] = 'scheduleBefore'; })
82
                    ->after(function () { $this->calls[] = 'scheduleAfter'; })
83
                    ->onSuccess(function () { $this->calls[] = 'scheduleSuccess'; })
84
                    ->onFailure(function () { $this->calls[] = 'scheduleFailure'; })
85
                ;
86
87
                $schedule->add($this->task)
88
                    ->filter(function () { $this->calls[] = 'taskFilter'; })
89
                    ->before(function () { $this->calls[] = 'taskBefore'; })
90
                    ->after(function () { $this->calls[] = 'taskAfter'; })
91
                    ->onSuccess(function () { $this->calls[] = 'taskSuccess'; })
92
                    ->onFailure(function () { $this->calls[] = 'taskFailure'; })
93
                ;
94
            }
95
        };
96
    }
97
}
98