Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

SeoBundle/Tests/unit/Twig/TwigExtensionTest.php (7 issues)

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
namespace Kunstmaan\SeoBundle\Tests\Entity;
4
5
use Kunstmaan\SeoBundle\Entity\Seo;
6
use Kunstmaan\SeoBundle\Twig\SeoTwigExtension;
7
use PHPUnit\Framework\TestCase;
8
use Psr\Cache\CacheItemInterface;
9
use Psr\Cache\CacheItemPoolInterface;
10
11
/**
12
 * Class TwigExtensionTests
13
 */
14
class TwigExtensionTest extends TestCase
15
{
16
    protected $emMock;
17
18
    protected $entityMock;
19
20
    protected $seoRepoMock;
21
22
    /**
23
     * Sets up the fixture, for example, opens a network connection.
24
     * This method is called before a test is executed.
25
     */
26
    protected function setUp()
27
    {
28
        $this->emMock = $this->createMock('\Doctrine\ORM\EntityManager');
29
    }
30
31
    /**
32
     * testShouldReturnNameForEntityWhenNoSEO
33
     */
34 View Code Duplication
    public function testShouldReturnNameForEntityWhenNoSEO()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        $name = 'OK';
37
38
        $this->entityWithName($name);
39
        $this->noSeoFound();
40
41
        $object = new SeoTwigExtension($this->emMock);
0 ignored issues
show
$this->emMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManager>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
43
        $result = $object->getTitleFor($this->entityMock);
0 ignored issues
show
$this->entityMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\NodeBundle\Entity\AbstractPage>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
45
        $this->assertEquals($name, $result);
46
    }
47
48
    /**
49
     * testShouldReturnNameForEntityWhenSEOWithTitleFound
50
     */
51 View Code Duplication
    public function testShouldReturnNameForEntityWhenSEOWithTitleFound()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $nokName = 'NOK';
54
        $name = 'OK';
55
56
        $this->entityWithName($nokName);
57
        $this->seoFoundWithTitle($name);
58
59
        $object = new SeoTwigExtension($this->emMock);
0 ignored issues
show
$this->emMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManager>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
61
        $result = $object->getTitleFor($this->entityMock);
0 ignored issues
show
$this->entityMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\NodeBundle\Entity\AbstractPage>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
63
        $this->assertEquals($name, $result);
64
    }
65
66
    public function testGetImageDimensionsWithValidFile()
67
    {
68
        $extension = new SeoTwigExtension($this->emMock);
69
70
        $dimensions = $extension->getImageDimensions(__DIR__ . '/../files/150.png');
71
72
        $this->assertSame(['width' => 150, 'height' => 150], $dimensions);
73
    }
74
75
    public function testGetImageDimensionsWithInvalidFile()
76
    {
77
        $extension = new SeoTwigExtension($this->emMock);
78
79
        $dimensions = $extension->getImageDimensions(__DIR__ . '/../files/unkown.png');
80
81
        $this->assertSame(['width' => null, 'height' => null], $dimensions);
82
    }
83
84
    public function testGetImageDimensionsWithCacheServiceAndCachedCall()
85
    {
86
        $cacheMock = $this->createMock(CacheItemPoolInterface::class);
87
        $cacheItemMock = $this->createMock(CacheItemInterface::class);
88
        $cacheItemMock->expects($this->once())->method('isHit')->willReturn(true);
89
        $cacheItemMock->expects($this->once())->method('get')->willReturn([151, 151]);
90
91
        $cacheMock->expects($this->once())->method('getItem')->withAnyParameters()->willReturn($cacheItemMock);
92
93
        $extension = new SeoTwigExtension($this->emMock);
94
        $extension->setRequestCache($cacheMock);
95
96
        $dimensions = $extension->getImageDimensions(__DIR__ . '/../files/150.png');
97
98
        $this->assertSame(['width' => 151, 'height' => 151], $dimensions);
99
    }
100
101
    public function testGetImageDimensionsWithCacheServiceAndNonCachedCall()
102
    {
103
        $cacheMock = $this->createMock(CacheItemPoolInterface::class);
104
        $cacheItemMock = $this->createMock(CacheItemInterface::class);
105
        $cacheItemMock->expects($this->once())->method('isHit')->willReturn(false);
106
        $cacheItemMock->expects($this->once())->method('set')->withAnyParameters();
107
        $cacheItemMock->expects($this->once())->method('get')->willReturn([150, 150]);
108
109
        $cacheMock->expects($this->once())->method('getItem')->withAnyParameters()->willReturn($cacheItemMock);
110
        $cacheMock->expects($this->once())->method('save')->with($cacheItemMock);
111
112
        $extension = new SeoTwigExtension($this->emMock);
113
        $extension->setRequestCache($cacheMock);
114
115
        $dimensions = $extension->getImageDimensions(__DIR__ . '/../files/150.png');
116
117
        $this->assertSame(['width' => 150, 'height' => 150], $dimensions);
118
    }
119
120
    /**
121
     * @param string $name
122
     */
123
    protected function entityWithName($name)
124
    {
125
        $this->entityMock = $this->createMock('Kunstmaan\NodeBundle\Entity\AbstractPage');
126
        $this->entityMock->expects($this->once())->method('getTitle')->willReturn($name);
127
    }
128
129
    /**
130
     * NoSeoFound
131
     */
132
    protected function noSeoFound()
133
    {
134
        $this->ensureSeoRepoMock();
135
        $this->seoRepoMock->expects($this->once())
136
            ->method('findOrCreateFor')
137
            ->willReturn(null);
138
139
        $this->wireUpSeoRepo();
140
    }
141
142
    /**
143
     * ensureSeoRepoMock
144
     */
145
    protected function ensureSeoRepoMock()
146
    {
147
        if (\is_null($this->seoRepoMock)) {
148
            $this->seoRepoMock = $this->createMock('Kunstmaan\SeoBundle\Repository\SeoRepository');
149
        }
150
    }
151
152
    /**
153
     * wireUpSeoRepo
154
     */
155
    protected function wireUpSeoRepo()
156
    {
157
        $this->emMock->expects($this->once())
158
            ->method('getRepository')
159
            ->with($this->equalTo('KunstmaanSeoBundle:Seo'))
160
            ->willReturn($this->seoRepoMock);
161
    }
162
163
    /**
164
     * @param string $title
165
     */
166
    protected function seoFoundWithTitle($title)
167
    {
168
        $this->ensureSeoRepoMock();
169
170
        $seoMock = new Seo();
171
        $seoMock->setRef($this->entityMock);
0 ignored issues
show
$this->entityMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\AdminBu...\Entity\AbstractEntity>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
172
        $seoMock->setMetaTitle($title);
173
174
        $this->seoRepoMock->expects($this->once())
175
            ->method('findOrCreateFor')
176
            ->willReturn($seoMock);
177
178
        $this->wireUpSeoRepo();
179
    }
180
}
181