From 8a3dd6d76a2a5ec727b46fc8bc6e655c27eb7001 Mon Sep 17 00:00:00 2001 From: peter279k Date: Wed, 16 May 2018 00:41:41 +0800 Subject: [PATCH] Test enhancement (#52) --- .travis.yml | 9 +++--- composer.json | 7 +++- tests/RRuleTest.php | 69 +++++++++++++++++++++++++-------------- tests/RSetTest.php | 71 ++++++++++++++++++++++++++++------------- tests/RfcParserTest.php | 7 ++-- tests/bootstrap.php | 10 +++++- 6 files changed, 118 insertions(+), 55 deletions(-) mode change 100755 => 100644 tests/bootstrap.php diff --git a/.travis.yml b/.travis.yml index d689653..f839146 100755 --- a/.travis.yml +++ b/.travis.yml @@ -1,15 +1,16 @@ -dist: precise language: php php: - - 5.3 - 5.4 - 5.5 - 5.6 - 7.0 - 7.1 - 7.2 +matrix: + include: + - php: 5.3 + dist: precise install: - - composer install - - composer require --dev phpunit/phpunit "<6" + - composer install -n script: - vendor/bin/phpunit \ No newline at end of file diff --git a/composer.json b/composer.json index 24778c6..5f783c0 100755 --- a/composer.json +++ b/composer.json @@ -16,7 +16,12 @@ "RRule\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "RRule\\Tests\\": "tests/" + } + }, "require-dev": { - "phpunit/phpunit": "<6" + "phpunit/phpunit": "^4.8|^5.5|^6.5" } } diff --git a/tests/RRuleTest.php b/tests/RRuleTest.php index 793e7ba..dfbad9c 100755 --- a/tests/RRuleTest.php +++ b/tests/RRuleTest.php @@ -1,12 +1,15 @@ assertInstanceOf('RRule\RRule', $result); } /** @@ -1758,12 +1762,19 @@ class RRuleTest extends PHPUnit_Framework_TestCase date_create('2017-01-01'),date_create('2017-01-02'),date_create('2017-01-03'), date_create('2017-01-04'),date_create('2017-01-05') ), $rrule->getOccurrences(5)); - try { - $rrule->getOccurrences(); - $this->fail('Expected exception (infinite rule) not thrown'); - } catch ( \LogicException $e ) { + } - } + /** + * @expectedException LogicException + * @expectedExceptionMessage Cannot get all occurrences of an infinite recurrence rule. + */ + public function testGetOccurrencesThrowsLogicException() + { + $rrule = new RRule(array( + 'FREQ' => 'DAILY', + 'DTSTART' => '2017-01-01' + )); + $rrule->getOccurrences(); } public function testGetOccurrencesBetween() @@ -1777,12 +1788,19 @@ class RRuleTest extends PHPUnit_Framework_TestCase $this->assertCount(1, $rrule->getOccurrencesBetween('2017-02-01', '2017-12-31', 1)); $this->assertEquals(array(date_create('2017-02-01')), $rrule->getOccurrencesBetween('2017-02-01', '2017-12-31', 1)); $this->assertCount(5, $rrule->getOccurrencesBetween('2017-01-01', null, 5)); - try { - $rrule->getOccurrencesBetween('2017-01-01', null); - $this->fail('Expected exception (infinite rule) not thrown'); - } catch ( \LogicException $e ) { - - } + } + + /** + * @expectedException LogicException + * @expectedExceptionMessage Cannot get all occurrences of an infinite recurrence rule. + */ + public function testGetOccurrencesBetweenThrowsLogicException() + { + $rrule = new RRule(array( + 'FREQ' => 'DAILY', + 'DTSTART' => '2017-01-01' + )); + $rrule->getOccurrencesBetween('2017-01-01', null); } /////////////////////////////////////////////////////////////////////////////// @@ -1943,13 +1961,15 @@ class RRuleTest extends PHPUnit_Framework_TestCase $rrule = new RRule('RRULE:FREQ=YEARLY', '2017-01-01'); $this->assertEquals('2017-01-01', $rrule[0]->format('Y-m-d')); + } - try { - $rrule = new RRule("DTSTART:19970512\nRRULE:FREQ=YEARLY", date_create('2017-01-01')); - $this->fail('Expected InvalidArgumentException (too many dtstart) not thrown'); - } catch ( \InvalidArgumentException $e ) { - - } + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage Too many DTSTART properties (there can be only one) + */ + public function testRfcStringParserWithMultipleDtStart() + { + $rrule = new RRule("DTSTART:19970512\nRRULE:FREQ=YEARLY", date_create('2017-01-01')); } /** @@ -1979,7 +1999,7 @@ class RRuleTest extends PHPUnit_Framework_TestCase /** * @dataProvider quirkyRfcStrings - * @expectedException PHPUnit_Framework_Error_Notice + * @expectedException PHPUnit\Framework\Error\Notice */ public function testQuirkyRfcStringsParserNotice($str,$occurrences) { @@ -2076,6 +2096,7 @@ class RRuleTest extends PHPUnit_Framework_TestCase $str = $rrule->rfcString(); $new_rrule = new RRule($str); + $this->assertInstanceOf('RRule\RRule', $new_rrule); } public function testUnsupportedTimezoneConvertedToUtc() diff --git a/tests/RSetTest.php b/tests/RSetTest.php index 5b07e5f..ec4655d 100755 --- a/tests/RSetTest.php +++ b/tests/RSetTest.php @@ -1,9 +1,15 @@ getOccurrences(5)); - try { - $rset->getOccurrences(); - $this->fail('Expected exception (infinite rule) not thrown'); - } catch ( \LogicException $e ) { + } - } + /** + * @expectedException LogicException + * @expectedExceptionMessage Cannot get all occurrences of an infinite recurrence set. + */ + public function testGetOccurrencesThrowsLogicException() + { + $rset = new RSet(); + $rset->addRRule(new RRule(array( + 'FREQ' => 'DAILY', + 'DTSTART' => '2017-01-01' + ))); + $rset->getOccurrences(); } public function testGetOccurrencesBetween() @@ -465,12 +479,20 @@ class RSetTest extends PHPUnit_Framework_TestCase $this->assertCount(1, $rset->getOccurrencesBetween('2017-02-01', '2017-12-31', 1)); $this->assertEquals(array(date_create('2017-02-01')), $rset->getOccurrencesBetween('2017-02-01', '2017-12-31', 1)); $this->assertCount(5, $rset->getOccurrencesBetween('2017-01-01', null, 5)); - try { - $rset->getOccurrencesBetween('2017-01-01', null); - $this->fail('Expected exception (infinite rule) not thrown'); - } catch ( \LogicException $e ) { - - } + } + + /** + * @expectedException LogicException + * @expectedExceptionMessage Cannot get all occurrences of an infinite recurrence rule. + */ + public function testGetOccurrencesBetweenThrowsLogicException() + { + $rset = new RSet(); + $rset->addRRule(new RRule(array( + 'FREQ' => 'DAILY', + 'DTSTART' => '2017-01-01' + ))); + $rset->getOccurrencesBetween('2017-01-01', null); } /////////////////////////////////////////////////////////////////////////////// @@ -539,17 +561,20 @@ class RSetTest extends PHPUnit_Framework_TestCase date_create('2017-01-02'), date_create('2017-01-03') ), $rset->getOccurrences()); - - try { - $rset = new RSet( - "DTSTART:DTSTART;TZID=America/New_York:19970901T090000\nRRULE:FREQ=DAILY;COUNT=3\nEXRULE:FREQ=DAILY;INTERVAL=2;COUNT=1", - date_create('2017-01-01') - ); - $this->fail('Expected InvalidArgumentException (too many start date) not thrown'); - } catch ( InvalidArgumentException $e ) { - - } } + + /** + * @expectedException InvalidArgumentException + * @expectedExcpetionMessage Failed to parse RFC string, multiple DTSTART found + */ + public function testParseRfcStringWithMultipleDtStart() + { + $rset = new RSet( + "DTSTART:DTSTART;TZID=America/New_York:19970901T090000\nRRULE:FREQ=DAILY;COUNT=3\nEXRULE:FREQ=DAILY;INTERVAL=2;COUNT=1", + date_create('2017-01-01') + ); + } + public function quirkyRfcStrings() { return array( @@ -569,7 +594,7 @@ class RSetTest extends PHPUnit_Framework_TestCase /** * @dataProvider quirkyRfcStrings - * @expectedException PHPUnit_Framework_Error_Notice + * @expectedException PHPUnit\Framework\Error\Notice */ public function testParseQuirkyRfcStringNotice($string, $occurrences) { diff --git a/tests/RfcParserTest.php b/tests/RfcParserTest.php index a62859a..5cc71ca 100755 --- a/tests/RfcParserTest.php +++ b/tests/RfcParserTest.php @@ -1,8 +1,11 @@