3 namespace Wikimedia\Assert\Test;
6 use PHPUnit_Framework_TestCase;
8 use Wikimedia\Assert\Assert;
9 use Wikimedia\Assert\AssertionException;
10 use Wikimedia\Assert\ParameterAssertionException;
11 use Wikimedia\Assert\ParameterElementTypeException;
12 use Wikimedia\Assert\ParameterTypeException;
15 * @covers Wikimedia\Assert\Assert
18 * @author Daniel Kinzler
19 * @copyright Wikimedia Deutschland e.V.
22 class AssertTest extends PHPUnit_Framework_TestCase {
24 public function testPrecondition_pass() {
25 Assert::precondition( true, 'test' );
28 public function testPrecondition_fail() {
29 $this->setExpectedException( 'Wikimedia\Assert\PreconditionException' );
30 Assert::precondition( false, 'test' );
33 public function testParameter_pass() {
34 Assert::parameter( true, 'foo', 'test' );
37 public function testParameter_fail() {
39 Assert::parameter( false, 'test', 'testing' );
40 } catch ( ParameterAssertionException $ex ) {
41 $this->assertEquals( 'test', $ex->getParameterName() );
45 public function validParameterTypeProvider() {
47 'simple' => array( 'string', 'hello' ),
48 'class' => array( 'RuntimeException', new RuntimeException() ),
49 'multi' => array( 'string|array|Closure', function() {} ),
50 'null' => array( 'integer|null', null ),
51 'callable' => array( 'null|callable', 'time' ),
56 * @dataProvider validParameterTypeProvider
58 public function testParameterType_pass( $type, $value ) {
59 Assert::parameterType( $type, $value, 'test' );
62 public function invalidParameterTypeProvider() {
64 'simple' => array( 'string', 5 ),
65 'class' => array( 'RuntimeException', new LogicException() ),
66 'multi' => array( 'string|integer|Closure', array() ),
67 'null' => array( 'integer|string', null ),
68 'callable' => array( 'null|callable', array() ),
73 * @dataProvider invalidParameterTypeProvider
75 public function testParameterType_fail( $type, $value ) {
77 Assert::parameterType( $type, $value, 'test' );
78 $this->fail( 'Expected ParameterTypeException' );
79 } catch ( ParameterTypeException $ex ) {
80 $this->assertEquals( $type, $ex->getParameterType() );
81 $this->assertEquals( 'test', $ex->getParameterName() );
85 public function testParameterType_catch() {
87 Assert::parameterType( 'string', 17, 'test' );
88 $this->fail( 'Expected exception' );
89 } catch ( AssertionException $ex ) {
94 public function validParameterElementTypeProvider() {
96 'empty' => array( 'string', array() ),
97 'simple' => array( 'string', array( 'hello', 'world' ) ),
98 'class' => array( 'RuntimeException', array( new RuntimeException() ) ),
99 'multi' => array( 'string|array|Closure', array( array(), function() {} ) ),
100 'null' => array( 'integer|null', array( null, 3, null ) ),
105 * @dataProvider validParameterElementTypeProvider
107 public function testParameterElementType_pass( $type, $value ) {
108 Assert::parameterElementType( $type, $value, 'test' );
111 public function invalidParameterElementTypeProvider() {
113 'simple' => array( 'string', array( 'hello', 5 ) ),
114 'class' => array( 'RuntimeException', array( new LogicException() ) ),
115 'multi' => array( 'string|array|Closure', array( array(), function() {}, 5 ) ),
116 'null' => array( 'integer|string', array( null, 3, null ) ),
121 * @dataProvider invalidParameterElementTypeProvider
123 public function testParameterElementType_fail( $type, $value ) {
125 Assert::parameterElementType( $type, $value, 'test' );
126 $this->fail( 'Expected ParameterElementTypeException' );
127 } catch ( ParameterElementTypeException $ex ) {
128 $this->assertEquals( $type, $ex->getElementType() );
129 $this->assertEquals( 'test', $ex->getParameterName() );
133 public function testParameterElementType_bad() {
134 $this->setExpectedException( 'Wikimedia\Assert\ParameterTypeException' );
135 Assert::parameterElementType( 'string', 'foo', 'test' );
138 public function testInvariant_pass() {
139 Assert::invariant( true, 'test' );
142 public function testInvariant_fail() {
143 $this->setExpectedException( 'Wikimedia\Assert\InvariantException' );
144 Assert::invariant( false, 'test' );
147 public function testPostcondition_pass() {
148 Assert::postcondition( true, 'test' );
151 public function testPostcondition_fail() {
152 $this->setExpectedException( 'Wikimedia\Assert\PostconditionException' );
153 Assert::postcondition( false, 'test' );