/** * Rewind the \Iterator to the first element (\Iterator interface) * * @return void */ public function rewind() { $this->_file->seek(0); $this->_getNextRow(); // skip first line with the header parent::rewind(); }
/** * Write row data to source file. * * @param array $rowData * @throws \Exception * @return $this */ public function writeRow(array $rowData) { if (null === $this->_headerCols) { $this->setHeaderCols(array_keys($rowData)); } $this->_fileHandler->writeCsv(array_merge($this->_headerCols, array_intersect_key($rowData, $this->_headerCols)), $this->_delimiter, $this->_enclosure); return $this; }
/** * Write closing tag and close stream * * @param string $type * @return void */ protected function _finalizeSitemap($type = self::TYPE_URL) { if ($this->_stream) { $this->_stream->write(sprintf($this->_tags[$type][self::CLOSE_TAG_KEY], $type)); $this->_stream->close(); } // Reset all counters $this->_lineCount = 0; $this->_fileSize = 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; }
public function testUnlock() { $result = true; $this->driver->expects($this->once())->method('fileUnlock')->with($this->resource)->will($this->returnValue($result)); $this->assertEquals($result, $this->file->unlock()); }