-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8b225fe
Showing
12 changed files
with
488 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/vendor/ | ||
composer.lock | ||
coverage.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
language: php | ||
php: | ||
- 5.4 | ||
- 5.5 | ||
- 5.6 | ||
- 7.0 | ||
- 7.1 | ||
|
||
script: | ||
- vendor/bin/tester tests -s -p php | ||
- php temp/code-checker/src/code-checker.php | ||
|
||
after_failure: | ||
# Print *.actual content | ||
- for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done | ||
|
||
before_script: | ||
# Install Nette Tester | ||
- travis_retry composer install --no-interaction --prefer-dist | ||
- travis_retry composer create-project nette/code-checker temp/code-checker ~2.5.0 | ||
|
||
sudo: false | ||
|
||
cache: | ||
directories: | ||
- $HOME/.composer/cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
tester = vendor/bin/tester | ||
tests_dir = tests/ | ||
coverage_name = $(tests_dir)coverage.html | ||
php_ini = $(tests_dir)php-unix.ini | ||
php_bin = php | ||
|
||
.PHONY: test coverage clean | ||
test: | ||
@$(tester) -p $(php_bin) -c $(php_ini) $(tests_dir) | ||
|
||
coverage: | ||
@$(tester) -p $(php_bin) -c $(php_ini) --coverage $(coverage_name) --coverage-src src/ $(tests_dir) | ||
|
||
clean: | ||
@rm -f $(coverage_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "czproject/assert", | ||
"type": "library", | ||
"description": "Assert helper.", | ||
"license": "BSD-3-Clause", | ||
"authors": [ | ||
{ | ||
"name": "Jan Pecha", | ||
"homepage": "https://www.janpecha.cz/" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.4.0" | ||
}, | ||
"autoload": { | ||
"classmap": ["src/"] | ||
}, | ||
"require-dev": { | ||
"nette/tester": "^1.7" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
New BSD License | ||
--------------- | ||
|
||
Copyright © 2017 Jan Pecha (https://www.janpecha.cz/) All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright notice, this | ||
list of conditions and the following disclaimer in the documentation and/or | ||
other materials provided with the distribution. | ||
* Neither the name of “CzProject“ nor the names of its contributors may be used to | ||
endorse or promote products derived from this software without specific prior | ||
written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
# CzProject\Assert | ||
|
||
Assert helper, throws exceptions. | ||
|
||
|
||
Installation | ||
------------ | ||
|
||
[Download a latest package](https://github.com/czproject/assert/releases) or use [Composer](http://getcomposer.org/): | ||
|
||
``` | ||
composer require czproject/assert | ||
``` | ||
|
||
`CzProject\Assert` requires PHP 5.4.0 or later. | ||
|
||
|
||
## Usage | ||
|
||
|
||
``` php | ||
<?php | ||
|
||
use CzProject\Assert\Assert; | ||
|
||
function add($a, $b) | ||
{ | ||
Assert::int($a); | ||
Assert::int($b); | ||
return $a + $b; | ||
} | ||
``` | ||
|
||
* `assert($value, $msg = NULL)` - checks if value is `TRUE` | ||
* `int($value, $msg = NULL)` - checks if value is `int` | ||
* `intOrNull($value, $msg = NULL)` - checks if value is `int|NULL` | ||
* `string($value, $msg = NULL)` - checks if value is `string` | ||
* `stringOrNull($value, $msg = NULL)` - checks if value is `string|NULL` | ||
* `type($value, $type, $msg = NULL)` - checks if value is instance of given type | ||
* `in($value, $arr, $msg = NULL)` - checks if value is in array | ||
|
||
------------------------------ | ||
|
||
License: [New BSD License](license.md) | ||
<br>Author: Jan Pecha, https://www.janpecha.cz/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
namespace CzProject\Assert; | ||
|
||
|
||
class Assert | ||
{ | ||
public function __construct() | ||
{ | ||
throw new StaticClassException('This is static class.'); | ||
} | ||
|
||
|
||
/** | ||
* Checks if value is TRUE. | ||
* @param mixed | ||
* @param string|NULL | ||
* @return void | ||
* @throws AssertException | ||
* @tracySkipLocation | ||
*/ | ||
public static function assert($value, $msg = NULL) | ||
{ | ||
if ($value !== TRUE) { | ||
throw new AssertException(self::message($msg, 'TRUE', $value)); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Checks if value is integer | ||
* @param mixed | ||
* @param string|NULL | ||
* @return void | ||
* @throws AssertException | ||
* @tracySkipLocation | ||
*/ | ||
public static function int($value, $msg = NULL) | ||
{ | ||
self::assert(is_int($value), self::message($msg, 'int', $value)); | ||
} | ||
|
||
|
||
/** | ||
* Checks if value is integer or NULL | ||
* @param mixed | ||
* @param string|NULL | ||
* @return void | ||
* @throws AssertException | ||
* @tracySkipLocation | ||
*/ | ||
public static function intOrNull($value, $msg = NULL) | ||
{ | ||
self::assert($value === NULL || is_int($value), self::message($msg, 'int|NULL', $value)); | ||
} | ||
|
||
|
||
/** | ||
* Checks if value is object of given type | ||
* @param mixed | ||
* @param string | ||
* @param string|NULL | ||
* @return void | ||
* @throws AssertException | ||
* @tracySkipLocation | ||
*/ | ||
public static function type($value, $type, $msg = NULL) | ||
{ | ||
self::assert($value instanceof $type, self::message($msg, $type, $value)); | ||
} | ||
|
||
|
||
/** | ||
* Checks if value is string | ||
* @param mixed | ||
* @param string|NULL | ||
* @return void | ||
* @throws AssertException | ||
* @tracySkipLocation | ||
*/ | ||
public static function string($value, $msg = NULL) | ||
{ | ||
self::assert(is_string($value), self::message($msg, 'string', $value)); | ||
} | ||
|
||
|
||
/** | ||
* Checks if value is string or NULL | ||
* @param mixed | ||
* @param string|NULL | ||
* @return void | ||
* @throws AssertException | ||
* @tracySkipLocation | ||
*/ | ||
public static function stringOrNull($value, $msg = NULL) | ||
{ | ||
self::assert($value === NULL || is_string($value), self::message($msg, 'string|NULL', $value)); | ||
} | ||
|
||
|
||
/** | ||
* Checks if value is in haystack | ||
* @param mixed | ||
* @param array | ||
* @param string|NULL | ||
* @return void | ||
* @throws AssertException | ||
* @tracySkipLocation | ||
*/ | ||
public static function in($value, array $haystack, $msg = NULL) | ||
{ | ||
self::assert(in_array($value, $haystack, TRUE), self::message($msg, 'value from specific range', $value)); | ||
} | ||
|
||
|
||
/** | ||
* @param string|NULL | ||
* @param string | ||
* @param mixed | ||
* @return string | ||
*/ | ||
private static function message($msg, $expected, $value) | ||
{ | ||
if ($msg !== NULL) { | ||
return $msg; | ||
} | ||
|
||
return "Invalid value type - expected $expected, " . (is_object($value) ? get_class($value) : gettype($value)) . ' given.'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace CzProject\Assert; | ||
|
||
|
||
class Exception extends \Exception | ||
{ | ||
} | ||
|
||
|
||
class AssertException extends Exception | ||
{ | ||
} | ||
|
||
|
||
class StaticClassException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
use CzProject\Assert\Assert as Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
test(function () { | ||
|
||
Tester\Assert::exception(function () { | ||
new Assert; | ||
}, 'CzProject\Assert\StaticClassException'); | ||
|
||
}); | ||
|
||
|
||
test(function () { | ||
|
||
Assert::assert(TRUE); | ||
|
||
Tester\Assert::exception(function () { | ||
Assert::assert(FALSE); | ||
}, 'CzProject\Assert\AssertException'); | ||
|
||
}); | ||
|
||
|
||
test(function () { | ||
|
||
Assert::int(1000); | ||
|
||
Tester\Assert::exception(function () { | ||
Assert::int('1000'); | ||
}, 'CzProject\Assert\AssertException'); | ||
|
||
}); | ||
|
||
|
||
test(function () { | ||
|
||
Assert::intOrNull(1000); | ||
Assert::intOrNull(NULL); | ||
|
||
Tester\Assert::exception(function () { | ||
Assert::intOrNull('1000'); | ||
}, 'CzProject\Assert\AssertException'); | ||
|
||
}); | ||
|
||
|
||
test(function () { | ||
|
||
Assert::string('1000'); | ||
|
||
Tester\Assert::exception(function () { | ||
Assert::string(1000); | ||
}, 'CzProject\Assert\AssertException'); | ||
|
||
}); | ||
|
||
|
||
test(function () { | ||
|
||
Assert::stringOrNull('1000'); | ||
Assert::stringOrNull(NULL); | ||
|
||
Tester\Assert::exception(function () { | ||
Assert::stringOrNull(1000); | ||
}, 'CzProject\Assert\AssertException'); | ||
|
||
}); | ||
|
||
|
||
test(function () { | ||
|
||
Assert::type(new \DateTime('UTC'), 'DateTime'); | ||
|
||
Tester\Assert::exception(function () { | ||
Assert::type(new \DateTime('UTC'), 'AnotherDateTime'); | ||
}, 'CzProject\Assert\AssertException'); | ||
|
||
}); | ||
|
||
|
||
test(function () { | ||
|
||
Assert::in(10, array(20, 15, 10, 5, 0)); | ||
|
||
Tester\Assert::exception(function () { | ||
Assert::in('10', array(20, 15, 10, 5, 0)); | ||
}, 'CzProject\Assert\AssertException'); | ||
|
||
}); |
Oops, something went wrong.