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

Validator/NoObjectExistsTest.php (1 issue)

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
0 ignored issues
show
The method expects cannot be called on $repository (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
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())
52
            ->method('findOneBy')
53
            ->will($this->returnValue(new stdClass()));
54
        $validator = new NoObjectExists(['object_repository' => $repository, 'fields' => 'matchKey']);
55
56
        $this->assertFalse($validator->isValid('matchValue'));
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