Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
DataTest.php
Go to the documentation of this file.
1 <?php
8 
12 class DataTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $model;
18 
22  protected $contextMock;
23 
27  protected $stringMock;
28 
30  protected $requestMock;
31 
35  protected $scopeConfigMock;
36 
40  protected $escaperMock;
41 
45  protected $storeManagerMock;
46 
50  private $urlBuilderMock;
51 
52  protected function setUp()
53  {
54  $this->stringMock = $this->createMock(\Magento\Framework\Stdlib\StringUtils::class);
55  $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
56  $this->escaperMock = $this->createMock(\Magento\Framework\Escaper::class);
57  $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
58  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
59  ->disableOriginalConstructor()
60  ->setMethods([])
61  ->getMock();
62  $this->urlBuilderMock = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
63  ->setMethods(['getUrl'])
64  ->disableOriginalConstructor()
65  ->getMockForAbstractClass();
66  $this->contextMock = $this->createMock(\Magento\Framework\App\Helper\Context::class);
67  $this->contextMock->expects($this->any())->method('getScopeConfig')->willReturn($this->scopeConfigMock);
68  $this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
69  $this->contextMock->expects($this->any())->method('getUrlBuilder')->willReturn($this->urlBuilderMock);
70 
71  $this->model = new \Magento\Search\Helper\Data(
72  $this->contextMock,
73  $this->stringMock,
74  $this->escaperMock,
75  $this->storeManagerMock
76  );
77  }
78 
79  public function testGetMinQueryLength()
80  {
81  $return = 'some_value';
82  $this->scopeConfigMock->expects($this->once())
83  ->method('getValue')
84  ->with(
85  \Magento\Search\Model\Query::XML_PATH_MIN_QUERY_LENGTH,
86  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
87  null
88  )
89  ->will($this->returnValue($return));
90  $this->assertEquals($return, $this->model->getMinQueryLength());
91  }
92 
93  public function testGetMaxQueryLength()
94  {
95  $return = 'some_value';
96  $this->scopeConfigMock->expects($this->once())
97  ->method('getValue')
98  ->with(
99  \Magento\Search\Model\Query::XML_PATH_MAX_QUERY_LENGTH,
100  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
101  null
102  )
103  ->will($this->returnValue($return));
104  $this->assertEquals($return, $this->model->getMaxQueryLength());
105  }
106 
110  public function testGetEscapedQueryText($queryText, $maxQueryLength, $expected)
111  {
112  $this->requestMock->expects($this->once())->method('getParam')->willReturn($queryText);
113  $this->stringMock->expects($this->any())->method('cleanString')->willReturnArgument(0);
114  $this->scopeConfigMock->expects($this->any())->method('getValue')->willReturn($maxQueryLength);
115  $this->stringMock
116  ->expects($this->any())
117  ->method('strlen')
118  ->will($this->returnCallback(function ($queryText) {
119  return strlen($queryText);
120  }));
121  $this->stringMock
122  ->expects($this->any())
123  ->method('substr')
124  ->with($queryText, 0, $maxQueryLength)
125  ->willReturn($expected);
126  $this->escaperMock->expects($this->any())->method('escapeHtml')->willReturnArgument(0);
127  $this->assertEquals($expected, $this->model->getEscapedQueryText());
128  }
129 
133  public function queryTextDataProvider()
134  {
135  return [
136  ['', 100, ''],
137  [null, 100, ''],
138  [['test'], 100, ''],
139  ['test', 100, 'test'],
140  ['testtest', 7, 'testtes'],
141  ];
142  }
143 
151  public function testGetSuggestUrl(bool $isSecure)
152  {
153  $this->requestMock->expects(self::once())
154  ->method('isSecure')
155  ->willReturn($isSecure);
156  $this->urlBuilderMock->expects(self::once())
157  ->method('getUrl')
158  ->with(self::identicalTo('search/ajax/suggest'), self::identicalTo(['_secure' => $isSecure]));
159  $this->model->getSuggestUrl();
160  }
161 
167  public function getSuggestUrlDataProvider()
168  {
169  return [
170  'non-secure' => [
171  'isSecure' => false,
172  ],
173  'secure' => [
174  'secure' => true,
175  ],
176  ];
177  }
178 }
testGetEscapedQueryText($queryText, $maxQueryLength, $expected)
Definition: DataTest.php:110
$maxQueryLength
Definition: form.phtml:17