Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Webmozart\Assert\Tests; Chris@0: Chris@0: use ArrayIterator; Chris@0: use Exception; Chris@0: use Error; Chris@0: use LogicException; Chris@0: use PHPUnit_Framework_TestCase; Chris@0: use RuntimeException; Chris@0: use stdClass; Chris@0: use Webmozart\Assert\Assert; Chris@0: Chris@0: /** Chris@0: * @since 1.0 Chris@0: * Chris@0: * @author Bernhard Schussek Chris@0: */ Chris@0: class AssertTest extends PHPUnit_Framework_TestCase Chris@0: { Chris@0: private static $resource; Chris@0: Chris@0: public static function getResource() Chris@0: { Chris@0: if (!static::$resource) { Chris@0: static::$resource = fopen(__FILE__, 'r'); Chris@0: } Chris@0: Chris@0: return static::$resource; Chris@0: } Chris@0: Chris@0: public static function tearDownAfterClass() Chris@0: { Chris@0: @fclose(self::$resource); Chris@0: } Chris@0: Chris@0: public function getTests() Chris@0: { Chris@0: $resource = self::getResource(); Chris@0: Chris@0: return array( Chris@0: array('string', array('value'), true), Chris@0: array('string', array(''), true), Chris@0: array('string', array(1234), false), Chris@0: array('stringNotEmpty', array('value'), true), Chris@0: array('stringNotEmpty', array(''), false), Chris@0: array('stringNotEmpty', array(1234), false), Chris@0: array('integer', array(123), true), Chris@0: array('integer', array('123'), false), Chris@0: array('integer', array(1.0), false), Chris@0: array('integer', array(1.23), false), Chris@0: array('integerish', array(1.0), true), Chris@0: array('integerish', array(1.23), false), Chris@0: array('integerish', array(123), true), Chris@0: array('integerish', array('123'), true), Chris@0: array('float', array(1.0), true), Chris@0: array('float', array(1.23), true), Chris@0: array('float', array(123), false), Chris@0: array('float', array('123'), false), Chris@0: array('numeric', array(1.0), true), Chris@0: array('numeric', array(1.23), true), Chris@0: array('numeric', array(123), true), Chris@0: array('numeric', array('123'), true), Chris@0: array('numeric', array('foo'), false), Chris@0: array('boolean', array(true), true), Chris@0: array('boolean', array(false), true), Chris@0: array('boolean', array(1), false), Chris@0: array('boolean', array('1'), false), Chris@0: array('scalar', array('1'), true), Chris@0: array('scalar', array(123), true), Chris@0: array('scalar', array(true), true), Chris@0: array('scalar', array(null), false), Chris@0: array('scalar', array(array()), false), Chris@0: array('scalar', array(new stdClass()), false), Chris@0: array('object', array(new stdClass()), true), Chris@0: array('object', array(new RuntimeException()), true), Chris@0: array('object', array(null), false), Chris@0: array('object', array(true), false), Chris@0: array('object', array(1), false), Chris@0: array('object', array(array()), false), Chris@0: array('resource', array($resource), true), Chris@0: array('resource', array($resource, 'stream'), true), Chris@0: array('resource', array($resource, 'other'), false), Chris@0: array('resource', array(1), false), Chris@0: array('isCallable', array('strlen'), true), Chris@0: array('isCallable', array(array($this, 'getTests')), true), Chris@0: array('isCallable', array(function () {}), true), Chris@0: array('isCallable', array(1234), false), Chris@0: array('isCallable', array('foobar'), false), Chris@0: array('isArray', array(array()), true), Chris@0: array('isArray', array(array(1, 2, 3)), true), Chris@0: array('isArray', array(new ArrayIterator(array())), false), Chris@0: array('isArray', array(123), false), Chris@0: array('isArray', array(new stdClass()), false), Chris@0: array('isTraversable', array(array()), true), Chris@0: array('isTraversable', array(array(1, 2, 3)), true), Chris@0: array('isTraversable', array(new ArrayIterator(array())), true), Chris@0: array('isTraversable', array(123), false), Chris@0: array('isTraversable', array(new stdClass()), false), Chris@0: array('isInstanceOf', array(new stdClass(), 'stdClass'), true), Chris@0: array('isInstanceOf', array(new Exception(), 'stdClass'), false), Chris@0: array('isInstanceOf', array(123, 'stdClass'), false), Chris@0: array('isInstanceOf', array(array(), 'stdClass'), false), Chris@0: array('notInstanceOf', array(new stdClass(), 'stdClass'), false), Chris@0: array('notInstanceOf', array(new Exception(), 'stdClass'), true), Chris@0: array('notInstanceOf', array(123, 'stdClass'), true), Chris@0: array('notInstanceOf', array(array(), 'stdClass'), true), Chris@0: array('true', array(true), true), Chris@0: array('true', array(false), false), Chris@0: array('true', array(1), false), Chris@0: array('true', array(null), false), Chris@0: array('false', array(false), true), Chris@0: array('false', array(true), false), Chris@0: array('false', array(1), false), Chris@0: array('false', array(0), false), Chris@0: array('false', array(null), false), Chris@0: array('null', array(null), true), Chris@0: array('null', array(false), false), Chris@0: array('null', array(0), false), Chris@0: array('notNull', array(false), true), Chris@0: array('notNull', array(0), true), Chris@0: array('notNull', array(null), false), Chris@0: array('isEmpty', array(null), true), Chris@0: array('isEmpty', array(false), true), Chris@0: array('isEmpty', array(0), true), Chris@0: array('isEmpty', array(''), true), Chris@0: array('isEmpty', array(1), false), Chris@0: array('isEmpty', array('a'), false), Chris@0: array('notEmpty', array(1), true), Chris@0: array('notEmpty', array('a'), true), Chris@0: array('notEmpty', array(null), false), Chris@0: array('notEmpty', array(false), false), Chris@0: array('notEmpty', array(0), false), Chris@0: array('notEmpty', array(''), false), Chris@0: array('eq', array(1, 1), true), Chris@0: array('eq', array(1, '1'), true), Chris@0: array('eq', array(1, true), true), Chris@0: array('eq', array(1, 0), false), Chris@0: array('notEq', array(1, 0), true), Chris@0: array('notEq', array(1, 1), false), Chris@0: array('notEq', array(1, '1'), false), Chris@0: array('notEq', array(1, true), false), Chris@0: array('same', array(1, 1), true), Chris@0: array('same', array(1, '1'), false), Chris@0: array('same', array(1, true), false), Chris@0: array('same', array(1, 0), false), Chris@0: array('notSame', array(1, 0), true), Chris@0: array('notSame', array(1, 1), false), Chris@0: array('notSame', array(1, '1'), true), Chris@0: array('notSame', array(1, true), true), Chris@0: array('greaterThan', array(1, 0), true), Chris@0: array('greaterThan', array(0, 0), false), Chris@0: array('greaterThanEq', array(2, 1), true), Chris@0: array('greaterThanEq', array(1, 1), true), Chris@0: array('greaterThanEq', array(0, 1), false), Chris@0: array('lessThan', array(0, 1), true), Chris@0: array('lessThan', array(1, 1), false), Chris@0: array('lessThanEq', array(0, 1), true), Chris@0: array('lessThanEq', array(1, 1), true), Chris@0: array('lessThanEq', array(2, 1), false), Chris@0: array('range', array(1, 1, 2), true), Chris@0: array('range', array(2, 1, 2), true), Chris@0: array('range', array(0, 1, 2), false), Chris@0: array('range', array(3, 1, 2), false), Chris@0: array('oneOf', array(1, array(1, 2, 3)), true), Chris@0: array('oneOf', array(1, array('1', '2', '3')), false), Chris@0: array('contains', array('abcd', 'ab'), true), Chris@0: array('contains', array('abcd', 'bc'), true), Chris@0: array('contains', array('abcd', 'cd'), true), Chris@0: array('contains', array('abcd', 'de'), false), Chris@0: array('contains', array('', 'de'), false), Chris@0: array('startsWith', array('abcd', 'ab'), true), Chris@0: array('startsWith', array('abcd', 'bc'), false), Chris@0: array('startsWith', array('', 'bc'), false), Chris@0: array('startsWithLetter', array('abcd'), true), Chris@0: array('startsWithLetter', array('1abcd'), false), Chris@0: array('startsWithLetter', array(''), false), Chris@0: array('endsWith', array('abcd', 'cd'), true), Chris@0: array('endsWith', array('abcd', 'bc'), false), Chris@0: array('endsWith', array('', 'bc'), false), Chris@0: array('regex', array('abcd', '~^ab~'), true), Chris@0: array('regex', array('abcd', '~^bc~'), false), Chris@0: array('regex', array('', '~^bc~'), false), Chris@0: array('alpha', array('abcd'), true), Chris@0: array('alpha', array('ab1cd'), false), Chris@0: array('alpha', array(''), false), Chris@0: array('digits', array('1234'), true), Chris@0: array('digits', array('12a34'), false), Chris@0: array('digits', array(''), false), Chris@0: array('alnum', array('ab12'), true), Chris@0: array('alnum', array('ab12$'), false), Chris@0: array('alnum', array(''), false), Chris@0: array('lower', array('abcd'), true), Chris@0: array('lower', array('abCd'), false), Chris@0: array('lower', array('ab_d'), false), Chris@0: array('lower', array(''), false), Chris@0: array('upper', array('ABCD'), true), Chris@0: array('upper', array('ABcD'), false), Chris@0: array('upper', array('AB_D'), false), Chris@0: array('upper', array(''), false), Chris@0: array('length', array('abcd', 4), true), Chris@0: array('length', array('abc', 4), false), Chris@0: array('length', array('abcde', 4), false), Chris@0: array('length', array('äbcd', 4), true, true), Chris@0: array('length', array('äbc', 4), false, true), Chris@0: array('length', array('äbcde', 4), false, true), Chris@0: array('minLength', array('abcd', 4), true), Chris@0: array('minLength', array('abcde', 4), true), Chris@0: array('minLength', array('abc', 4), false), Chris@0: array('minLength', array('äbcd', 4), true, true), Chris@0: array('minLength', array('äbcde', 4), true, true), Chris@0: array('minLength', array('äbc', 4), false, true), Chris@0: array('maxLength', array('abcd', 4), true), Chris@0: array('maxLength', array('abc', 4), true), Chris@0: array('maxLength', array('abcde', 4), false), Chris@0: array('maxLength', array('äbcd', 4), true, true), Chris@0: array('maxLength', array('äbc', 4), true, true), Chris@0: array('maxLength', array('äbcde', 4), false, true), Chris@0: array('lengthBetween', array('abcd', 3, 5), true), Chris@0: array('lengthBetween', array('abc', 3, 5), true), Chris@0: array('lengthBetween', array('abcde', 3, 5), true), Chris@0: array('lengthBetween', array('ab', 3, 5), false), Chris@0: array('lengthBetween', array('abcdef', 3, 5), false), Chris@0: array('lengthBetween', array('äbcd', 3, 5), true, true), Chris@0: array('lengthBetween', array('äbc', 3, 5), true, true), Chris@0: array('lengthBetween', array('äbcde', 3, 5), true, true), Chris@0: array('lengthBetween', array('äb', 3, 5), false, true), Chris@0: array('lengthBetween', array('äbcdef', 3, 5), false, true), Chris@0: array('fileExists', array(__FILE__), true), Chris@0: array('fileExists', array(__DIR__), true), Chris@0: array('fileExists', array(__DIR__.'/foobar'), false), Chris@0: array('file', array(__FILE__), true), Chris@0: array('file', array(__DIR__), false), Chris@0: array('file', array(__DIR__.'/foobar'), false), Chris@0: array('directory', array(__DIR__), true), Chris@0: array('directory', array(__FILE__), false), Chris@0: array('directory', array(__DIR__.'/foobar'), false), Chris@0: // no tests for readable()/writable() for now Chris@0: array('classExists', array(__CLASS__), true), Chris@0: array('classExists', array(__NAMESPACE__.'\Foobar'), false), Chris@0: array('subclassOf', array(__CLASS__, 'PHPUnit_Framework_TestCase'), true), Chris@0: array('subclassOf', array(__CLASS__, 'stdClass'), false), Chris@0: array('implementsInterface', array('ArrayIterator', 'Traversable'), true), Chris@0: array('implementsInterface', array(__CLASS__, 'Traversable'), false), Chris@0: array('propertyExists', array((object) array('property' => 0), 'property'), true), Chris@0: array('propertyExists', array((object) array('property' => null), 'property'), true), Chris@0: array('propertyExists', array((object) array('property' => null), 'foo'), false), Chris@0: array('propertyNotExists', array((object) array('property' => 0), 'property'), false), Chris@0: array('propertyNotExists', array((object) array('property' => null), 'property'), false), Chris@0: array('propertyNotExists', array((object) array('property' => null), 'foo'), true), Chris@0: array('methodExists', array('RuntimeException', 'getMessage'), true), Chris@0: array('methodExists', array(new RuntimeException(), 'getMessage'), true), Chris@0: array('methodExists', array('stdClass', 'getMessage'), false), Chris@0: array('methodExists', array(new stdClass(), 'getMessage'), false), Chris@0: array('methodExists', array(null, 'getMessage'), false), Chris@0: array('methodExists', array(true, 'getMessage'), false), Chris@0: array('methodExists', array(1, 'getMessage'), false), Chris@0: array('methodNotExists', array('RuntimeException', 'getMessage'), false), Chris@0: array('methodNotExists', array(new RuntimeException(), 'getMessage'), false), Chris@0: array('methodNotExists', array('stdClass', 'getMessage'), true), Chris@0: array('methodNotExists', array(new stdClass(), 'getMessage'), true), Chris@0: array('methodNotExists', array(null, 'getMessage'), true), Chris@0: array('methodNotExists', array(true, 'getMessage'), true), Chris@0: array('methodNotExists', array(1, 'getMessage'), true), Chris@0: array('keyExists', array(array('key' => 0), 'key'), true), Chris@0: array('keyExists', array(array('key' => null), 'key'), true), Chris@0: array('keyExists', array(array('key' => null), 'foo'), false), Chris@0: array('keyNotExists', array(array('key' => 0), 'key'), false), Chris@0: array('keyNotExists', array(array('key' => null), 'key'), false), Chris@0: array('keyNotExists', array(array('key' => null), 'foo'), true), Chris@0: array('count', array(array(0, 1, 2), 3), true), Chris@0: array('count', array(array(0, 1, 2), 2), false), Chris@0: array('uuid', array('00000000-0000-0000-0000-000000000000'), true), Chris@0: array('uuid', array('ff6f8cb0-c57d-21e1-9b21-0800200c9a66'), true), Chris@0: array('uuid', array('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'), true), Chris@0: array('uuid', array('ff6f8cb0-c57d-31e1-9b21-0800200c9a66'), true), Chris@0: array('uuid', array('ff6f8cb0-c57d-41e1-9b21-0800200c9a66'), true), Chris@0: array('uuid', array('ff6f8cb0-c57d-51e1-9b21-0800200c9a66'), true), Chris@0: array('uuid', array('FF6F8CB0-C57D-11E1-9B21-0800200C9A66'), true), Chris@0: array('uuid', array('zf6f8cb0-c57d-11e1-9b21-0800200c9a66'), false), Chris@0: array('uuid', array('af6f8cb0c57d11e19b210800200c9a66'), false), Chris@0: array('uuid', array('ff6f8cb0-c57da-51e1-9b21-0800200c9a66'), false), Chris@0: array('uuid', array('af6f8cb-c57d-11e1-9b21-0800200c9a66'), false), Chris@0: array('uuid', array('3f6f8cb0-c57d-11e1-9b21-0800200c9a6'), false), Chris@0: array('throws', array(function() { throw new LogicException('test'); }, 'LogicException'), true), Chris@0: array('throws', array(function() { throw new LogicException('test'); }, 'IllogicException'), false), Chris@0: array('throws', array(function() { throw new Exception('test'); }), true), Chris@0: array('throws', array(function() { trigger_error('test'); }, 'Throwable'), true, false, 70000), Chris@0: array('throws', array(function() { trigger_error('test'); }, 'Unthrowable'), false, false, 70000), Chris@0: array('throws', array(function() { throw new Error(); }, 'Throwable'), true, true, 70000), Chris@0: ); Chris@0: } Chris@0: Chris@0: public function getMethods() Chris@0: { Chris@0: $methods = array(); Chris@0: Chris@0: foreach ($this->getTests() as $params) { Chris@0: $methods[$params[0]] = array($params[0]); Chris@0: } Chris@0: Chris@0: return array_values($methods); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @dataProvider getTests Chris@0: */ Chris@0: public function testAssert($method, $args, $success, $multibyte = false, $minVersion = null) Chris@0: { Chris@0: if ($minVersion && PHP_VERSION_ID < $minVersion) { Chris@0: $this->markTestSkipped(sprintf('This test requires php %s or upper.', $minVersion)); Chris@0: Chris@0: return; Chris@0: } Chris@0: if ($multibyte && !function_exists('mb_strlen')) { Chris@0: $this->markTestSkipped('The function mb_strlen() is not available'); Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: if (!$success) { Chris@0: $this->setExpectedException('\InvalidArgumentException'); Chris@0: } Chris@0: Chris@0: call_user_func_array(array('Webmozart\Assert\Assert', $method), $args); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @dataProvider getTests Chris@0: */ Chris@0: public function testNullOr($method, $args, $success, $multibyte = false, $minVersion = null) Chris@0: { Chris@0: if ($minVersion && PHP_VERSION_ID < $minVersion) { Chris@0: $this->markTestSkipped(sprintf('This test requires php %s or upper.', $minVersion)); Chris@0: Chris@0: return; Chris@0: } Chris@0: if ($multibyte && !function_exists('mb_strlen')) { Chris@0: $this->markTestSkipped('The function mb_strlen() is not available'); Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: if (!$success && null !== reset($args)) { Chris@0: $this->setExpectedException('\InvalidArgumentException'); Chris@0: } Chris@0: Chris@0: call_user_func_array(array('Webmozart\Assert\Assert', 'nullOr'.ucfirst($method)), $args); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @dataProvider getMethods Chris@0: */ Chris@0: public function testNullOrAcceptsNull($method) Chris@0: { Chris@0: call_user_func(array('Webmozart\Assert\Assert', 'nullOr'.ucfirst($method)), null); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @dataProvider getTests Chris@0: */ Chris@0: public function testAllArray($method, $args, $success, $multibyte = false, $minVersion = null) Chris@0: { Chris@0: if ($minVersion && PHP_VERSION_ID < $minVersion) { Chris@0: $this->markTestSkipped(sprintf('This test requires php %s or upper.', $minVersion)); Chris@0: Chris@0: return; Chris@0: } Chris@0: if ($multibyte && !function_exists('mb_strlen')) { Chris@0: $this->markTestSkipped('The function mb_strlen() is not available'); Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: if (!$success) { Chris@0: $this->setExpectedException('\InvalidArgumentException'); Chris@0: } Chris@0: Chris@0: $arg = array_shift($args); Chris@0: array_unshift($args, array($arg)); Chris@0: Chris@0: call_user_func_array(array('Webmozart\Assert\Assert', 'all'.ucfirst($method)), $args); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @dataProvider getTests Chris@0: */ Chris@0: public function testAllTraversable($method, $args, $success, $multibyte = false, $minVersion = null) Chris@0: { Chris@0: if ($minVersion && PHP_VERSION_ID < $minVersion) { Chris@0: $this->markTestSkipped(sprintf('This test requires php %s or upper.', $minVersion)); Chris@0: Chris@0: return; Chris@0: } Chris@0: if ($multibyte && !function_exists('mb_strlen')) { Chris@0: $this->markTestSkipped('The function mb_strlen() is not available'); Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: if (!$success) { Chris@0: $this->setExpectedException('\InvalidArgumentException'); Chris@0: } Chris@0: Chris@0: $arg = array_shift($args); Chris@0: array_unshift($args, new ArrayIterator(array($arg))); Chris@0: Chris@0: call_user_func_array(array('Webmozart\Assert\Assert', 'all'.ucfirst($method)), $args); Chris@0: } Chris@0: Chris@0: public function getStringConversions() Chris@0: { Chris@0: return array( Chris@0: array('integer', array('foobar'), 'Expected an integer. Got: string'), Chris@0: array('string', array(1), 'Expected a string. Got: integer'), Chris@0: array('string', array(true), 'Expected a string. Got: boolean'), Chris@0: array('string', array(null), 'Expected a string. Got: NULL'), Chris@0: array('string', array(array()), 'Expected a string. Got: array'), Chris@0: array('string', array(new stdClass()), 'Expected a string. Got: stdClass'), Chris@0: array('string', array(self::getResource()), 'Expected a string. Got: resource'), Chris@0: Chris@0: array('eq', array('1', '2'), 'Expected a value equal to "2". Got: "1"'), Chris@0: array('eq', array(1, 2), 'Expected a value equal to 2. Got: 1'), Chris@0: array('eq', array(true, false), 'Expected a value equal to false. Got: true'), Chris@0: array('eq', array(true, null), 'Expected a value equal to null. Got: true'), Chris@0: array('eq', array(null, true), 'Expected a value equal to true. Got: null'), Chris@0: array('eq', array(array(1), array(2)), 'Expected a value equal to array. Got: array'), Chris@0: array('eq', array(new ArrayIterator(array()), new stdClass()), 'Expected a value equal to stdClass. Got: ArrayIterator'), Chris@0: array('eq', array(1, self::getResource()), 'Expected a value equal to resource. Got: 1'), Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @dataProvider getStringConversions Chris@0: */ Chris@0: public function testConvertValuesToStrings($method, $args, $exceptionMessage) Chris@0: { Chris@0: $this->setExpectedException('\InvalidArgumentException', $exceptionMessage); Chris@0: Chris@0: call_user_func_array(array('Webmozart\Assert\Assert', $method), $args); Chris@0: } Chris@0: }