CsvRendererTest::createUrlInfo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 2
b 0
f 0
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
class CsvRendererTest extends \PHPUnit_Framework_TestCase
29
{
30
    /**
31
     * @var BufferedIO
32
     */
33
    private $io;
34
35
    protected function setUp()
36
    {
37
        $this->io = new BufferedIO();
38
    }
39
40
    /**
41
     * testCsvRendererError.
42
     */
43
    public function testCsvRendererError()
44
    {
45
        $renderer = RendererFactory::create('csv', $this->io);
46
        $renderer->render($this->createResultCollection(true));
47
48
        $output = <<<CSV
49
"Global Status","Status Code",Name,"Reponse Time","Http Error Log","Validator Error Log"
50
FAIL,400,Example,0.42,"Couldn't resolve proxy name",
51
52
CSV;
53
        $this->assertInstanceOf(RendererInterface::class, $renderer);
54
        $this->assertSame($output, $this->io->fetchOutput());
55
    }
56
57
    /**
58
     * testCsvRenderer.
59
     */
60
    public function testCsvRenderer()
61
    {
62
        $renderer = RendererFactory::create('csv', $this->io);
63
        $renderer->render($this->createResultCollection());
64
65
        $output = <<<CSV
66
"Global Status","Status Code",Name,"Reponse Time","Http Error Log","Validator Error Log"
67
OK,200,Example,0.42,,
68
69
CSV;
70
71
        $this->assertInstanceOf(RendererInterface::class, $renderer);
72
        $this->assertSame($output, $this->io->fetchOutput());
73
    }
74
75
    /**
76
     * createResultCollection.
77
     *
78
     * @param bool $hasError
79
     */
80
    public function createResultCollection($hasError = false)
81
    {
82
        $errorLog = null;
83
        if ($hasError) {
84
            $errorLog = curl_strerror(5);
85
        }
86
        $result = new Result($this->createUrlInfo(), $hasError ? 400 : 200, 0.42, $errorLog);
87
88
        $resultCollection = new ResultCollection();
89
        $resultCollection->append($result);
90
91
        return $resultCollection;
92
    }
93
94
    private function createUrlInfo()
95
    {
96
        return new UrlInfo(
97
            'Example',
98
            'http://example.com',
99
            'GET',
100
            [],
101
            1,
102
            200,
103
            (new Validator()),
104
            null,
105
            null
106
        );
107
    }
108
}
109