Completed
Push — master ( e0017c...6b1304 )
by Tom
14s queued 11s
created

DoctrineModuleTest/Validator/ObjectExistsTest.php (10 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineModuleTest\Validator\Adapter;
6
7
use DoctrineModule\Validator\ObjectExists;
8
use PHPUnit\Framework\TestCase as BaseTestCase;
9
use stdClass;
10
use function str_replace;
11
12
/**
13
 * Tests for the ObjectExists validator
14
 *
15
 * @link    http://www.doctrine-project.org/
16
 *
17
 * @covers \DoctrineModule\Validator\ObjectExists
18
 */
19
class ObjectExistsTest extends BaseTestCase
20
{
21
    public function testCanValidateWithSingleField() : void
22
    {
23
        $repository = $this->createMock('Doctrine\Persistence\ObjectRepository');
24
25
        $repository
26
            ->expects($this->exactly(2))
27
            ->method('findOneBy')
28
            ->with(['matchKey' => 'matchValue'])
29
            ->will($this->returnValue(new stdClass()));
30
31
        $validator = new ObjectExists(['object_repository' => $repository, 'fields' => 'matchKey']);
32
33
        $this->assertTrue($validator->isValid('matchValue'));
34
        $this->assertTrue($validator->isValid(['matchKey' => 'matchValue']));
35
    }
36
37
    public function testCanValidateWithMultipleFields() : void
38
    {
39
        $repository = $this->createMock('Doctrine\Persistence\ObjectRepository');
40
        $repository
41
            ->expects($this->exactly(2))
42
            ->method('findOneBy')
43
            ->with(['firstMatchKey' => 'firstMatchValue', 'secondMatchKey' => 'secondMatchValue'])
44
            ->will($this->returnValue(new stdClass()));
45
46
        $validator = new ObjectExists([
47
            'object_repository' => $repository,
48
            'fields'            => [
49
                'firstMatchKey',
50
                'secondMatchKey',
51
            ],
52
        ]);
53
        $this->assertTrue(
54
            $validator->isValid([
55
                'firstMatchKey'  => 'firstMatchValue',
56
                'secondMatchKey' => 'secondMatchValue',
57
            ])
58
        );
59
        $this->assertTrue($validator->isValid(['firstMatchValue', 'secondMatchValue']));
60
    }
61
62
    public function testCanValidateFalseOnNoResult() : void
63
    {
64
        $repository = $this->createMock('Doctrine\Persistence\ObjectRepository');
65
        $repository
66
            ->expects($this->once())
67
            ->method('findOneBy')
68
            ->will($this->returnValue(null));
69
70
        $validator = new ObjectExists([
71
            'object_repository' => $repository,
72
            'fields'            => 'field',
73
        ]);
74
        $this->assertFalse($validator->isValid('value'));
75
    }
76
77
    public function testWillRefuseMissingRepository() : void
78
    {
79
        $this->expectException('Laminas\Validator\Exception\InvalidArgumentException');
0 ignored issues
show
The method expectException() does not seem to exist on object<DoctrineModuleTes...apter\ObjectExistsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
81
        new ObjectExists(['fields' => 'field']);
82
    }
83
84
    public function testWillRefuseNonObjectRepository() : void
85
    {
86
        $this->expectException('Laminas\Validator\Exception\InvalidArgumentException');
0 ignored issues
show
The method expectException() does not seem to exist on object<DoctrineModuleTes...apter\ObjectExistsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
88
        new ObjectExists(['object_repository' => 'invalid', 'fields' => 'field']);
89
    }
90
91
    public function testWillRefuseInvalidRepository() : void
92
    {
93
        $this->expectException('Laminas\Validator\Exception\InvalidArgumentException');
0 ignored issues
show
The method expectException() does not seem to exist on object<DoctrineModuleTes...apter\ObjectExistsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
95
        new ObjectExists(['object_repository' => new stdClass(), 'fields' => 'field']);
96
    }
97
98
    public function testWillRefuseMissingFields() : void
99
    {
100
        $this->expectException('Laminas\Validator\Exception\InvalidArgumentException');
0 ignored issues
show
The method expectException() does not seem to exist on object<DoctrineModuleTes...apter\ObjectExistsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
102
        new ObjectExists([
103
            'object_repository' => $this->createMock('Doctrine\Persistence\ObjectRepository'),
104
        ]);
105
    }
106
107
    public function testWillRefuseEmptyFields() : void
108
    {
109
        $this->expectException('Laminas\Validator\Exception\InvalidArgumentException');
0 ignored issues
show
The method expectException() does not seem to exist on object<DoctrineModuleTes...apter\ObjectExistsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
111
        new ObjectExists([
112
            'object_repository' => $this->createMock('Doctrine\Persistence\ObjectRepository'),
113
            'fields'            => [],
114
        ]);
115
    }
116
117
    public function testWillRefuseNonStringFields() : void
118
    {
119
        $this->expectException('Laminas\Validator\Exception\InvalidArgumentException');
0 ignored issues
show
The method expectException() does not seem to exist on object<DoctrineModuleTes...apter\ObjectExistsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
        new ObjectExists([
121
            'object_repository' => $this->createMock('Doctrine\Persistence\ObjectRepository'),
122
            'fields'            => [123],
123
        ]);
124
    }
125
126
    public function testWillNotValidateOnFieldsCountMismatch() : void
127
    {
128
        $this->expectException(
0 ignored issues
show
The method expectException() does not seem to exist on object<DoctrineModuleTes...apter\ObjectExistsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
            'Laminas\Validator\Exception\RuntimeException'
130
        );
131
        $this->expectExceptionMessage(
0 ignored issues
show
The method expectExceptionMessage() does not seem to exist on object<DoctrineModuleTes...apter\ObjectExistsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
            'Provided values count is 1, while expected number of fields to be matched is 2'
133
        );
134
        $validator = new ObjectExists([
135
            'object_repository' => $this->createMock('Doctrine\Persistence\ObjectRepository'),
136
            'fields'            => ['field1', 'field2'],
137
        ]);
138
        $validator->isValid(['field1Value']);
139
    }
140
141
    public function testWillNotValidateOnFieldKeysMismatch() : void
142
    {
143
        $this->expectException(
0 ignored issues
show
The method expectException() does not seem to exist on object<DoctrineModuleTes...apter\ObjectExistsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
            'Laminas\Validator\Exception\RuntimeException'
145
        );
146
        $this->expectExceptionMessage(
0 ignored issues
show
The method expectExceptionMessage() does not seem to exist on object<DoctrineModuleTes...apter\ObjectExistsTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
147
            'Field "field2" was not provided, but was expected since the configured field lists needs it for validation'
148
        );
149
150
        $validator = new ObjectExists([
151
            'object_repository' => $this->createMock('Doctrine\Persistence\ObjectRepository'),
152
            'fields'            => ['field1', 'field2'],
153
        ]);
154
155
        $validator->isValid(['field1' => 'field1Value']);
156
    }
157
158
    public function testErrorMessageIsStringInsteadArray() : void
159
    {
160
        $repository = $this->createMock('Doctrine\Persistence\ObjectRepository');
161
        $validator  = new ObjectExists([
162
            'object_repository' => $this->createMock('Doctrine\Persistence\ObjectRepository'),
163
            'fields'            => 'field',
164
        ]);
165
166
        $this->assertFalse($validator->isValid('value'));
167
168
        $messageTemplates = $validator->getMessageTemplates();
169
170
        $expectedMessage = str_replace(
171
            '%value%',
172
            'value',
173
            $messageTemplates[ObjectExists::ERROR_NO_OBJECT_FOUND]
174
        );
175
        $messages        = $validator->getMessages();
176
        $receivedMessage = $messages[ObjectExists::ERROR_NO_OBJECT_FOUND];
177
178
        $this->assertSame($expectedMessage, $receivedMessage);
179
    }
180
}
181