Added some annotations to tests

This commit is contained in:
Petri Haikonen
2020-04-25 18:16:02 +02:00
committed by Fonata
parent b4e4c14b42
commit a3981edb84

View File

@@ -4,6 +4,7 @@ namespace ParseCsv\tests\methods;
use ParseCsv\Csv; use ParseCsv\Csv;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use ReflectionClass;
class ParseTest extends TestCase { class ParseTest extends TestCase {
@@ -28,6 +29,9 @@ class ParseTest extends TestCase {
$this->parseRepetitiveString('http://looks/like/an/url'); $this->parseRepetitiveString('http://looks/like/an/url');
} }
/**
* @param string $content
*/
private function parseRepetitiveString($content) { private function parseRepetitiveString($content) {
$this->csv->delimiter = ';'; $this->csv->delimiter = ';';
$this->csv->heading = false; $this->csv->heading = false;
@@ -56,6 +60,9 @@ class ParseTest extends TestCase {
$this->assertEquals($this->_get_magazines_data(), $this->csv->data); $this->assertEquals($this->_get_magazines_data(), $this->csv->data);
} }
/**
* @return array
*/
public function autoDetectionProvider() { public function autoDetectionProvider() {
return [ return [
'UTF8_no_BOM' => [__DIR__ . '/../example_files/UTF-8_sep_row_but_no_BOM.csv'], 'UTF8_no_BOM' => [__DIR__ . '/../example_files/UTF-8_sep_row_but_no_BOM.csv'],
@@ -66,7 +73,9 @@ class ParseTest extends TestCase {
public function testSingleColumnWithZeros() { public function testSingleColumnWithZeros() {
$this->csv->delimiter = null; $this->csv->delimiter = null;
$this->csv->parse("URL\nhttp://www.amazon.com/ROX-Ice-Ball-Maker-Original/dp/B00MX59NMQ/ref=sr_1_1?ie=UTF8&qid=1435604374&sr=8-1&keywords=rox,+ice+molds"); $this->csv->parse(
"URL\nhttp://www.amazon.com/ROX-Ice-Ball-Maker-Original/dp/B00MX59NMQ/ref=sr_1_1?ie=UTF8&qid=1435604374&sr=8-1&keywords=rox,+ice+molds"
);
$row = array_pop($this->csv->data); $row = array_pop($this->csv->data);
$expected_data = ['URL' => 'http://www.amazon.com/ROX-Ice-Ball-Maker-Original/dp/B00MX59NMQ/ref=sr_1_1?ie=UTF8&qid=1435604374&sr=8-1&keywords=rox,+ice+molds']; $expected_data = ['URL' => 'http://www.amazon.com/ROX-Ice-Ball-Maker-Original/dp/B00MX59NMQ/ref=sr_1_1?ie=UTF8&qid=1435604374&sr=8-1&keywords=rox,+ice+molds'];
$this->assertEquals($expected_data, $row); $this->assertEquals($expected_data, $row);
@@ -88,14 +97,17 @@ class ParseTest extends TestCase {
$sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\"c\r\nd\""; $sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\"c\r\nd\"";
$expected_data = [86545235689, 34365587654, 13469874576]; $expected_data = [86545235689, 34365587654, 13469874576];
$actual_data = $this->invokeMethod($this->csv, '_parse_string', array($sInput)); $actual_data = $this->invokeMethod($this->csv, '_parse_string', [$sInput]);
$actual_column = array_map('reset', $actual_data); $actual_column = array_map('reset', $actual_data);
$this->assertEquals($expected_data, $actual_column); $this->assertEquals($expected_data, $actual_column);
$this->assertEquals([ $this->assertEquals(
'a', [
'b', 'a',
"c\r\nd", 'b',
], array_map('next', $actual_data)); "c\r\nd",
],
array_map('next', $actual_data)
);
} }
public function testSingleColumn() { public function testSingleColumn() {
@@ -115,18 +127,24 @@ class ParseTest extends TestCase {
$this->csv->output_encoding = 'UTF-8'; $this->csv->output_encoding = 'UTF-8';
$this->csv->auto(__DIR__ . '/../example_files/Piwik_API_download.csv'); $this->csv->auto(__DIR__ . '/../example_files/Piwik_API_download.csv');
$aAction27 = array_column($this->csv->data, 'url (actionDetails 27)'); $aAction27 = array_column($this->csv->data, 'url (actionDetails 27)');
$this->assertEquals([ $this->assertEquals(
'http://application/_Main/_GraphicMeanSTD_MDI/btnConfBandOptions', [
'', 'http://application/_Main/_GraphicMeanSTD_MDI/btnConfBandOptions',
'', '',
], $aAction27); '',
],
$aAction27
);
$aCity = array_column($this->csv->data, 'city'); $aCity = array_column($this->csv->data, 'city');
$this->assertEquals([ $this->assertEquals(
'São Paulo', [
'Johannesburg', 'São Paulo',
'', 'Johannesburg',
], $aCity); '',
],
$aCity
);
} }
/** /**
@@ -135,7 +153,8 @@ class ParseTest extends TestCase {
public function testStringWithLeadingBOM() { public function testStringWithLeadingBOM() {
$string_with_bom = strtr( $string_with_bom = strtr(
file_get_contents(__DIR__ . '/../example_files/UTF-8_with_BOM_and_sep_row.csv'), file_get_contents(__DIR__ . '/../example_files/UTF-8_with_BOM_and_sep_row.csv'),
["sep=;\n" => '']); ["sep=;\n" => '']
);
// Is the BOM still there? // Is the BOM still there?
self::assertSame(0xEF, ord($string_with_bom)); self::assertSame(0xEF, ord($string_with_bom));
@@ -220,11 +239,17 @@ class ParseTest extends TestCase {
$this->assertFalse($this->csv->autoDetectFileHasHeading()); $this->assertFalse($this->csv->autoDetectFileHasHeading());
} }
/**
* @doesNotPerformAssertions
*/
public function testVeryLongNonExistingFile() { public function testVeryLongNonExistingFile() {
$this->csv->parse(str_repeat('long_string', PHP_MAXPATHLEN)); $this->csv->parse(str_repeat('long_string', PHP_MAXPATHLEN));
$this->csv->auto(str_repeat('long_string', PHP_MAXPATHLEN)); $this->csv->auto(str_repeat('long_string', PHP_MAXPATHLEN));
} }
/**
* @return array
*/
protected function _get_magazines_data() { protected function _get_magazines_data() {
return [ return [
[ [
@@ -245,7 +270,11 @@ class ParseTest extends TestCase {
]; ];
} }
public function autoQuotesDataProvider() { /**
* @return array
*/
public function autoQuotesDataProvider(): array
{
return array( return array(
array('auto-double-enclosure.csv', '"'), array('auto-double-enclosure.csv', '"'),
array('auto-single-enclosure.csv', "'"), array('auto-single-enclosure.csv', "'"),
@@ -270,14 +299,14 @@ class ParseTest extends TestCase {
/** /**
* Call protected/private method of a class. * Call protected/private method of a class.
* *
* @param object &$object Instantiated object that we will run method on. * @param object $object Instantiated object that we will run method on.
* @param string $methodName Method name to call * @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method. * @param array $parameters Array of parameters to pass into method.
* *
* @return mixed Method return. * @return mixed Method return.
*/ */
private function invokeMethod(&$object, $methodName, array $parameters = array()) { private function invokeMethod($object, $methodName, $parameters = []) {
$reflection = new \ReflectionClass(get_class($object)); $reflection = new ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName); $method = $reflection->getMethod($methodName);
$method->setAccessible(true); $method->setAccessible(true);
@@ -285,7 +314,7 @@ class ParseTest extends TestCase {
} }
public function testWaiverFieldSeparator() { public function testWaiverFieldSeparator() {
$this->assertSame(false, $this->csv->auto(__DIR__ . '/../example_files/waiver_field_separator.csv')); $this->assertFalse($this->csv->auto(__DIR__ . '/../example_files/waiver_field_separator.csv'));
$expected = [ $expected = [
'liability waiver', 'liability waiver',
'release of liability form', 'release of liability form',