]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - vendor/wikimedia/assert/tests/phpunit/AssertTest.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / vendor / wikimedia / assert / tests / phpunit / AssertTest.php
1 <?php
2
3 namespace Wikimedia\Assert\Test;
4
5 use LogicException;
6 use PHPUnit_Framework_TestCase;
7 use RuntimeException;
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;
13
14 /**
15  * @covers Wikimedia\Assert\Assert
16  *
17  * @license MIT
18  * @author Daniel Kinzler
19  * @copyright Wikimedia Deutschland e.V.
20  */
21
22 class AssertTest extends PHPUnit_Framework_TestCase {
23
24         public function testPrecondition_pass() {
25                 Assert::precondition( true, 'test' );
26         }
27
28         public function testPrecondition_fail() {
29                 $this->setExpectedException( 'Wikimedia\Assert\PreconditionException' );
30                 Assert::precondition( false, 'test' );
31         }
32
33         public function testParameter_pass() {
34                 Assert::parameter( true, 'foo', 'test' );
35         }
36
37         public function testParameter_fail() {
38                 try {
39                         Assert::parameter( false, 'test', 'testing' );
40                 } catch ( ParameterAssertionException $ex ) {
41                         $this->assertEquals( 'test', $ex->getParameterName() );
42                 }
43         }
44
45         public function validParameterTypeProvider() {
46                 return array(
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' ),
52                 );
53         }
54
55         /**
56          * @dataProvider validParameterTypeProvider
57          */
58         public function testParameterType_pass( $type, $value ) {
59                 Assert::parameterType( $type, $value, 'test' );
60         }
61
62         public function invalidParameterTypeProvider() {
63                 return array(
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() ),
69                 );
70         }
71
72         /**
73          * @dataProvider invalidParameterTypeProvider
74          */
75         public function testParameterType_fail( $type, $value ) {
76                 try {
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() );
82                 }
83         }
84
85         public function testParameterType_catch() {
86                 try {
87                         Assert::parameterType( 'string', 17, 'test' );
88                         $this->fail( 'Expected exception' );
89                 } catch ( AssertionException $ex ) {
90                         // ok
91                 }
92         }
93
94         public function validParameterElementTypeProvider() {
95                 return array(
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 ) ),
101                 );
102         }
103
104         /**
105          * @dataProvider validParameterElementTypeProvider
106          */
107         public function testParameterElementType_pass( $type, $value ) {
108                 Assert::parameterElementType( $type, $value, 'test' );
109         }
110
111         public function invalidParameterElementTypeProvider() {
112                 return array(
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 ) ),
117                 );
118         }
119
120         /**
121          * @dataProvider invalidParameterElementTypeProvider
122          */
123         public function testParameterElementType_fail( $type, $value ) {
124                 try {
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() );
130                 }
131         }
132
133         public function testParameterElementType_bad() {
134                 $this->setExpectedException( 'Wikimedia\Assert\ParameterTypeException' );
135                 Assert::parameterElementType( 'string', 'foo', 'test' );
136         }
137
138         public function testInvariant_pass() {
139                 Assert::invariant( true, 'test' );
140         }
141
142         public function testInvariant_fail() {
143                 $this->setExpectedException( 'Wikimedia\Assert\InvariantException' );
144                 Assert::invariant( false, 'test' );
145         }
146
147         public function testPostcondition_pass() {
148                 Assert::postcondition( true, 'test' );
149         }
150
151         public function testPostcondition_fail() {
152                 $this->setExpectedException( 'Wikimedia\Assert\PostconditionException' );
153                 Assert::postcondition( false, 'test' );
154         }
155
156 }