Skip to content
Browse files

Initial commit Assert

  • Loading branch information...
1 parent c00b3fb commit 9ccbe3b3a931a44ffa6ba4be85c4d8bf4ce4ff41 @beberlei committed May 19, 2012
Showing with 128 additions and 16 deletions.
  1. +1 −0 .gitignore
  2. +17 −16 README
  3. +9 −0 composer.json
  4. +6 −0 composer.lock
  5. +30 −0 lib/Assert/Assertion.php
  6. +19 −0 lib/Assert/InvalidArgumentException.php
  7. +8 −0 phpunit.xml.dist
  8. +27 −0 tests/Assert/Tests/AssertTest.php
  9. +11 −0 tests/bootstrap.php
View
1 .gitignore
@@ -0,0 +1 @@
+vendor
View
33 README
@@ -5,7 +5,8 @@ There is usually lots of duplicate code necessary for this kind of code
and php's assert() method is not desired here, because its clumsy to
work with inline. This library is similar to the assert functionality of PHPUnit.
-Idea is to reduce the
+Idea is to reduce the amount of code for implementing assertions
+in your model and also reduce/simplify the code paths.
Rule of engagement:
@@ -14,19 +15,19 @@ You can pass next argument to control the message, the message code is encoded i
assertion itself.
<?php
- Assert\Assert::true($value);
- Assert\Assert::false($value);
- Assert\Assert::integer($value);
- Assert\Assert::double($value);
- Assert\Assert::boolean($value);
- Assert\Assert::scalar($value);
- Assert\Assert::instanceOf($value, $className);
- Assert\Assert::subclassOf($value, $className);
- Assert\Assert::notEmpty($value);
- Assert\Assert::notNull($value);
- Assert\Assert::regexp($value);
- Assert\Assert::numeric($value);
- Assert\Assert::keyIsset($array, $key);
- Assert\Assert::keyExists($array, $key);
- Assert\Assert::arrayx($array);
+ use Assert\Assertion;
+
+ Assertion::integer($value);
+ Assertion::double($value);
+ Assertion::boolean($value);
+ Assertion::scalar($value);
+ Assertion::instanceOf($value, $className);
+ Assertion::subclassOf($value, $className);
+ Assertion::notEmpty($value);
+ Assertion::notNull($value);
+ Assertion::regexp($value);
+ Assertion::numeric($value);
+ Assertion::keyIsset($array, $key);
+ Assertion::keyExists($array, $key);
+ Assertion::arrayx($array);
View
9 composer.json
@@ -0,0 +1,9 @@
+{
+ "name": "beberlei/assert",
+ "description": "Assertion library",
+ "autoload": {
+ "psr-0": {
+ "Assert": "lib/"
+ }
+ }
+}
View
6 composer.lock
@@ -0,0 +1,6 @@
+{
+ "hash": "40f892fb4d2ba34beafa93933bf5b67d",
+ "packages": [],
+ "packages-dev": null,
+ "aliases": []
+}
View
30 lib/Assert/Assertion.php
@@ -0,0 +1,30 @@
+<?php
+/**
+ * Assert
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to kontakt@beberlei.de so I can send you a copy immediately.
+ */
+
+namespace Assert;
+
+/**
+ * Assert library
+ */
+class Assertion
+{
+ const INVALID_INTEGER = 10;
+
+ static public function integer($value, $message = null)
+ {
+ if (!is_int($value)) {
+ throw new InvalidArgumentException($message, self::INVALID_INTEGER);
+ }
+ }
+}
+
View
19 lib/Assert/InvalidArgumentException.php
@@ -0,0 +1,19 @@
+<?php
+/**
+ * Assert
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to kontakt@beberlei.de so I can send you a copy immediately.
+ */
+
+namespace Assert;
+
+class InvalidArgumentException extends \InvalidArgumentException
+{
+}
+
View
8 phpunit.xml.dist
@@ -0,0 +1,8 @@
+<?xml version="1.0" ?>
+<phpunit bootstrap="tests/bootstrap.php">
+ <testsuites>
+ <testsuite name="Assertion">
+ <directory suffix="Test.php">./tests</directory>
+ </testsuite>
+ </testsuites>
+</phpunit>
View
27 tests/Assert/Tests/AssertTest.php
@@ -0,0 +1,27 @@
+<?php
+namespace Assert\Tests;
+
+use Assert\Assertion;
+
+class AssertTest extends \PHPUnit_Framework_TestCase
+{
+ static public function dataInvalidInteger()
+ {
+ return array(
+ array("1"),
+ array(1.23),
+ array(false),
+ array("test"),
+ );
+ }
+
+ /**
+ * @dataProvider dataInvalidInteger
+ */
+ public function testInvalidInteger($nonInteger)
+ {
+ $this->setExpectedException('Assert\InvalidArgumentException', null, Assertion::INVALID_INTEGER);
+ Assertion::integer($nonInteger);
+ }
+}
+
View
11 tests/bootstrap.php
@@ -0,0 +1,11 @@
+<?php
+
+$loader = @include __DIR__ . '/../vendor/autoload.php';
+if (!$loader) {
+ die(<<<'EOT'
+You must set up the project dependencies, run the following commands:
+wget http://getcomposer.org/composer.phar
+php composer.phar install
+EOT
+ );
+}

0 comments on commit 9ccbe3b

Please sign in to comment.
Something went wrong with that request. Please try again.