FOSSology  4.4.0
Open Source License Compliance by Open Source Software
HighlightRendererTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4  Author: Andreas Würl
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\View;
10 
15 use Mockery\MockInterface;
16 use Mockery as M;
17 
18 class HighlightRendererTest extends \PHPUnit\Framework\TestCase
19 {
21  private $level = 5;
23  private $highlight;
25  private $splitPosition;
27  private $htmlElement;
29  private $highlightRenderer;
31  private $colorMap;
32 
33  function setUp() : void
34  {
35  $this->highlightRenderer = new HighlightRenderer();
36  $this->colorMap = array('type1' => 'red', 'type2' => 'yellow', 'any' => 'gray');
37 
38  $this->prepareMocks();
39  }
40 
41  function tearDown() : void
42  {
43  M::close();
44  }
45 
46  public function prepareMocks()
47  {
48  $this->htmlElement = M::mock(SimpleHtmlElement::class);
49  $this->htmlElement->shouldReceive('getOpeningText')->andReturn('<element>');
50  $this->htmlElement->shouldReceive('getClosingText')->andReturn('</element>');
51 
52  $this->highlight = M::mock(Highlight::class);
53  $this->highlight->shouldReceive('getType')->andReturn(Highlight::MATCH)->byDefault();
54  $this->highlight->shouldReceive('getInfoText')->andReturn("<infoText>")->byDefault();
55  $this->highlight->shouldReceive('getHtmlElement')->andReturn(null)->byDefault();
56 
57  $this->splitPosition = M::mock(SplitPosition::class);
58  $this->splitPosition->shouldReceive('getLevel')->andReturn($this->level)->byDefault();
59  $this->splitPosition->shouldReceive('getHighlight')->andReturn($this->highlight)->byDefault();
60  }
61 
62  public function testCreateSpanStart()
63  {
64  $result = $this->highlightRenderer->createSpanStart($this->splitPosition);
65 
66  assertThat($result, is("<span class=\"hi-match\" title=\"<infoText>\">"));
67  }
68 
69  public function testCreateSpanStartWrappingHtmlElement()
70  {
71  $this->highlight->shouldReceive('getHtmlElement')->andReturn($this->htmlElement);
72 
73  $result = $this->highlightRenderer->createSpanStart($this->splitPosition);
74 
75  assertThat($result, is("<span class=\"hi-match\" title=\"<infoText>\"><element>"));
76  }
77 
78  public function testCreateSpanStartWithUndefinedType()
79  {
80  $this->highlight->shouldReceive('getType')->andReturn("<anything>");
81  $this->highlight->shouldReceive('getInfoText')->andReturn(null);
82 
83  $result = $this->highlightRenderer->createSpanStart($this->splitPosition);
84 
85  assertThat($result, is("<span class=\"hi-undefined\" title=\"\">"));
86  }
87 
88  public function testCreateSpanStartWithPadding()
89  {
90  $this->splitPosition->shouldReceive('getLevel')->andReturn(-1);
91 
92  $result = $this->highlightRenderer->createSpanStart($this->splitPosition);
93 
94  assertThat($result, is("<span class=\"hi-match\" title=\"<infoText>\">"));
95  }
96 
97  public function testCreateSpanEnd()
98  {
99  $result = $this->highlightRenderer->createSpanEnd($this->splitPosition);
100 
101  assertThat($result, is("</span>"));
102  }
103 
104  public function testCreateSpanEndWithWrappeingHtmlElement()
105  {
106  $this->highlight->shouldReceive('getHtmlElement')->andReturn($this->htmlElement);
107 
108  $result = $this->highlightRenderer->createSpanEnd($this->splitPosition);
109 
110  assertThat($result, is("</element></span>"));
111  }
112 }