diff options
author | matt <matt@twilio.com> | 2016-08-15 21:07:40 -0700 |
---|---|---|
committer | matt <matt@twilio.com> | 2016-08-15 21:07:40 -0700 |
commit | e841d0b348d2a0770180a4050ad661faa65ae3db (patch) | |
tree | 54cdfb10e3291972853e75b19756054f6929e29c | |
parent | c735e12e3c82679b7864888c992ae9bfb1b8ae80 (diff) | |
download | twilio-php-e841d0b348d2a0770180a4050ad661faa65ae3db.zip twilio-php-e841d0b348d2a0770180a4050ad661faa65ae3db.tar.gz twilio-php-e841d0b348d2a0770180a4050ad661faa65ae3db.tar.bz2 |
Fix readLimits logic for selecting best pageSize
* pageSize should be the smaller of the limit and the MAX_PAGE_SIZE
* pageSize should always be clamped to the MAX_PAGE_SIZE
* Test Suite with assertions about how `readLimits` should work.
-rw-r--r-- | Twilio/Tests/Unit/VersionTest.php | 92 | ||||
-rw-r--r-- | Twilio/Version.php | 4 |
2 files changed, 95 insertions, 1 deletions
diff --git a/Twilio/Tests/Unit/VersionTest.php b/Twilio/Tests/Unit/VersionTest.php new file mode 100644 index 0000000..8241d47 --- /dev/null +++ b/Twilio/Tests/Unit/VersionTest.php @@ -0,0 +1,92 @@ +<?php + + +namespace Twilio\Tests\Unit; + +use Twilio\Domain; +use Twilio\Rest\Client; +use Twilio\Values; +use Twilio\Version; + +class TestDomain extends Domain {} +class TestVersion extends Version {} + +class VersionTest extends UnitTest { + + /** + * @param $message + * @param $limit + * @param $pageSize + * @param $expectedLimit + * @param $expectedPageSize + * @param $expectedPageLimit + * @dataProvider readLimitProvider + */ + public function testReadLimits($message, $limit, $pageSize, $expectedLimit, $expectedPageSize, $expectedPageLimit) { + $version = new TestVersion(new TestDomain(new Client('username', 'password'))); + $actual = $version->readLimits($limit, $pageSize); + $this->assertEquals($expectedLimit, $actual['limit'], "$message: Limit does not match"); + $this->assertEquals($expectedPageSize, $actual['pageSize'], "$message: PageSize does not match"); + $this->assertEquals($expectedPageLimit, $actual['pageLimit'], "$message: PageLimit does not match"); + } + + public function readLimitProvider() { + return array( + array( + 'Nothing Specified', + null, null, + Values::NONE, Values::NONE, Values::NONE, + ), + array( + 'Limit Specified - Under Max Page Size', + Version::MAX_PAGE_SIZE - 1, null, + Version::MAX_PAGE_SIZE - 1, Version::MAX_PAGE_SIZE - 1, 1, + ), + array( + 'Limit Specified - At Max Page Size', + Version::MAX_PAGE_SIZE, null, + Version::MAX_PAGE_SIZE, Version::MAX_PAGE_SIZE, 1, + ), + array( + 'Limit Specified - Over Max Page Size', + Version::MAX_PAGE_SIZE + 1, null, + Version::MAX_PAGE_SIZE + 1, Version::MAX_PAGE_SIZE, 2, + ), + array( + 'Page Size Specified - Under Max Page Size', + null, Version::MAX_PAGE_SIZE - 1, + Values::NONE, Version::MAX_PAGE_SIZE - 1, Values::NONE, + ), + array( + 'Page Size Specified - At Max Page Size', + null, Version::MAX_PAGE_SIZE, + Values::NONE, Version::MAX_PAGE_SIZE, Values::NONE, + ), + array( + 'Page Size Specified - Over Max Page Size', + null, Version::MAX_PAGE_SIZE + 1, + Values::NONE, Version::MAX_PAGE_SIZE, Values::NONE + ), + array( + 'Limit less than Page Size', + 50, 100, + 50, 100, 1, + ), + array( + 'Limit equal to Page Size', + 100, 100, + 100, 100, 1, + ), + array( + 'Limit greater than Page Size - evenly divisible', + 100, 50, + 100, 50, 2, + ), + array( + 'Limit greater than Page Size - not evenly divisible', + 100, 30, + 100, 30, 4 + ), + ); + } +}
\ No newline at end of file diff --git a/Twilio/Version.php b/Twilio/Version.php index eabdd39..3e64431 100644 --- a/Twilio/Version.php +++ b/Twilio/Version.php @@ -155,12 +155,14 @@ abstract class Version { $pageLimit = Values::NONE; if ($limit) { - if ($pageSize) { + if (is_null($pageSize)) { $pageSize = min($limit, self::MAX_PAGE_SIZE); } $pageLimit = (int)(ceil($limit / (float)$pageSize)); } + $pageSize = min($pageSize, self::MAX_PAGE_SIZE); + return array( 'limit' => $limit ?: Values::NONE, 'pageSize' => $pageSize ?: Values::NONE, |