RendererTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 129
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A tearDown() 0 4 1
A testGetFormats() 0 15 3
A test__call() 0 10 2
A testRenderOneFilter() 0 24 1
A testRenderChainedFilters() 0 34 1
1
<?php
2
3
/**
4
 * @file
5
 * Grafizzi\Graph\Tests\RendererTest: a component of the Grafizzi library.
6
 *
7
 * (c) 2012 Frédéric G. MARAND <[email protected]>
8
 *
9
 * Grafizzi is free software: you can redistribute it and/or modify it under the
10
 * terms of the GNU Lesser General Public License as published by the Free
11
 * Software Foundation, either version 3 of the License, or (at your option) any
12
 * later version.
13
 *
14
 * Grafizzi is distributed in the hope that it will be useful, but WITHOUT ANY
15
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16
 * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
17
 * details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with Grafizzi, in the COPYING.LESSER.txt file.  If not, see
21
 * <http://www.gnu.org/licenses/>
22
 */
23
24
namespace Grafizzi\Graph\Tests;
25
26
require 'vendor/autoload.php';
27
28
use Grafizzi\Graph\Renderer;
29
use \ErrorException;
30
31
/**
32
 * Renderer test case.
33
 *
34
 * Note: at least in PHP3.7, when passing, expectOutputString is not
35
 * included in the assertions count, but it is included when failing.
36
 */
37
class RendererTest extends BaseGraphTest {
38
39
  const ERASABLE = 'input should be overwritten';
40
41
  /**
42
   *
43
   * @var Renderer
44
   */
45
  private $renderer;
46
47
  /**
48
   * Prepares the environment before running a test.
49
   */
50
  protected function setUp() : void {
51
    parent::setUpExtended();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (setUpExtended() instead of setUp()). Are you sure this is correct? If so, you might want to change this to $this->setUpExtended().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
52
    $this->renderer = new Renderer($this->dic);
53
    $this->renderer->pipe = $this->Graph->build();
54
  }
55
56
  /**
57
   * Cleans up the environment after running a test.
58
   */
59
  protected function tearDown() : void {
60
    $this->renderer = null;
61
    parent::tearDown();
62
  }
63
64
  /**
65
   * Tests Renderer::getFormats()
66
   */
67
  public function testGetFormats() {
68
    $dic = $this->dic;
69
    $dic['use_exceptions'] = false;
70
    $formats = Renderer::getFormats($dic);
71
    $this->assertTrue(is_array($formats), 'Renderer::getFormats() returns an array when exceptions are not used.');
72
73
    $dic['use_exceptions'] = true;
74
    try {
75
      $formats = Renderer::getFormats($dic);
76
      $this->assertTrue(is_array($formats) && !empty($formats), 'Renderer::getFormats() returns a non-empty array when exceptions are used.');
77
    }
78
    catch (ErrorException $e) {
0 ignored issues
show
Bug introduced by
The class ErrorException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
79
      $this->fail('Renderer::getFormats() could not find dot and threw an ErrorException.');
80
    }
81
  }
82
83
  /**
84
   * Test use of magic __call() with non existent filter.
85
   *
86
   * - must throw DomainException
87
   * - must not overwrite source data
88
   */
89
  public function test__call() {
90
    $expected = $out = 'Some data';
91
    try {
92
      $out = call_user_func(array($this->renderer, 'nonexistent'));
93
    }
94
    catch (\DomainException $e) {
95
      $this->assertInstanceOf('\\DomainException', $e, 'Rendering non existent filter throws DomainException');
96
    }
97
    $this->assertEquals($expected, $out, 'Output is not overwritten by non existent filter.');
98
  }
99
100
  /**
101
   * Tests rendering with a single filter.
102
   */
103
  public function testRenderOneFilter() {
104
    // Callback can be a closure.
105
    $callback = function ($x) {
106
      return strrev($x);
107
    };
108
109
    // The value of this string should be overwritten by the filter.
110
    $output = self::ERASABLE;
111
112
    $expected = $callback($this->renderer->pipe);
113
114
    // Invoke filter via renderer, overwriting output and pipe.
115
    call_user_func(array($this->renderer, 'string'), array(
116
      'out' => &$output,
117
      'callback' => $callback,
118
    ));
119
120
    $pipe = $this->renderer->pipe;
121
    $this->assertIsString($pipe, "String filter returns string output");
122
    $this->assertEquals($expected, $this->renderer->pipe, "Filter with closure works on renderer pipe.");
123
124
    // No error output.
125
    $this->expectOutputString("");
126
  }
127
128
  /**
129
   * Test the fluent filter interface.
130
   */
131
  public function testRenderChainedFilters() {
132
    $callback = 'strrev';
133
134
    // The value of this string should be overwritten by the filter.
135
    $output1 = $output2 = $output3 = self::ERASABLE;
136
137
    $input = $this->renderer->pipe;
138
    $step1 = call_user_func(array($this->renderer, 'string'), array(
139
      'out' => &$output1,
140
      'callback' => $callback,
141
    ));
142
143
    $r = call_user_func(array($step1, 'string'), array(
144
      'out' => &$output2,
145
      'callback' => $callback,
146
    ));
147
148
    $this->assertEquals($input, $this->renderer->pipe, "Chained filters with strrev work.");
149
150
    // Test a filter chain: string, then sink.
151
    $pipe_input = $this->renderer->pipe;
152
153
    $r->string(array(
154
      'out' => &$output3,
155
      'callback' => $callback,
156
    ))->sink();
157
158
    $this->assertEquals($callback($pipe_input), $output3, "String filter updates argument.");
159
    $pipe = $this->renderer->pipe;
160
    $this->assertEmpty($pipe, 'Sink filter drops content from stdout.');
161
162
    // No error output from either filter.
163
    $this->expectOutputString("");
164
  }
165
}
166
167