Completed
Pull Request — master (#564)
by Michał
07:58
created

ObjectExistsTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 9
Bugs 3 Features 0
Metric Value
wmc 12
c 9
b 3
f 0
lcom 1
cbo 4
dl 0
loc 152
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanValidateWithSingleField() 0 15 1
A testCanValidateWithMultipleFields() 0 19 1
A testCanValidateFalseOnNoResult() 0 14 1
A testWillRefuseMissingRepository() 0 6 1
A testWillRefuseNonObjectRepository() 0 6 1
A testWillRefuseInvalidRepository() 0 6 1
A testWillRefuseMissingFields() 0 8 1
A testWillRefuseEmptyFields() 0 9 1
A testWillRefuseNonStringFields() 0 9 1
A testWillNotValidateOnFieldsCountMismatch() 0 11 1
A testWillNotValidateOnFieldKeysMismatch() 0 14 1
A testErrorMessageIsStringInsteadArray() 0 21 1
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 Doctrine\Common\Persistence\ObjectRepository;
23
use DoctrineModule\Validator\ObjectExists;
24
use Zend\Validator\Exception;
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 \PHPUnit_Framework_TestCase
36
{
37
    public function testCanValidateWithSingleField()
38
    {
39
        $repository = $this->createMock(ObjectRepository::class);
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->createMock(ObjectRepository::class);
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'            => ['firstMatchKey', 'secondMatchKey'],
65
        ]);
66
        $this->assertTrue($validator->isValid([
67
            'firstMatchKey'  => 'firstMatchValue',
68
            'secondMatchKey' => 'secondMatchValue',
69
        ]));
70
        $this->assertTrue($validator->isValid(['firstMatchValue', 'secondMatchValue']));
71
    }
72
73
    public function testCanValidateFalseOnNoResult()
74
    {
75
        $repository = $this->createMock(ObjectRepository::class);
76
        $repository
77
            ->expects($this->once())
78
            ->method('findOneBy')
79
            ->will($this->returnValue(null));
80
81
        $validator = new ObjectExists([
82
            'object_repository' => $repository,
83
            'fields'            => 'field',
84
        ]);
85
        $this->assertFalse($validator->isValid('value'));
86
    }
87
88
    public function testWillRefuseMissingRepository()
89
    {
90
        $this->expectException(Exception\InvalidArgumentException::class);
91
92
        new ObjectExists(['fields' => 'field']);
93
    }
94
95
    public function testWillRefuseNonObjectRepository()
96
    {
97
        $this->expectException(Exception\InvalidArgumentException::class);
98
99
        new ObjectExists(['object_repository' => 'invalid', 'fields' => 'field']);
100
    }
101
102
    public function testWillRefuseInvalidRepository()
103
    {
104
        $this->expectException(Exception\InvalidArgumentException::class);
105
106
        new ObjectExists(['object_repository' => new \stdClass(), 'fields' => 'field']);
107
    }
108
109
    public function testWillRefuseMissingFields()
110
    {
111
        $this->expectException(Exception\InvalidArgumentException::class);
112
113
        new ObjectExists([
114
            'object_repository' => $this->createMock(ObjectRepository::class),
115
        ]);
116
    }
117
118
    public function testWillRefuseEmptyFields()
119
    {
120
        $this->expectException(Exception\InvalidArgumentException::class);
121
122
        new ObjectExists([
123
            'object_repository' => $this->createMock(ObjectRepository::class),
124
            'fields'            => [],
125
        ]);
126
    }
127
128
    public function testWillRefuseNonStringFields()
129
    {
130
        $this->expectException(Exception\InvalidArgumentException::class);
131
132
        new ObjectExists([
133
            'object_repository' => $this->createMock(ObjectRepository::class),
134
            'fields'            => [123],
135
        ]);
136
    }
137
138
    public function testWillNotValidateOnFieldsCountMismatch()
139
    {
140
        $this->expectException(Exception\RuntimeException::class);
141
        $this->expectExceptionMessage('Provided values count is 1, while expected number of fields to be matched is 2');
142
143
        $validator = new ObjectExists([
144
            'object_repository' => $this->createMock(ObjectRepository::class),
145
            'fields'            => ['field1', 'field2'],
146
        ]);
147
        $validator->isValid(['field1Value']);
148
    }
149
150
    public function testWillNotValidateOnFieldKeysMismatch()
151
    {
152
        $this->expectException(Exception\RuntimeException::class);
153
        $this->expectExceptionMessage(
154
            'Field "field2" was not provided, but was expected since the configured field lists needs it for validation'
155
        );
156
157
        $validator = new ObjectExists([
158
            'object_repository' => $this->createMock(ObjectRepository::class),
159
            'fields'            => ['field1', 'field2'],
160
        ]);
161
162
        $validator->isValid(['field1' => 'field1Value']);
163
    }
164
165
    public function testErrorMessageIsStringInsteadArray()
166
    {
167
        $validator  = new ObjectExists([
168
            'object_repository' => $this->createMock(ObjectRepository::class),
169
            'fields'            => 'field',
170
        ]);
171
172
        $this->assertFalse($validator->isValid('value'));
173
174
        $messageTemplates = $validator->getMessageTemplates();
175
176
        $expectedMessage = str_replace(
177
            '%value%',
178
            'value',
179
            $messageTemplates[ObjectExists::ERROR_NO_OBJECT_FOUND]
180
        );
181
        $messages        = $validator->getMessages();
182
        $receivedMessage = $messages[ObjectExists::ERROR_NO_OBJECT_FOUND];
183
184
        $this->assertTrue($expectedMessage == $receivedMessage);
185
    }
186
}
187