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

Validator/NoObjectExistsTest.php (3 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\NoObjectExists;
8
use PHPUnit\Framework\TestCase as BaseTestCase;
9
use stdClass;
10
use function str_replace;
11
12
/**
13
 * Tests for the NoObjectExists tests
14
 *
15
 * @link    http://www.doctrine-project.org/
16
 */
17
class NoObjectExistsTest extends BaseTestCase
18
{
19
    public function testCanValidateWithNoAvailableObjectInRepository() : void
20
    {
21
        $repository = $this->createMock('Doctrine\Persistence\ObjectRepository');
22
23
        $repository
24
            ->expects($this->once())
25
            ->method('findOneBy')
26
            ->will($this->returnValue(null));
27
28
        $validator = new NoObjectExists(['object_repository' => $repository, 'fields' => 'matchKey']);
29
30
        $this->assertTrue($validator->isValid('matchValue'));
31
    }
32
33
    public function testCannotValidateWithAvailableObjectInRepository() : void
34
    {
35
        $repository = $this->createMock('Doctrine\Persistence\ObjectRepository');
36
37
        $repository
38
            ->expects($this->once())
39
            ->method('findOneBy')
40
            ->will($this->returnValue(new stdClass()));
41
42
        $validator = new NoObjectExists(['object_repository' => $repository, 'fields' => 'matchKey']);
43
44
        $this->assertFalse($validator->isValid('matchValue'));
45
    }
46
47
    public function testErrorMessageIsStringInsteadArray() : void
48
    {
49
        $repository = $this->createMock('Doctrine\Persistence\ObjectRepository');
50
        $repository
51
            ->expects($this->once())
0 ignored issues
show
The method once() does not seem to exist on object<DoctrineModuleTes...ter\NoObjectExistsTest>.

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...
52
            ->method('findOneBy')
53
            ->will($this->returnValue(new stdClass()));
0 ignored issues
show
The method returnValue() does not seem to exist on object<DoctrineModuleTes...ter\NoObjectExistsTest>.

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...
54
        $validator = new NoObjectExists(['object_repository' => $repository, 'fields' => 'matchKey']);
55
56
        $this->assertFalse($validator->isValid('matchValue'));
0 ignored issues
show
The method assertFalse() does not seem to exist on object<DoctrineModuleTes...ter\NoObjectExistsTest>.

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...
57
58
        $messageTemplates = $validator->getMessageTemplates();
59
60
        $expectedMessage = str_replace(
61
            '%value%',
62
            'matchValue',
63
            $messageTemplates[NoObjectExists::ERROR_OBJECT_FOUND]
64
        );
65
        $messages        = $validator->getMessages();
66
        $receivedMessage = $messages[NoObjectExists::ERROR_OBJECT_FOUND];
67
68
        $this->assertSame($expectedMessage, $receivedMessage);
69
    }
70
}
71