RedirectManagerTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Zenstruck\RedirectBundle\Tests\Service;
4
5
use PHPUnit\Framework\TestCase;
6
use Zenstruck\RedirectBundle\Service\RedirectManager;
7
use Zenstruck\RedirectBundle\Tests\Fixture\Bundle\Entity\DummyRedirect;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
class RedirectManagerTest extends TestCase
13
{
14
    public const REDIRECT_DUMMY_CLASS = 'Zenstruck\RedirectBundle\Tests\Fixture\Bundle\Entity\DummyRedirect';
15
16
    private $om;
17
18
    private $repository;
19
20
    /** @var RedirectManager */
21
    private $redirectManager;
22
23
    protected function setUp(): void
24
    {
25
        $this->om = $this->createMock('Doctrine\Persistence\ObjectManager');
26
        $this->repository = $this->createMock('Doctrine\Persistence\ObjectRepository');
27
28
        $this->om->expects($this->once())
29
            ->method('getRepository')
30
            ->with($this->equalTo(self::REDIRECT_DUMMY_CLASS))
31
            ->willReturn($this->repository)
32
        ;
33
34
        $this->redirectManager = new RedirectManager(self::REDIRECT_DUMMY_CLASS, $this->om);
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function update_redirect()
41
    {
42
        $redirect = new DummyRedirect('/foo', '/bar');
43
        $redirect->increaseCount(5);
44
        $this->assertNull($redirect->getLastAccessed());
45
46
        $this->om->expects($this->once())
47
            ->method('flush')
48
        ;
49
50
        $this->repository->expects($this->once())
51
            ->method('findOneBy')
52
            ->with(['source' => '/foo'])
53
            ->willReturn($redirect)
54
        ;
55
56
        $redirect = $this->redirectManager->findAndUpdate('/foo');
57
58
        $this->assertSame(6, $redirect->getCount());
59
        $this->assertEqualsWithDelta(\time(), $redirect->getLastAccessed()->format('U'), 1);
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function no_redirect_found()
66
    {
67
        $this->om->expects($this->never())
68
            ->method('flush')
69
        ;
70
71
        $this->repository->expects($this->once())
72
            ->method('findOneBy')
73
            ->with(['source' => '/foo'])
74
            ->willReturn(null)
75
        ;
76
77
        $redirect = $this->redirectManager->findAndUpdate('/foo');
78
79
        $this->assertNull($redirect);
80
    }
81
}
82