Completed
Push — master ( 70cb44...646998 )
by Ivannis Suárez
05:28
created

ValidatorTests::testConstraints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Cubiche/Validator component.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Core\Validator\Tests\Units;
12
13
use Cubiche\Core\Validator\Assert;
14
use Cubiche\Core\Validator\Exception\ValidationException;
15
use Cubiche\Core\Validator\Tests\Fixtures\Blog;
16
use Cubiche\Core\Validator\Tests\Fixtures\Post;
17
use Cubiche\Core\Validator\Validator;
18
use Cubiche\Core\Validator\ValidatorInterface;
19
20
/**
21
 * ValidatorTests class.
22
 *
23
 * Generated by TestGenerator on 2016-05-18 at 15:26:52.
24
 */
25
class ValidatorTests extends TestCase
26
{
27
    /**
28
     * @return Validator
29
     */
30
    public function creaateValidator()
31
    {
32
        return Validator::create();
33
    }
34
35
    /**
36
     * Test create method.
37
     */
38
    public function testCreate()
39
    {
40
        $this
41
            ->given($validator = $this->creaateValidator())
42
            ->then()
43
                ->object($validator)
44
                    ->isInstanceOf(ValidatorInterface::class)
45
        ;
46
    }
47
48
    /**
49
     * Test assert method.
50
     */
51 View Code Duplication
    public function testAssertExplicitConstraints()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $this
54
            ->given($validator = $this->creaateValidator())
55
            ->then()
56
                ->boolean($validator->assert('ivannis', Assert::alnum()->noWhitespace()->length(1, 15)))
57
                    ->isTrue()
58
                ->boolean($validator->assert('ivannis', Assert::alnum()->noWhitespace()->length(1, 15), 'foo'))
59
                    ->isTrue()
60
                ->exception(function () use ($validator) {
61
                    $validator->assert('some tests', Assert::alnum()->noWhitespace()->length(1, 15));
62
                })->isInstanceOf(ValidationException::class)
63
                ->exception(function () use ($validator) {
64
                    $validator->assert('value');
65
                })->isInstanceOf(\RuntimeException::class)
66
        ;
67
    }
68
69
    /**
70
     * Test assert method.
71
     */
72
    public function testAssertObject()
73
    {
74
        $this
75
            ->given($validator = $this->creaateValidator())
76
            ->and($post = new Post('title', 'some content'))
77
            ->and($post1 = new Post())
78
            ->and($post2 = new Post(3, 10))
79
            ->and($blog = new Blog())
80
            ->then()
81
                ->boolean($validator->assert($post))
82
                    ->isTrue()
83
                ->boolean($validator->assert($post, null, 'bar'))
84
                    ->isTrue()
85
                ->exception(function () use ($validator, $post1) {
86
                    $validator->assert($post1);
87
                })->isInstanceOf(ValidationException::class)
88
                ->exception(function () use ($validator, $post2) {
89
                    $validator->assert($post2);
90
                })->isInstanceOf(ValidationException::class)
91
                ->boolean($validator->assert($post2, null, 'foo'))
92
                    ->isTrue()
93
                ->boolean($validator->assert($blog))
94
                    ->isTrue()
95
                ->boolean($validator->assert($blog, null, 'foo'))
96
                    ->isTrue()
97
        ;
98
    }
99
100
    /**
101
     * Test assert method.
102
     */
103
    public function testAssertArrayObject()
104
    {
105
        $this
106
            ->given($validator = $this->creaateValidator())
107
            ->and($post = new Post('title', 'some content'))
108
            ->and($post1 = new Post())
109
            ->and($post2 = new Post(3, 10))
110
            ->and($blog = new Blog())
111
            ->and($blog1 = new Blog())
112
            ->then()
113
                ->boolean($validator->assert([$post, [$blog, $blog1]]))
114
                    ->isTrue()
115
                ->boolean($validator->assert([$post, $blog], null, 'bar'))
116
                    ->isTrue()
117
                ->exception(function () use ($validator, $post1, $blog) {
118
                    $validator->assert([$post1, $blog, 300]);
119
                })->isInstanceOf(\RuntimeException::class)
120
                ->exception(function () use ($validator, $post1, $blog) {
121
                    $validator->assert([$post1, $blog]);
122
                })->isInstanceOf(ValidationException::class)
123
                ->exception(function () use ($validator, $post2, $blog) {
124
                    $validator->assert([$post2, $blog]);
125
                })->isInstanceOf(ValidationException::class)
126
                ->boolean($validator->assert([$post2, $blog], null, 'foo'))
127
                    ->isTrue()
128
        ;
129
    }
130
131
    /**
132
     * Test validate method.
133
     */
134 View Code Duplication
    public function testValidateExplicitConstraints()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
    {
136
        $this
137
            ->given($validator = $this->creaateValidator())
138
            ->then()
139
                ->boolean($validator->validate('ivannis', Assert::alnum()->noWhitespace()->length(1, 15)))
140
                    ->isTrue()
141
                ->boolean($validator->validate('ivannis', Assert::alnum()->noWhitespace()->length(1, 15), 'foo'))
142
                    ->isTrue()
143
                ->boolean($validator->validate('some tests', Assert::alnum()->noWhitespace()->length(1, 15)))
144
                    ->isFalse()
145
                ->exception(function () use ($validator) {
146
                    $validator->validate('value');
147
                })->isInstanceOf(\RuntimeException::class)
148
        ;
149
    }
150
151
    /**
152
     * Test validate method.
153
     */
154
    public function testValidateObject()
155
    {
156
        $this
157
            ->given($validator = $this->creaateValidator())
158
            ->and($post = new Post('title', 'some content'))
159
            ->and($post1 = new Post())
160
            ->and($post2 = new Post(3, 10))
161
            ->and($blog = new Blog())
162
            ->then()
163
                ->boolean($validator->validate($post))
164
                    ->isTrue()
165
                ->boolean($validator->validate($post, null, 'bar'))
166
                    ->isTrue()
167
                ->boolean($validator->validate($post1))
168
                    ->isFalse()
169
                ->boolean($validator->validate($post2))
170
                    ->isFalse()
171
                ->boolean($validator->validate($post2, null, 'foo'))
172
                    ->isTrue()
173
                ->boolean($validator->validate($blog))
174
                    ->isTrue()
175
                ->boolean($validator->validate($blog, null, 'foo'))
176
                    ->isTrue()
177
        ;
178
    }
179
180
    /**
181
     * Test validate method.
182
     */
183
    public function testValidateArrayObject()
184
    {
185
        $this
186
            ->given($validator = $this->creaateValidator())
187
            ->and($post = new Post('title', 'some content'))
188
            ->and($post1 = new Post())
189
            ->and($post2 = new Post(3, 10))
190
            ->and($blog = new Blog())
191
            ->and($blog1 = new Blog())
192
            ->then()
193
                ->boolean($validator->validate([$post, [$blog, $blog1]]))
194
                    ->isTrue()
195
                ->boolean($validator->validate([$post, $blog], null, 'bar'))
196
                    ->isTrue()
197
                ->exception(function () use ($validator, $post1, $blog) {
198
                    $validator->validate([$post1, $blog, 300]);
199
                })->isInstanceOf(\RuntimeException::class)
200
                ->boolean($validator->validate([$post1, $blog]))
201
                    ->isFalse()
202
                ->boolean($validator->validate([$post2, $blog]))
203
                    ->isFalse()
204
                ->boolean($validator->validate([$post2, $blog], null, 'foo'))
205
                    ->isTrue()
206
        ;
207
    }
208
}
209