FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
HighlightRendererTest.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, Siemens AG
4 Author: Andreas Würl
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 namespace Fossology\Lib\View;
21 
27 use Mockery as M;
28 
29 class HighlightRendererTest extends \PHPUnit\Framework\TestCase
30 {
32  private $level = 5;
34  private $highlight;
36  private $splitPosition;
38  private $htmlElement;
40  private $highlightRenderer;
42  private $colorMap;
43 
44  function setUp()
45  {
46  $this->highlightRenderer = new HighlightRenderer();
47  $this->colorMap = array('type1' => 'red', 'type2' => 'yellow', 'any' => 'gray');
48 
49  $this->prepareMocks();
50  }
51 
52  function tearDown()
53  {
54  M::close();
55  }
56 
57  public function prepareMocks()
58  {
59  $this->htmlElement = M::mock(SimpleHtmlElement::class);
60  $this->htmlElement->shouldReceive('getOpeningText')->andReturn('<element>');
61  $this->htmlElement->shouldReceive('getClosingText')->andReturn('</element>');
62 
63  $this->highlight = M::mock(Highlight::class);
64  $this->highlight->shouldReceive('getType')->andReturn(Highlight::MATCH)->byDefault();
65  $this->highlight->shouldReceive('getInfoText')->andReturn("<infoText>")->byDefault();
66  $this->highlight->shouldReceive('getHtmlElement')->andReturn(null)->byDefault();
67 
68  $this->splitPosition = M::mock(SplitPosition::class);
69  $this->splitPosition->shouldReceive('getLevel')->andReturn($this->level)->byDefault();
70  $this->splitPosition->shouldReceive('getHighlight')->andReturn($this->highlight)->byDefault();
71  }
72 
73  public function testCreateSpanStart()
74  {
75  $result = $this->highlightRenderer->createSpanStart($this->splitPosition);
76 
77  assertThat($result, is("<span class=\"hi-match\" title=\"<infoText>\">"));
78  }
79 
80  public function testCreateSpanStartWrappingHtmlElement()
81  {
82  $this->highlight->shouldReceive('getHtmlElement')->andReturn($this->htmlElement);
83 
84  $result = $this->highlightRenderer->createSpanStart($this->splitPosition);
85 
86  assertThat($result, is("<span class=\"hi-match\" title=\"<infoText>\"><element>"));
87  }
88 
89  public function testCreateSpanStartWithUndefinedType()
90  {
91  $this->highlight->shouldReceive('getType')->andReturn("<anything>");
92  $this->highlight->shouldReceive('getInfoText')->andReturn(null);
93 
94  $result = $this->highlightRenderer->createSpanStart($this->splitPosition);
95 
96  assertThat($result, is("<span class=\"hi-undefined\" title=\"\">"));
97  }
98 
99  public function testCreateSpanStartWithPadding()
100  {
101  $this->splitPosition->shouldReceive('getLevel')->andReturn(-1);
102 
103  $result = $this->highlightRenderer->createSpanStart($this->splitPosition);
104 
105  assertThat($result, is("<span class=\"hi-match\" title=\"<infoText>\">"));
106  }
107 
108  public function testCreateSpanEnd()
109  {
110  $result = $this->highlightRenderer->createSpanEnd($this->splitPosition);
111 
112  assertThat($result, is("</span>"));
113  }
114 
115  public function testCreateSpanEndWithWrappeingHtmlElement()
116  {
117  $this->highlight->shouldReceive('getHtmlElement')->andReturn($this->htmlElement);
118 
119  $result = $this->highlightRenderer->createSpanEnd($this->splitPosition);
120 
121  assertThat($result, is("</element></span>"));
122  }
123 }