Passed
Pull Request — master (#8)
by Kevin
15:51
created

PingTaskRunnerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
eloc 40
c 1
b 0
f 1
dl 0
loc 79
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A creates_failure_on_500() 0 20 1
A creates_failure_on_404() 0 20 1
A createBuilder() 0 4 1
A creates_successful_result() 0 18 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Tests\Schedule\Task\Runner;
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\Component\HttpClient\Exception\ClientException;
7
use Symfony\Component\HttpClient\Exception\ServerException;
8
use Symfony\Component\HttpClient\MockHttpClient;
9
use Symfony\Component\HttpClient\Response\MockResponse;
10
use Zenstruck\ScheduleBundle\Schedule\Task\PingTask;
11
use Zenstruck\ScheduleBundle\Schedule\Task\Runner\PingTaskRunner;
12
use Zenstruck\ScheduleBundle\Tests\Fixture\MockScheduleBuilder;
13
14
/**
15
 * @author Kevin Bond <[email protected]>
16
 */
17
final class PingTaskRunnerTest extends TestCase
18
{
19
    /**
20
     * @test
21
     */
22
    public function creates_successful_result()
23
    {
24
        $context = self::createBuilder(
25
            new MockHttpClient(
26
                new MockResponse('response body', [
27
                    'response_headers' => [
28
                        'HTTP/1.1 200 OK',
29
                        'Server: Foo',
30
                    ],
31
                ])
32
            ))
33
            ->addTask(new PingTask('https://example.com'))
34
            ->run()
35
        ;
36
37
        $this->assertTrue($context->isSuccessful());
38
        $this->assertCount(1, $run = $context->getSuccessful());
39
        $this->assertSame("HTTP/1.1 200 OK\nServer: Foo\n\nresponse body", $run[0]->getOutput());
40
    }
41
42
    /**
43
     * @test
44
     */
45
    public function creates_failure_on_404()
46
    {
47
        $context = self::createBuilder(
48
            new MockHttpClient(
49
                new MockResponse('response body', [
50
                    'response_headers' => [
51
                        'HTTP/1.1 404 Not Found',
52
                        'Server: Foo',
53
                    ],
54
                ])
55
            ))
56
            ->addTask(new PingTask('https://example.com'))
57
            ->run()
58
        ;
59
60
        $this->assertFalse($context->isSuccessful());
61
        $this->assertTrue($context->isFailure());
62
        $this->assertCount(1, $run = $context->getFailures());
63
        $this->assertInstanceOf(ClientException::class, $run[0]->getException());
64
        $this->assertSame('ClientException: HTTP/1.1 404 Not Found returned for "https://example.com/".', $run[0]->getDescription());
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function creates_failure_on_500()
71
    {
72
        $context = self::createBuilder(
73
            new MockHttpClient(
74
                new MockResponse('response body', [
75
                    'response_headers' => [
76
                        'HTTP/1.1 500 Internal Server Error',
77
                        'Server: Foo',
78
                    ],
79
                ])
80
            ))
81
            ->addTask(new PingTask('https://example.com'))
82
            ->run()
83
        ;
84
85
        $this->assertFalse($context->isSuccessful());
86
        $this->assertTrue($context->isFailure());
87
        $this->assertCount(1, $run = $context->getFailures());
88
        $this->assertInstanceOf(ServerException::class, $run[0]->getException());
89
        $this->assertSame('ServerException: HTTP/1.1 500 Internal Server Error returned for "https://example.com/".', $run[0]->getDescription());
90
    }
91
92
    private static function createBuilder(MockHttpClient $httpClient): MockScheduleBuilder
93
    {
94
        return (new MockScheduleBuilder())
95
            ->addRunner(new PingTaskRunner($httpClient))
96
        ;
97
    }
98
}
99