Completed
Push — master ( 1db3cd...632e40 )
by Jeroen
24:52 queued 11:31
created

SeoBundle/Tests/unit/Twig/TwigExtensionTest.php (8 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
            array('getRepository', 'getClassMetadata', 'persist', 'flush'), array(), '', false);
0 ignored issues
show
The call to TwigExtensionTest::createMock() has too many arguments starting with array('getRepository', '...a', 'persist', 'flush').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
30
    }
31
32
    /**
33
     * testShouldReturnNameForEntityWhenNoSEO
34
     */
35 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...
36
    {
37
        $name = 'OK';
38
39
        $this->entityWithName($name);
40
        $this->noSeoFound();
41
42
        $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...
43
44
        $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...
45
46
        $this->assertEquals($name, $result);
47
    }
48
49
    /**
50
     * testShouldReturnNameForEntityWhenSEOWithTitleFound
51
     */
52 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...
53
    {
54
        $nokName = 'NOK';
55
        $name = 'OK';
56
57
        $this->entityWithName($nokName);
58
        $this->seoFoundWithTitle($name);
59
60
        $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...
61
62
        $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...
63
64
        $this->assertEquals($name, $result);
65
    }
66
67
    public function testGetImageDimensionsWithValidFile()
68
    {
69
        $extension = new SeoTwigExtension($this->emMock);
70
71
        $dimensions = $extension->getImageDimensions(__DIR__ . '/../files/150.png');
72
73
        $this->assertSame(['width' => 150, 'height' => 150], $dimensions);
74
    }
75
76
    public function testGetImageDimensionsWithInvalidFile()
77
    {
78
        $extension = new SeoTwigExtension($this->emMock);
79
80
        $dimensions = $extension->getImageDimensions(__DIR__ . '/../files/unkown.png');
81
82
        $this->assertSame(['width' => null, 'height' => null], $dimensions);
83
    }
84
85
    public function testGetImageDimensionsWithCacheServiceAndCachedCall()
86
    {
87
        $cacheMock = $this->createMock(CacheItemPoolInterface::class);
88
        $cacheItemMock = $this->createMock(CacheItemInterface::class);
89
        $cacheItemMock->expects($this->once())->method('isHit')->willReturn(true);
90
        $cacheItemMock->expects($this->once())->method('get')->willReturn([151, 151]);
91
92
        $cacheMock->expects($this->once())->method('getItem')->withAnyParameters()->willReturn($cacheItemMock);
93
94
        $extension = new SeoTwigExtension($this->emMock);
95
        $extension->setRequestCache($cacheMock);
96
97
        $dimensions = $extension->getImageDimensions(__DIR__ . '/../files/150.png');
98
99
        $this->assertSame(['width' => 151, 'height' => 151], $dimensions);
100
    }
101
102
    public function testGetImageDimensionsWithCacheServiceAndNonCachedCall()
103
    {
104
        $cacheMock = $this->createMock(CacheItemPoolInterface::class);
105
        $cacheItemMock = $this->createMock(CacheItemInterface::class);
106
        $cacheItemMock->expects($this->once())->method('isHit')->willReturn(false);
107
        $cacheItemMock->expects($this->once())->method('set')->withAnyParameters();
108
        $cacheItemMock->expects($this->once())->method('get')->willReturn([150, 150]);
109
110
        $cacheMock->expects($this->once())->method('getItem')->withAnyParameters()->willReturn($cacheItemMock);
111
        $cacheMock->expects($this->once())->method('save')->with($cacheItemMock);
112
113
        $extension = new SeoTwigExtension($this->emMock);
114
        $extension->setRequestCache($cacheMock);
115
116
        $dimensions = $extension->getImageDimensions(__DIR__ . '/../files/150.png');
117
118
        $this->assertSame(['width' => 150, 'height' => 150], $dimensions);
119
    }
120
121
    /**
122
     * @param string $name
123
     */
124
    protected function entityWithName($name)
125
    {
126
        $this->entityMock = $this->createMock('Kunstmaan\NodeBundle\Entity\AbstractPage');
127
        $this->entityMock->expects($this->once())->method('getTitle')->will($this->returnValue($name));
128
    }
129
130
    /**
131
     * NoSeoFound
132
     */
133
    protected function noSeoFound()
134
    {
135
        $this->ensureSeoRepoMock();
136
        $this->seoRepoMock->expects($this->once())
137
            ->method('findOrCreateFor')
138
            ->will($this->returnValue(null));
139
140
        $this->wireUpSeoRepo();
141
    }
142
143
    /**
144
     * ensureSeoRepoMock
145
     */
146
    protected function ensureSeoRepoMock()
147
    {
148
        if (is_null($this->seoRepoMock)) {
149
            $this->seoRepoMock = $this->createMock('Kunstmaan\SeoBundle\Repository\SeoRepository', array(), array(), '', false);
150
        }
151
    }
152
153
    /**
154
     * wireUpSeoRepo
155
     */
156
    protected function wireUpSeoRepo()
157
    {
158
        $this->emMock->expects($this->once())
159
            ->method('getRepository')
160
            ->with($this->equalTo('KunstmaanSeoBundle:Seo'))
161
            ->will($this->returnValue($this->seoRepoMock));
162
    }
163
164
    /**
165
     * @param string $title
166
     */
167
    protected function seoFoundWithTitle($title)
168
    {
169
        $this->ensureSeoRepoMock();
170
171
        $seoMock = new Seo();
172
        $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...
173
        $seoMock->setMetaTitle($title);
174
175
        $this->seoRepoMock->expects($this->once())
176
            ->method('findOrCreateFor')
177
            ->will($this->returnValue($seoMock));
178
179
        $this->wireUpSeoRepo();
180
    }
181
}
182