TYPO3 CMS  TYPO3_8-7
CropViewHelperTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 
25 class CropViewHelperTest extends ViewHelperBaseTestcase
26 {
30  protected $viewHelper;
31 
35  protected $mockContentObject;
36 
37  protected function setUp()
38  {
39  parent::setUp();
40  $this->mockContentObject = $this->createMock(ContentObjectRenderer::class);
41  $this->viewHelper = new CropViewHelper();
42  $this->injectDependenciesIntoViewHelper($this->viewHelper);
43  $this->viewHelper->setRenderChildrenClosure(
44  function () {
45  return 'Some Content';
46  }
47  );
48  }
49 
54  {
55  $this->mockContentObject->expects($this->once())->method('cropHTML')->with('Some Content', '123|...|1')->will($this->returnValue('Cropped Content'));
56  GeneralUtility::addInstance(ContentObjectRenderer::class, $this->mockContentObject);
57  $this->setArgumentsUnderTest(
58  $this->viewHelper,
59  [
60  'maxCharacters' => '123',
61  ]
62  );
63  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
64  $this->assertEquals('Cropped Content', $actualResult);
65  }
66 
71  {
72  $this->mockContentObject->expects($this->once())->method('cropHTML')->with('Some Content', '-321|custom suffix|1')->will($this->returnValue('Cropped Content'));
73  GeneralUtility::addInstance(ContentObjectRenderer::class, $this->mockContentObject);
74  $this->setArgumentsUnderTest(
75  $this->viewHelper,
76  [
77  'maxCharacters' => '-321',
78  'append' => 'custom suffix',
79  ]
80  );
81  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
82  $this->assertEquals('Cropped Content', $actualResult);
83  }
84 
89  {
90  $this->mockContentObject->expects($this->once())->method('cropHTML')->with('Some Content', '123|...|')->will($this->returnValue('Cropped Content'));
91  GeneralUtility::addInstance(ContentObjectRenderer::class, $this->mockContentObject);
92  $this->setArgumentsUnderTest(
93  $this->viewHelper,
94  [
95  'maxCharacters' => '123',
96  'append' => '...',
97  'respectWordBoundaries' => false,
98  ]
99  );
100  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
101  $this->assertEquals('Cropped Content', $actualResult);
102  }
103 
107  public function respectHtmlCanBeDisabled()
108  {
109  $this->mockContentObject->expects($this->once())->method('crop')->with('Some Content', '123|...|1')->will($this->returnValue('Cropped Content'));
110  GeneralUtility::addInstance(ContentObjectRenderer::class, $this->mockContentObject);
111  $this->setArgumentsUnderTest(
112  $this->viewHelper,
113  [
114  'maxCharacters' => '123',
115  'append' => '...',
116  'respectWordBoundaries' => true,
117  'respectHtml' => false,
118  ]
119  );
120  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
121  $this->assertEquals('Cropped Content', $actualResult);
122  }
123 }
static addInstance($className, $instance)