TableRendererTest::createResultCollection()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
cc 4
eloc 10
nc 2
nop 1
dl 17
loc 17
rs 9.2
c 4
b 0
f 2
1
<?php
2
3
/*
4
 * This file is part of the hogosha-monitor package
5
 *
6
 * Copyright (c) 2016 Guillaume Cavana
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Guillaume Cavana <[email protected]>
14
 */
15
16
namespace Hogosha\Monitor\Renderer;
17
18
use Hogosha\Monitor\Model\Result;
19
use Hogosha\Monitor\Model\ResultCollection;
20
use Hogosha\Monitor\Model\UrlInfo;
21
use Hogosha\Monitor\Validator\Validator;
22
use Webmozart\Console\Api\IO\IO;
23
use Webmozart\Console\IO\BufferedIO;
24
25
/**
26
 * @author Guillaume Cavana <[email protected]>
27
 */
28 View Code Duplication
class TableRendererTest extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
{
30
    /**
31
     * @var BufferedIO
32
     */
33
    private $io;
34
35
    protected function setUp()
36
    {
37
        $this->io = new BufferedIO();
38
    }
39
40
    /**
41
     * testTableRendererError.
42
     */
43
    public function testTableRendererError()
44
    {
45
        $renderer = RendererFactory::create('table', $this->io);
46
        $renderer->render($this->createResultCollection(true));
47
48
        $output = <<<TABLE
49
+--------+--------+---------+----------+--------------------+------------------+
50
| Global | Status | Name    | Response | Http Error Log     | Validator Error  |
51
| Status | Code   |         | Time     |                    | Log              |
52
+--------+--------+---------+----------+--------------------+------------------+
53
| FAIL   | 400    | Example | 0.42     | Couldn't           | Validation Error |
54
|        |        |         |          | resolve proxy name |                  |
55
+--------+--------+---------+----------+--------------------+------------------+
56
57
TABLE;
58
        $this->assertInstanceOf(RendererInterface::class, $renderer);
59
        $this->assertSame($output, $this->io->fetchOutput());
60
    }
61
62
    /**
63
     * testTableRenderer.
64
     */
65
    public function testTableRenderer()
66
    {
67
        $renderer = RendererFactory::create('table', $this->io);
68
        $renderer->render($this->createResultCollection());
69
70
        $output = <<<TABLE
71
+--------+--------+---------+----------+----------------+---------------------+
72
| Global | Status | Name    | Response | Http Error Log | Validator Error Log |
73
| Status | Code   |         | Time     |                |                     |
74
+--------+--------+---------+----------+----------------+---------------------+
75
| OK     | 200    | Example | 0.42     |                |                     |
76
+--------+--------+---------+----------+----------------+---------------------+
77
78
TABLE;
79
        $this->assertInstanceOf(RendererInterface::class, $renderer);
80
        $this->assertSame($output, $this->io->fetchOutput());
81
    }
82
83
    /**
84
     * createResultCollection.
85
     *
86
     * @param bool $hasError
87
     */
88
    public function createResultCollection($hasError = false)
89
    {
90
        $errorLog = null;
91
        $validationErrorLog = null;
92
93
        if ($hasError) {
94
            $errorLog = curl_strerror(5);
95
            $validationErrorLog = 'Validation Error';
96
        }
97
98
        $result = new Result($this->createUrlInfo(), $hasError ? 400 : 200, 0.42, $errorLog, $hasError ? false : true, $validationErrorLog);
99
100
        $resultCollection = new ResultCollection();
101
        $resultCollection->append($result);
102
103
        return $resultCollection;
104
    }
105
106
    private function createUrlInfo()
107
    {
108
        return new UrlInfo(
109
            'Example',
110
            'http://example.com',
111
            'GET',
112
            [],
113
            1,
114
            200,
115
            (new Validator()),
116
            null,
117
            null
118
        );
119
    }
120
}
121