📜 ⬆️ ⬇️

PHPUnit: spreadsheet (spreadsheet) as a data source (data provider)

The PHPUnit documentation has a small section dedicated to data sources (data providers) that allow you to feed a large amount of data to the test, and just below there is even an example of a data source for a CSV file.


Of course, use a full spreadsheet (spreadsheet)!

We agree that:

And we will immediately get down to business (minimum text, maximum code), the entire code is also available via the link at the bottom of the note:
')

Step # 1: Dependencies


We will need ( composer.json ):
 { "name": "PHPUnitSpreadsheetDataProvider", "require": { "php": ">=5.5.0", "phpunit/phpunit": "4.5.*", "phpoffice/phpexcel": "1.8.*" }, "autoload": { "classmap": [ "src/" ] } } 


Step # 2: Basic Logic


It all comes down to adding your own method that will receive data from the file and turn it into a data source for tests.

Receiving:
 <?php // PHPUnitTestCase.php trait PHPUnitSDP_PHPUnitTestCase { /** * @var PHPExcel_Reader_IReader|PHPExcel_Reader_Abstract */ private $_reader; /** *      . * * @param string $resource * * @return string */ protected function getTestResource($resource = null) { $path = (new ReflectionClass($this))->getFileName(); $dirname = pathinfo($path, PATHINFO_DIRNAME); $filename = pathinfo($path, PATHINFO_FILENAME); $resource = $resource ?: 'xml'; return "{$dirname}/{$filename}.{$resource}"; } /** *    . * * @param string $test * * @return array|Iterator */ public function getTestDataProvider($test) { //  $position = 2; $resource = $this->getTestResource('data.ods'); // Reader? if (is_null($this->_reader)) { $this->_reader = PHPExcel_IOFactory::createReaderForFile($resource); } //  $this->_reader->setReadDataOnly(true); $this->_reader->setLoadSheetsOnly($test); //  return new PHPUnitSDP_PHPExcelWorksheetRowIterator( $this->_reader->load($resource)->getActiveSheet(), $position); } } 

Two points deserve attention (the rest, I hope, obviously):
  1. $position = 2; - the first line (numbering starts from 1) with data, everything that can be used for commenting (see example below)
  2. $resource - defines the name of the file with data, in this case it is " .data.ods "

Transformation:
 <?php // PHPExcelWorksheetRowIterator.php class PHPUnitSDP_PHPExcelWorksheetRowIterator extends PHPExcel_Worksheet_RowIterator { /** * @return array */ public function current() { $current = array(); foreach (parent::current()->getCellIterator() as $cell) { /* @var $cell PHPExcel_Cell */ $current[] = $this->getValue($cell->getCalculatedValue()); } return $current; } /** * @param mixed $value * * @return mixed */ protected function getValue($value) { switch (mb_strtolower(trim($value))) { case 'null': $value = null; break; case 'true': $value = true; break; case 'false': $value = false; break; default: /* empty */ break; } return $value; } } 

Of the features, it is worth noting the possibility of using formulas in cells, but there is still no way to refuse a separate method for converting values ​​- firstly, not all data types have necessary functions (the same NULL ), secondly, the calculation of formulas requires time and resources.

Step # 3: Data


image

Step # 4: Test


 // SDPTest.php class SDPTest extends PHPUnit_Framework_TestCase { use PHPUnitSDP_PHPUnitTestCase; /** * @dataProvider getTestDataProvider * * @param number $base * @param number $exp * @param number $expected * * @return void */ public function testPow($base, $exp, $expected) { $this->assertEquals($expected, pow($base, $exp)); } /** * @dataProvider getTestDataProvider * * @param number $arg * @param number $expected * * @return void */ public function testSqrt($arg, $expected) { $this->assertEquals($expected, sqrt($arg)); } } 

All magic is enclosed in the @dataProvider getTestDataProvider annotation @dataProvider getTestDataProvider - before running the test, PHPUnit will call the previously defined PHPUnitSDP_PHPUnitTestCase::getTestDataProvider() method with an argument containing the name of the test and get the required data source.

Step # 5: Result


 PHPUnitSpreadsheetDataProvider> phpunit PHPUnit 4.5.0 by Sebastian Bergmann and contributors. Configuration read from PHPUnitSpreadsheetDataProvider/phpunit.xml ......F Time: 158 ms, Memory: 8.75Mb There was 1 failure: 1) SDPTest::testSqrt with data set #4 (4.0, 3.0) Failed asserting that 2.0 matches expected 3.0. PHPUnitSpreadsheetDataProvider/tests/SDPTest.php:28 phar://PHPUnitSpreadsheetDataProvider/phpunit.phar/phpunit/TextUI/Command.php:152 phar://PHPUnitSpreadsheetDataProvider/phpunit.phar/phpunit/TextUI/Command.php:104 FAILURES! Tests: 7, Assertions: 7, Failures: 1. 


Conclusion


I hope this recipe is useful to someone :)

Project: yadi.sk/d/AyegnPCqf7i9Y

Source: https://habr.com/ru/post/252489/


All Articles