Completed
Pull Request — master (#121)
by Alejandro
08:44
created

updateMetadataByShortCodeUpdatesProvidedData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Core\Service;
5
6
use Doctrine\ORM\EntityManagerInterface;
7
use Doctrine\ORM\EntityRepository;
8
use PHPUnit\Framework\TestCase;
9
use Prophecy\Argument;
10
use Prophecy\Prophecy\ObjectProphecy;
11
use Shlinkio\Shlink\Core\Entity\ShortUrl;
12
use Shlinkio\Shlink\Core\Entity\Tag;
13
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
14
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
15
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
16
use Shlinkio\Shlink\Core\Service\ShortUrlService;
17
18
class ShortUrlServiceTest extends TestCase
19
{
20
    /**
21
     * @var ShortUrlService
22
     */
23
    protected $service;
24
    /**
25
     * @var ObjectProphecy|EntityManagerInterface
26
     */
27
    protected $em;
28
29
    public function setUp()
30
    {
31
        $this->em = $this->prophesize(EntityManagerInterface::class);
32
        $this->em->persist(Argument::any())->willReturn(null);
33
        $this->em->flush()->willReturn(null);
34
        $this->service = new ShortUrlService($this->em->reveal());
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function listedUrlsAreReturnedFromEntityManager()
41
    {
42
        $list = [
43
            new ShortUrl(),
44
            new ShortUrl(),
45
            new ShortUrl(),
46
            new ShortUrl(),
47
        ];
48
49
        $repo = $this->prophesize(ShortUrlRepository::class);
50
        $repo->findList(Argument::cetera())->willReturn($list)->shouldBeCalledTimes(1);
51
        $repo->countList(Argument::cetera())->willReturn(count($list))->shouldBeCalledTimes(1);
52
        $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
0 ignored issues
show
Bug introduced by
The method getRepository does only exist in Doctrine\ORM\EntityManagerInterface, but not in Prophecy\Prophecy\ObjectProphecy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
53
54
        $list = $this->service->listShortUrls();
55
        $this->assertEquals(4, $list->getCurrentItemCount());
56
    }
57
58
    /**
59
     * @test
60
     */
61
    public function exceptionIsThrownWhenSettingTagsOnInvalidShortcode()
62
    {
63
        $shortCode = 'abc123';
64
        $repo = $this->prophesize(ShortUrlRepository::class);
65
        $repo->findOneBy(['shortCode' => $shortCode])->willReturn(null)
66
                                                     ->shouldBeCalledTimes(1);
67
        $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
0 ignored issues
show
Bug introduced by
The method getRepository does only exist in Doctrine\ORM\EntityManagerInterface, but not in Prophecy\Prophecy\ObjectProphecy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
68
69
        $this->expectException(InvalidShortCodeException::class);
70
        $this->service->setTagsByShortCode($shortCode);
71
    }
72
73
    /**
74
     * @test
75
     */
76
    public function providedTagsAreGetFromRepoAndSetToTheShortUrl()
77
    {
78
        $shortUrl = $this->prophesize(ShortUrl::class);
79
        $shortUrl->setTags(Argument::any())->shouldBeCalledTimes(1);
80
        $shortCode = 'abc123';
81
        $repo = $this->prophesize(ShortUrlRepository::class);
82
        $repo->findOneBy(['shortCode' => $shortCode])->willReturn($shortUrl->reveal())
83
                                                     ->shouldBeCalledTimes(1);
84
        $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
0 ignored issues
show
Bug introduced by
The method getRepository does only exist in Doctrine\ORM\EntityManagerInterface, but not in Prophecy\Prophecy\ObjectProphecy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
85
86
        $tagRepo = $this->prophesize(EntityRepository::class);
87
        $tagRepo->findOneBy(['name' => 'foo'])->willReturn(new Tag())->shouldbeCalledTimes(1);
88
        $tagRepo->findOneBy(['name' => 'bar'])->willReturn(null)->shouldbeCalledTimes(1);
89
        $this->em->getRepository(Tag::class)->willReturn($tagRepo->reveal());
90
91
        $this->service->setTagsByShortCode($shortCode, ['foo', 'bar']);
92
    }
93
94
    /**
95
     * @test
96
     */
97
    public function updateMetadataByShortCodeUpdatesProvidedData()
98
    {
99
        $shortUrl = new ShortUrl();
100
101
        $repo = $this->prophesize(ShortUrlRepository::class);
102
        $findShortUrl = $repo->findOneBy(['shortCode' => 'abc123'])->willReturn($shortUrl);
103
        $getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
0 ignored issues
show
Bug introduced by
The method getRepository does only exist in Doctrine\ORM\EntityManagerInterface, but not in Prophecy\Prophecy\ObjectProphecy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
104
        $flush = $this->em->flush($shortUrl)->willReturn(null);
0 ignored issues
show
Unused Code introduced by
The call to EntityManagerInterface::flush() has too many arguments starting with $shortUrl.

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...
Bug introduced by
The method flush does only exist in Doctrine\ORM\EntityManagerInterface, but not in Prophecy\Prophecy\ObjectProphecy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
105
106
        $result = $this->service->updateMetadataByShortCode('abc123', ShortUrlMeta::createFromParams(
107
            (new \DateTime('2017-01-01 00:00:00'))->format(\DateTime::ATOM),
108
            (new \DateTime('2017-01-05 00:00:00'))->format(\DateTime::ATOM),
109
            null,
110
            5
111
        ));
112
113
        $this->assertSame($shortUrl, $result);
114
        $this->assertEquals(new \DateTime('2017-01-01 00:00:00'), $shortUrl->getValidSince());
115
        $this->assertEquals(new \DateTime('2017-01-05 00:00:00'), $shortUrl->getValidUntil());
116
        $this->assertEquals(5, $shortUrl->getMaxVisits());
117
        $findShortUrl->shouldHaveBeenCalled();
118
        $getRepo->shouldHaveBeenCalled();
119
        $flush->shouldHaveBeenCalled();
120
    }
121
}
122