NoObjectExistsTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 54
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanValidateWithNoAvailableObjectInRepository() 0 13 1
A testCannotValidateWithAvailableObjectInRepository() 0 13 1
A testErrorMessageIsStringInsteadArray() 0 23 1
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())
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