Пример #1
0
 /**
  * Prepare mock of Sitemap model
  *
  * @param array $actualData
  * @param int $maxLines
  * @param int $maxFileSize
  * @param array $expectedFile
  * @param int $expectedWrites
  * @param array $robotsInfo
  * @return \Magento\Sitemap\Model\Sitemap|PHPUnit_Framework_MockObject_MockObject
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 protected function _prepareSitemapModelMock(&$actualData, $maxLines, $maxFileSize, $expectedFile, $expectedWrites, $robotsInfo)
 {
     // Check that all $expectedWrites lines were written
     $actualData = [];
     $currentFile = '';
     $streamWriteCallback = function ($str) use(&$actualData, &$currentFile) {
         if (!array_key_exists($currentFile, $actualData)) {
             $actualData[$currentFile] = '';
         }
         $actualData[$currentFile] .= $str;
     };
     // Check that all expected lines were written
     $this->_fileMock->expects($this->exactly($expectedWrites))->method('write')->will($this->returnCallback($streamWriteCallback));
     // Check that all expected file descriptors were created
     $this->_directoryMock->expects($this->exactly(count($expectedFile)))->method('openFile')->will($this->returnCallback(function ($file) use(&$currentFile) {
         $currentFile = $file;
     }));
     // Check that all file descriptors were closed
     $this->_fileMock->expects($this->exactly(count($expectedFile)))->method('close');
     if (count($expectedFile) == 1) {
         $this->_directoryMock->expects($this->once())->method('renameFile')->will($this->returnCallback(function ($from, $to) {
             \PHPUnit_Framework_Assert::assertEquals('/sitemap-1-1.xml', $from);
             \PHPUnit_Framework_Assert::assertEquals('/sitemap.xml', $to);
         }));
     }
     // Check robots txt
     $robotsStart = '';
     if (isset($robotsInfo['robotsStart'])) {
         $robotsStart = $robotsInfo['robotsStart'];
     }
     $robotsFinish = 'Sitemap: http://store.com/sitemap.xml';
     if (isset($robotsInfo['robotsFinish'])) {
         $robotsFinish = $robotsInfo['robotsFinish'];
     }
     $this->_directoryMock->expects($this->any())->method('readFile')->will($this->returnValue($robotsStart));
     $this->_directoryMock->expects($this->any())->method('write')->with($this->equalTo('robots.txt'), $this->equalTo($robotsFinish));
     // Mock helper methods
     $pushToRobots = 0;
     if (isset($robotsInfo['pushToRobots'])) {
         $pushToRobots = (int) $robotsInfo['pushToRobots'];
     }
     $this->_helperMockSitemap->expects($this->any())->method('getMaximumLinesNumber')->will($this->returnValue($maxLines));
     $this->_helperMockSitemap->expects($this->any())->method('getMaximumFileSize')->will($this->returnValue($maxFileSize));
     $this->_helperMockSitemap->expects($this->any())->method('getEnableSubmissionRobots')->will($this->returnValue($pushToRobots));
     $model = $this->_getModelMock(true);
     return $model;
 }