PingExtensionTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 70
rs 10
c 0
b 0
f 0
eloc 38

6 Methods

Rating   Name   Duplication   Size   Complexity  
A failure_webhooks_are_pinged() 0 16 1
A hp$0 ➔ createBuilder() 0 24 1
A hp$0 ➔ buildSchedule() 0 14 1
createBuilder() 0 24 ?
A hp$0 ➔ __construct() 0 3 1
A success_webhooks_are_pinged() 0 16 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Tests\Schedule\Extension;
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 Symfony\Contracts\HttpClient\HttpClientInterface;
7
use Zenstruck\ScheduleBundle\Schedule;
8
use Zenstruck\ScheduleBundle\Schedule\Extension\Handler\PingHandler;
9
use Zenstruck\ScheduleBundle\Schedule\ScheduleBuilder;
10
use Zenstruck\ScheduleBundle\Schedule\Task;
11
use Zenstruck\ScheduleBundle\Tests\Fixture\MockScheduleBuilder;
12
use Zenstruck\ScheduleBundle\Tests\Fixture\MockTask;
13
14
/**
15
 * @author Kevin Bond <[email protected]>
16
 */
17
final class PingExtensionTest extends TestCase
18
{
19
    /**
20
     * @test
21
     */
22
    public function success_webhooks_are_pinged()
23
    {
24
        $client = $this->createMock(HttpClientInterface::class);
25
        $client->expects($this->exactly(6))->method('request')->withConsecutive(
26
            [$this->equalTo('GET'), $this->equalTo('schedule-before.com'), $this->isType('array')],
27
            [$this->equalTo('GET'), $this->equalTo('task-before.com'), $this->isType('array')],
28
            [$this->equalTo('GET'), $this->equalTo('task-after.com'), $this->isType('array')],
29
            [$this->equalTo('GET'), $this->equalTo('task-success.com'), $this->isType('array')],
30
            [$this->equalTo('GET'), $this->equalTo('schedule-after.com'), $this->isType('array')],
31
            [$this->equalTo('GET'), $this->equalTo('schedule-success.com'), $this->isType('array')]
32
        );
33
34
        (new MockScheduleBuilder())
35
            ->addHandler(new PingHandler($client))
36
            ->addBuilder($this->createBuilder(MockTask::success()))
37
            ->run()
38
        ;
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function failure_webhooks_are_pinged()
45
    {
46
        $client = $this->createMock(HttpClientInterface::class);
47
        $client->expects($this->exactly(6))->method('request')->withConsecutive(
48
            [$this->equalTo('GET'), $this->equalTo('schedule-before.com'), $this->isType('array')],
49
            [$this->equalTo('GET'), $this->equalTo('task-before.com'), $this->isType('array')],
50
            [$this->equalTo('GET'), $this->equalTo('task-after.com'), $this->isType('array')],
51
            [$this->equalTo('GET'), $this->equalTo('task-failure.com'), $this->isType('array')],
52
            [$this->equalTo('GET'), $this->equalTo('schedule-after.com'), $this->isType('array')],
53
            [$this->equalTo('GET'), $this->equalTo('schedule-failure.com'), $this->isType('array')]
54
        );
55
56
        (new MockScheduleBuilder())
57
            ->addHandler(new PingHandler($client))
58
            ->addBuilder($this->createBuilder(MockTask::exception(new \Exception())))
59
            ->run()
60
        ;
61
    }
62
63
    private function createBuilder(Task $task): ScheduleBuilder
64
    {
65
        return new class($task) implements ScheduleBuilder {
66
            private $task;
67
68
            public function __construct(Task $task)
69
            {
70
                $this->task = $task;
71
            }
72
73
            public function buildSchedule(Schedule $schedule): void
74
            {
75
                $schedule
76
                    ->pingBefore('schedule-before.com')
77
                    ->pingAfter('schedule-after.com')
78
                    ->pingOnSuccess('schedule-success.com')
79
                    ->pingOnFailure('schedule-failure.com')
80
                ;
81
82
                $schedule->add($this->task)
83
                    ->pingBefore('task-before.com')
84
                    ->pingAfter('task-after.com')
85
                    ->pingOnSuccess('task-success.com')
86
                    ->pingOnFailure('task-failure.com')
87
                ;
88
            }
89
        };
90
    }
91
}
92