Completed
Push — master ( da34c0...e18438 )
by Tom
05:01 queued 04:53
created

testWillNotValidateOnFieldsCountMismatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineModuleTest\Validator\Adapter;
21
22
use stdClass;
23
use PHPUnit_Framework_TestCase as BaseTestCase;
24
use DoctrineModule\Validator\ObjectExists;
25
26
/**
27
 * Tests for the ObjectExists validator
28
 *
29
 * @license MIT
30
 * @link    http://www.doctrine-project.org/
31
 * @author  Marco Pivetta <[email protected]>
32
 *
33
 * @covers \DoctrineModule\Validator\ObjectExists
34
 */
35
class ObjectExistsTest extends BaseTestCase
36
{
37
    public function testCanValidateWithSingleField()
38
    {
39
        $repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
40
41
        $repository
42
            ->expects($this->exactly(2))
43
            ->method('findOneBy')
44
            ->with(['matchKey' => 'matchValue'])
45
            ->will($this->returnValue(new stdClass()));
46
47
        $validator = new ObjectExists(['object_repository' => $repository, 'fields' => 'matchKey']);
48
49
        $this->assertTrue($validator->isValid('matchValue'));
50
        $this->assertTrue($validator->isValid(['matchKey' => 'matchValue']));
51
    }
52
53
    public function testCanValidateWithMultipleFields()
54
    {
55
        $repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
56
        $repository
57
            ->expects($this->exactly(2))
58
            ->method('findOneBy')
59
            ->with(['firstMatchKey' => 'firstMatchValue', 'secondMatchKey' => 'secondMatchValue'])
60
            ->will($this->returnValue(new stdClass()));
61
62
        $validator = new ObjectExists([
63
            'object_repository' => $repository,
64
            'fields'            => [
65
                'firstMatchKey',
66
                'secondMatchKey',
67
            ],
68
        ]);
69
        $this->assertTrue(
70
            $validator->isValid([
71
                'firstMatchKey'  => 'firstMatchValue',
72
                'secondMatchKey' => 'secondMatchValue',
73
            ])
74
        );
75
        $this->assertTrue($validator->isValid(['firstMatchValue', 'secondMatchValue']));
76
    }
77
78
    public function testCanValidateFalseOnNoResult()
79
    {
80
        $repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
81
        $repository
82
            ->expects($this->once())
83
            ->method('findOneBy')
84
            ->will($this->returnValue(null));
85
86
        $validator = new ObjectExists([
87
            'object_repository' => $repository,
88
            'fields'            => 'field',
89
        ]);
90
        $this->assertFalse($validator->isValid('value'));
91
    }
92
93
    public function testWillRefuseMissingRepository()
94
    {
95
        $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException');
96
97
        new ObjectExists(['fields' => 'field']);
98
    }
99
100
    public function testWillRefuseNonObjectRepository()
101
    {
102
        $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException');
103
104
        new ObjectExists(['object_repository' => 'invalid', 'fields' => 'field']);
105
    }
106
107
    public function testWillRefuseInvalidRepository()
108
    {
109
        $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException');
110
111
        new ObjectExists(['object_repository' => new stdClass(), 'fields' => 'field']);
112
    }
113
114
    public function testWillRefuseMissingFields()
115
    {
116
        $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException');
117
118
        new ObjectExists([
119
            'object_repository' => $this->getMock('Doctrine\Common\Persistence\ObjectRepository'),
120
        ]);
121
    }
122
123
    public function testWillRefuseEmptyFields()
124
    {
125
        $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException');
126
127
        new ObjectExists([
128
            'object_repository' => $this->getMock('Doctrine\Common\Persistence\ObjectRepository'),
129
            'fields'            => [],
130
        ]);
131
    }
132
133
    public function testWillRefuseNonStringFields()
134
    {
135
        $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException');
136
        new ObjectExists([
137
            'object_repository' => $this->getMock('Doctrine\Common\Persistence\ObjectRepository'),
138
            'fields'            => [123],
139
        ]);
140
    }
141
142
    public function testWillNotValidateOnFieldsCountMismatch()
143
    {
144
        $this->setExpectedException(
145
            'Zend\Validator\Exception\RuntimeException',
146
            'Provided values count is 1, while expected number of fields to be matched is 2'
147
        );
148
        $validator = new ObjectExists([
149
            'object_repository' => $this->getMock('Doctrine\Common\Persistence\ObjectRepository'),
150
            'fields'            => ['field1', 'field2'],
151
        ]);
152
        $validator->isValid(['field1Value']);
153
    }
154
155
    public function testWillNotValidateOnFieldKeysMismatch()
156
    {
157
        $this->setExpectedException(
158
            'Zend\Validator\Exception\RuntimeException',
159
            'Field "field2" was not provided, but was expected since the configured field lists needs it for validation'
160
        );
161
162
        $validator = new ObjectExists([
163
            'object_repository' => $this->getMock('Doctrine\Common\Persistence\ObjectRepository'),
164
            'fields'            => ['field1', 'field2'],
165
        ]);
166
167
        $validator->isValid(['field1' => 'field1Value']);
168
    }
169
170
    public function testErrorMessageIsStringInsteadArray()
171
    {
172
        $repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
0 ignored issues
show
Unused Code introduced by
$repository is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
173
        $validator  = new ObjectExists([
174
            'object_repository' => $this->getMock('Doctrine\Common\Persistence\ObjectRepository'),
175
            'fields'            => 'field',
176
        ]);
177
178
        $this->assertFalse($validator->isValid('value'));
179
180
        $messageTemplates = $validator->getMessageTemplates();
181
182
        $expectedMessage = str_replace(
183
            '%value%',
184
            'value',
185
            $messageTemplates[ObjectExists::ERROR_NO_OBJECT_FOUND]
186
        );
187
        $messages        = $validator->getMessages();
188
        $receivedMessage = $messages[ObjectExists::ERROR_NO_OBJECT_FOUND];
189
190
        $this->assertTrue($expectedMessage == $receivedMessage);
191
    }
192
}
193