-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathUniqueUrlValidatorTest.php
137 lines (105 loc) · 4.68 KB
/
UniqueUrlValidatorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\PageBundle\Tests\Validator;
use Sonata\PageBundle\Model\PageInterface;
use Sonata\PageBundle\Model\PageManagerInterface;
use Sonata\PageBundle\Model\SiteInterface;
use Sonata\PageBundle\Validator\Constraints\UniqueUrl;
use Sonata\PageBundle\Validator\UniqueUrlValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
final class UniqueUrlValidatorTest extends ConstraintValidatorTestCase
{
/**
* @var PageManagerInterface
*/
protected $manager;
protected function setUp(): void
{
$this->manager = $this->createMock(PageManagerInterface::class);
parent::setUp();
}
public function testValidateWithoutSite(): void
{
$page = $this->createMock(PageInterface::class);
$page->expects(static::exactly(1))->method('getSite')->willReturn(null);
$page->expects(static::never())->method('isError');
$this->manager->expects(static::never())->method('fixUrl');
$this->manager->expects(static::never())->method('findBy');
$this->validator->validate($page, new UniqueUrl());
$this->buildViolation('error.uniq_url.no_site')
->atPath($this->propertyPath.'.site')
->assertRaised();
}
/**
* @group legacy
*/
public function testValidateWithNoPageFound(): void
{
$site = $this->createMock(SiteInterface::class);
$page = $this->createMock(PageInterface::class);
$page->expects(static::exactly(2))->method('getSite')->willReturn($site);
$page->expects(static::exactly(2))->method('isError')->willReturn(false);
$this->manager->expects(static::once())->method('fixUrl');
$this->manager->expects(static::once())->method('findBy')->willReturn([$page]);
$this->validator->validate($page, new UniqueUrl());
$this->assertNoViolation();
}
public function testValidateWithPageFound(): void
{
$url = '/salut';
$site = $this->createMock(SiteInterface::class);
$page = $this->createMock(PageInterface::class);
$page->expects(static::exactly(2))->method('getSite')->willReturn($site);
$page->expects(static::exactly(2))->method('isError')->willReturn(false);
$page->method('getUrl')->willReturn($url);
$pageFound = $this->createMock(PageInterface::class);
$pageFound->method('getUrl')->willReturn($url);
$this->manager->expects(static::once())->method('fixUrl');
$this->manager->expects(static::once())->method('findBy')->willReturn([$page, $pageFound]);
$this->validator->validate($page, new UniqueUrl());
$this->buildViolation('error.uniq_url')
->setParameter('%url%', $url)
->atPath($this->propertyPath.'.url')
->assertRaised();
}
public function testValidateWithRootUrlAndNoParent(): void
{
$site = $this->createMock(SiteInterface::class);
$page = $this->createMock(PageInterface::class);
$page->expects(static::exactly(2))->method('getSite')->willReturn($site);
$page->expects(static::exactly(2))->method('isError')->willReturn(false);
$page->expects(static::once())->method('getParent')->willReturn(null);
$page->method('getUrl')->willReturn('/');
$pageFound = $this->createMock(PageInterface::class);
$pageFound->method('getUrl')->willReturn('/');
$this->manager->expects(static::once())->method('fixUrl');
$this->manager->expects(static::once())->method('findBy')->willReturn([$page, $pageFound]);
$this->validator->validate($page, new UniqueUrl());
$this->buildViolation('error.uniq_url.parent_unselect')
->atPath($this->propertyPath.'.parent')
->assertRaised();
}
public function testValidateWithPageDynamic(): void
{
$site = $this->createMock(SiteInterface::class);
$page = $this->createMock(PageInterface::class);
$page->expects(static::once())->method('getSite')->willReturn($site);
$page->expects(static::once())->method('isError')->willReturn(false);
$page->expects(static::once())->method('isDynamic')->willReturn(true);
$page->method('getUrl')->willReturn('/salut');
$this->validator->validate($page, new UniqueUrl());
$this->assertNoViolation();
}
protected function createValidator(): UniqueUrlValidator
{
return new UniqueUrlValidator($this->manager);
}
}