1
0
mirror of https://github.com/rlanvin/php-rrule.git synced 2025-02-20 09:54:16 +01:00

Make offsetGet throw exception for illegal offset

Ref issue #22
This commit is contained in:
rlanvin 2017-01-06 16:16:44 +02:00
parent 472462e0bb
commit 0785c6ce7d
5 changed files with 180 additions and 95 deletions

View File

@ -2,6 +2,10 @@
## [Unreleased]
### Fixed
- `RRule::offsetGet` and `RSet::offsetGet` throw `InvalidArgumentException` for illegal offset types [#22](https://github.com/rlanvin/php-rrule/issues/22)
## [1.4.0] - 2016-11-11
### Added

View File

@ -1111,7 +1111,7 @@ class RRule implements RRuleInterface
*/
public function offsetExists($offset)
{
return is_numeric($offset) && $offset >= 0 && $offset < count($this);
return is_numeric($offset) && $offset >= 0 && ! is_float($offset) && $offset < count($this);
}
/**
@ -1119,6 +1119,10 @@ class RRule implements RRuleInterface
*/
public function offsetGet($offset)
{
if ( ! is_numeric($offset) || $offset < 0 || is_float($offset) ) {
throw new \InvalidArgumentException('Illegal offset type: '.gettype($offset));
}
if ( isset($this->cache[$offset]) ) {
// found in cache
return clone $this->cache[$offset];

View File

@ -427,7 +427,7 @@ class RSet implements RRuleInterface
*/
public function offsetExists($offset)
{
return is_numeric($offset) && $offset >= 0 && $offset < count($this);
return is_numeric($offset) && $offset >= 0 && ! is_float($offset) && $offset < count($this);
}
/**
@ -435,6 +435,10 @@ class RSet implements RRuleInterface
*/
public function offsetGet($offset)
{
if ( ! is_numeric($offset) || $offset < 0 || is_float($offset) ) {
throw new \InvalidArgumentException('Illegal offset type: '.gettype($offset));
}
if ( isset($this->cache[$offset]) ) {
// found in cache
return clone $this->cache[$offset];

View File

@ -2092,44 +2092,6 @@ class RRuleTest extends PHPUnit_Framework_TestCase
$this->assertTrue(\RRule\is_leap_year(2000));
}
public function testCountable()
{
$rrule = new RRule(array(
'freq' => 'yearly',
'count' => 10
));
$this->assertEquals(10, count($rrule));
}
public function testOffsetExists()
{
$rrule = new RRule(array(
'freq' => 'daily',
'count' => 3,
'byday' => 'TU,TH',
'dtstart' => '2007-01-01'
));
$this->assertTrue(isset($rrule[0]));
$this->assertTrue(isset($rrule[1]));
$this->assertTrue(isset($rrule[2]));
$this->assertFalse(isset($rrule[3]));
}
public function testOffsetGet()
{
$rrule = new RRule(array(
'freq' => 'daily',
'count' => 3,
'byday' => 'TU,TH',
'dtstart' => '2007-01-01'
));
$this->assertEquals(date_create('2007-01-02'), $rrule[0]);
$this->assertEquals(date_create('2007-01-04'), $rrule[1]);
$this->assertEquals(date_create('2007-01-09'), $rrule[2]);
$this->assertEquals(null, $rrule[4]);
}
public function testDateTimeMutableReferenceBug()
{
$date = date_create('2007-01-01');
@ -2208,6 +2170,78 @@ class RRuleTest extends PHPUnit_Framework_TestCase
$this->assertInternalType('array', $rrule->getRule());
}
///////////////////////////////////////////////////////////////////////////////
// Array access and countable interfaces
public function testCountable()
{
$rrule = new RRule(array(
'freq' => 'yearly',
'count' => 10
));
$this->assertEquals(10, count($rrule));
}
public function testOffsetExists()
{
$rrule = new RRule(array(
'freq' => 'daily',
'count' => 3,
'byday' => 'TU,TH',
'dtstart' => '2007-01-01'
));
$this->assertTrue(isset($rrule[0]));
$this->assertTrue(isset($rrule[1]));
$this->assertTrue(isset($rrule[2]));
$this->assertFalse(isset($rrule[3]));
}
public function testOffsetGet()
{
$rrule = new RRule(array(
'freq' => 'daily',
'count' => 3,
'byday' => 'TU,TH',
'dtstart' => '2007-01-01'
));
$this->assertEquals(date_create('2007-01-02'), $rrule[0]);
$this->assertEquals(date_create('2007-01-02'), $rrule['0']);
$this->assertEquals(date_create('2007-01-04'), $rrule[1]);
$this->assertEquals(date_create('2007-01-04'), $rrule['1']);
$this->assertEquals(date_create('2007-01-09'), $rrule[2]);
$this->assertEquals(null, $rrule[4]);
$this->assertEquals(null, $rrule['4']);
}
public function illegalOffsets()
{
return array(
array('dtstart'),
array('1dtstart'),
array(array()),
array(1.1),
array(-1),
array(null),
array(new stdClass())
);
}
/**
* @dataProvider illegalOffsets
* @expectedException InvalidArgumentException
*/
public function testOffsetGetInvalidArgument($offset)
{
$rrule = new RRule(array(
'freq' => 'daily',
'count' => 3,
'byday' => 'TU,TH',
'dtstart' => '2007-01-01'
));
$rrule[$offset];
}
///////////////////////////////////////////////////////////////////////////////
// Human readable string conversion

View File

@ -42,7 +42,7 @@ class RSetTest extends PHPUnit_Framework_TestCase
public function testCombineRDate()
{
$rset =new RSet();
$rset->addDate(date_create('1997-09-09 09:00')); // adding out of order
$rset->addDate(date_create('1997-09-09 09:00')); // adding out é order
$rset->addDate('1997-09-04 09:00');
$rset->addDate('1997-09-04 09:00'); // adding a duplicate
@ -172,6 +172,99 @@ class RSetTest extends PHPUnit_Framework_TestCase
), $rset->getOccurrences());
}
///////////////////////////////////////////////////////////////////////////////
// Array access and countable interface
public function testCountable()
{
$rset = new RSet();
$rset->addRRule(array(
'FREQ' => 'YEARLY',
'COUNT' => 6,
'BYDAY' => 'TU, TH',
'DTSTART' => date_create('1997-09-02 09:00')
));
$rset->addExdate('1997-09-04 09:00:00');
$rset->addExdate('1997-09-11 09:00:00');
$rset->addExdate('1997-09-18 09:00:00');
$this->assertEquals(3, count($rset));
}
public function testOffsetExists()
{
$rset = new RSet();
$rset->addRRule(array(
'FREQ' => 'YEARLY',
'COUNT' => 6,
'BYDAY' => 'TU, TH',
'DTSTART' => date_create('1997-09-02 09:00')
));
$rset->addExdate('1997-09-04 09:00:00');
$rset->addExdate('1997-09-11 09:00:00');
$rset->addExdate('1997-09-18 09:00:00');
$this->assertTrue(isset($rset[0]));
$this->assertTrue(isset($rset[1]));
$this->assertTrue(isset($rset['1']));
$this->assertTrue(isset($rset[2]));
$this->assertFalse(isset($rset[3]));
$this->assertFalse(isset($rset['foobar']));
}
public function testOffsetGet()
{
$rset = new RSet();
$rset->addRRule(array(
'FREQ' => 'YEARLY',
'COUNT' => 6,
'BYDAY' => 'TU, TH',
'DTSTART' => date_create('1997-09-02 09:00:00')
));
$rset->addExdate('1997-09-04 09:00:00');
$rset->addExdate('1997-09-11 09:00:00');
$rset->addExdate('1997-09-18 09:00:00');
$this->assertEquals(date_create('1997-09-02 09:00:00'), $rset[0]);
$this->assertEquals(date_create('1997-09-02 09:00:00'), $rset['0']);
$this->assertEquals(date_create('1997-09-09 09:00:00'), $rset[1]);
$this->assertEquals(date_create('1997-09-09 09:00:00'), $rset['1']);
$this->assertEquals(date_create('1997-09-16 09:00:00'), $rset[2]);
$this->assertEquals(null, $rset[3]);
$this->assertEquals(null, $rset['3']);
}
public function illegalOffsets()
{
return array(
array('dtstart'),
array('1dtstart'),
array(array()),
array(1.1),
array(-1),
array(null),
array(new stdClass())
);
}
/**
* @dataProvider illegalOffsets
* @expectedException InvalidArgumentException
*/
public function testOffsetGetInvalidArgument($offset)
{
$rset = new RSet();
$rset->addRRule(array(
'FREQ' => 'YEARLY',
'COUNT' => 6,
'BYDAY' => 'TU, TH',
'DTSTART' => date_create('1997-09-02 09:00:00')
));
$rset[$offset];
}
///////////////////////////////////////////////////////////////////////////////
// Other tests
@ -269,60 +362,6 @@ class RSetTest extends PHPUnit_Framework_TestCase
), $rset->getOccurrences(), 'Iteration works');
}
public function testCountable()
{
$rset = new RSet();
$rset->addRRule(array(
'FREQ' => 'YEARLY',
'COUNT' => 6,
'BYDAY' => 'TU, TH',
'DTSTART' => date_create('1997-09-02 09:00')
));
$rset->addExdate('1997-09-04 09:00:00');
$rset->addExdate('1997-09-11 09:00:00');
$rset->addExdate('1997-09-18 09:00:00');
$this->assertEquals(3, count($rset));
}
public function testOffsetExists()
{
$rset = new RSet();
$rset->addRRule(array(
'FREQ' => 'YEARLY',
'COUNT' => 6,
'BYDAY' => 'TU, TH',
'DTSTART' => date_create('1997-09-02 09:00')
));
$rset->addExdate('1997-09-04 09:00:00');
$rset->addExdate('1997-09-11 09:00:00');
$rset->addExdate('1997-09-18 09:00:00');
$this->assertTrue(isset($rset[0]));
$this->assertTrue(isset($rset[1]));
$this->assertTrue(isset($rset[2]));
$this->assertFalse(isset($rset[3]));
}
public function testOffsetGet()
{
$rset = new RSet();
$rset->addRRule(array(
'FREQ' => 'YEARLY',
'COUNT' => 6,
'BYDAY' => 'TU, TH',
'DTSTART' => date_create('1997-09-02 09:00:00')
));
$rset->addExdate('1997-09-04 09:00:00');
$rset->addExdate('1997-09-11 09:00:00');
$rset->addExdate('1997-09-18 09:00:00');
$this->assertEquals(date_create('1997-09-02 09:00:00'), $rset[0]);
$this->assertEquals(date_create('1997-09-09 09:00:00'), $rset[1]);
$this->assertEquals(date_create('1997-09-16 09:00:00'), $rset[2]);
$this->assertEquals(null, $rset[3]);
}
public function testRSetInRset()
{
$rset = new RSet();