1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\NewsBundle\Tests\Model; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Sonata\NewsBundle\Model\PostInterface; |
18
|
|
|
use Sonata\NewsBundle\Permalink\DatePermalink; |
19
|
|
|
|
20
|
|
|
class DatePermalinkTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
public function testGenerate(): void |
23
|
|
|
{ |
24
|
|
|
$post = $this->createMock(PostInterface::class); |
25
|
|
|
$post->expects($this->any())->method('getSlug')->willReturn('the-slug'); |
26
|
|
|
$post->expects($this->any())->method('getYear')->willReturn('2011'); |
27
|
|
|
$post->expects($this->any())->method('getMonth')->willReturn('12'); |
28
|
|
|
$post->expects($this->any())->method('getDay')->willReturn('30'); |
29
|
|
|
|
30
|
|
|
$permalink = new DatePermalink(); |
31
|
|
|
$this->assertSame('2011/12/30/the-slug', $permalink->generate($post)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testCustomFormatting(): void |
35
|
|
|
{ |
36
|
|
|
$post = $this->createMock(PostInterface::class); |
37
|
|
|
$post->expects($this->any())->method('getSlug')->willReturn('the-slug'); |
38
|
|
|
$post->expects($this->any())->method('getYear')->willReturn('2011'); |
39
|
|
|
$post->expects($this->any())->method('getMonth')->willReturn('2'); |
40
|
|
|
$post->expects($this->any())->method('getDay')->willReturn('01'); |
41
|
|
|
|
42
|
|
|
$permalink = new DatePermalink('%1$02d/%2$02d/%3$02d/%4$s'); |
43
|
|
|
$this->assertSame('2011/02/01/the-slug', $permalink->generate($post)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testGetParameters(): void |
47
|
|
|
{ |
48
|
|
|
$permalink = new DatePermalink(); |
49
|
|
|
$expected = [ |
50
|
|
|
'year' => 2011, |
51
|
|
|
'month' => 12, |
52
|
|
|
'day' => 30, |
53
|
|
|
'slug' => 'the-slug', |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
$this->assertSame($expected, $permalink->getParameters('2011/12/30/the-slug')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGetParametersWithWrongUrl(): void |
60
|
|
|
{ |
61
|
|
|
$this->expectException('InvalidArgumentException'); |
62
|
|
|
|
63
|
|
|
$permalink = new DatePermalink(); |
64
|
|
|
$expected = [ |
65
|
|
|
'year' => '2011', |
66
|
|
|
'month' => '12', |
67
|
|
|
'day' => '30', |
68
|
|
|
'slug' => 'the-slug', |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
$this->assertSame($expected, $permalink->getParameters('2011/12/the-slug')); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|