Passed
Push — master ( c87045...1efded )
by Chris
34s
created

DiscountCodeAvailabilityTest::provideIsAvailable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace ConferenceTools\Tickets\Domain\Service\Availability;
4
5
use Carnage\Cqrs\Persistence\ReadModel\RepositoryInterface;
6
use ConferenceTools\Tickets\Domain\Service\Availability\Filters\FilterInterface;
7
use ConferenceTools\Tickets\Domain\ValueObject\DiscountType;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Mockery\Adapter\Phpunit\MockeryTestCase;
10
use ConferenceTools\Tickets\Domain\Finder\TicketCounterInterface;
0 ignored issues
show
Bug introduced by
The type ConferenceTools\Tickets\...\TicketCounterInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use ConferenceTools\Tickets\Domain\ReadModel\TicketCounts\TicketCounter;
12
use ConferenceTools\Tickets\Domain\Service\Configuration;
13
use Mockery as m;
14
15
class DiscountCodeAvailabilityTest extends MockeryTestCase
16
{
17
    /**
18
     * @var Configuration
19
     */
20
    private $config;
21
22
    /**
23
     * @var ArrayCollection
24
     */
25
    private $ticketCounters;
0 ignored issues
show
introduced by
The private property $ticketCounters is not used, and could be removed.
Loading history...
26
27
    /**
28
     * @var FilterInterface[]
29
     */
30
    private $filters;
31
32
    public function __construct($name = null, array $data = array(), $dataName = '')
33
    {
34
        parent::__construct($name, $data, $dataName);
35
        $this->config = Configuration::fromArray([
36
            'tickets' => [
37
                'early' => ['name' => 'Early Bird', 'cost' => 5000, 'available' => 75],
38
            ],
39
            'discountCodes' => [
40
                '50off' => [
41
                    'type' => DiscountType\Percentage::class,
42
                    'options' => ['percentage' => 50],
43
                    'name' => '50% Off',
44
                    'metadata' => [
45
                        'availableFrom' =>(new \DateTime)->sub(new \DateInterval('P2D')),
46
                        'availableTo' =>(new \DateTime)->sub(new \DateInterval('P1D')),
47
                    ]
48
                ],
49
                '5offone' => [
50
                    'type' => DiscountType\Fixed::class,
51
                    'options' => ['net' => 500],
52
                    'name' => '$5 Off',
53
                    'metadata' => [
54
                        'availableFrom' =>(new \DateTime)->sub(new \DateInterval('P1D')),
55
                        'availableTo' =>(new \DateTime)->add(new \DateInterval('P1D')),
56
                    ]
57
                ],
58
                '5offall' => [
59
                    'type' => DiscountType\FixedPerTicket::class,
60
                    'options' => ['gross' => 500],
61
                    'name' => '$5 Off',
62
                ],
63
            ],
64
            'financial' => [
65
                'taxRate' => 10,
66
                'currency' => 'GBP',
67
                'displayTax' => true
68
            ]
69
        ]);
70
71
        $this->filters = [
72
            new Filters\DiscountByDate($this->config),
73
        ];
74
    }
75
76 View Code Duplication
    public function testFetchAllAvailableDiscountCodes()
0 ignored issues
show
Duplication introduced by
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...
77
    {
78
        $mockFinder = m::mock(RepositoryInterface::class);
79
        $mockFinder->shouldReceive('matching')->andReturn(new ArrayCollection($this->config->getDiscountCodes()));
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'matching'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        $mockFinder->/** @scrutinizer ignore-call */ 
80
                     shouldReceive('matching')->andReturn(new ArrayCollection($this->config->getDiscountCodes()));

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. Please note the @ignore annotation hint above.

Loading history...
80
81
        $sut = new DiscountCodeAvailability($mockFinder, ...$this->filters);
0 ignored issues
show
Bug introduced by
$mockFinder of type Mockery\MockInterface is incompatible with the type Carnage\Cqrs\Persistence...del\RepositoryInterface expected by parameter $repository of ConferenceTools\Tickets\...lability::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
        $sut = new DiscountCodeAvailability(/** @scrutinizer ignore-type */ $mockFinder, ...$this->filters);
Loading history...
82
83
        $result = $sut->fetchAllAvailableDiscountCodes();
84
85
        self::assertTrue($result->count() === 2, 'The expected number of discount codes was not returned');
86
        self::assertFalse($result->contains($this->config->getDiscountCodes()['50off']), 'Expired code was included in results');
87
    }
88
89
    /**
90
     * @dataProvider provideIsAvailable
91
     *
92
     */
93 View Code Duplication
    public function testIsAvailable($code, $expected)
0 ignored issues
show
Duplication introduced by
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...
94
    {
95
        $mockFinder = m::mock(RepositoryInterface::class);
96
        $mockFinder->shouldReceive('matching')->andReturn(new ArrayCollection($this->config->getDiscountCodes()));
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'matching'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        $mockFinder->/** @scrutinizer ignore-call */ 
97
                     shouldReceive('matching')->andReturn(new ArrayCollection($this->config->getDiscountCodes()));

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. Please note the @ignore annotation hint above.

Loading history...
97
98
        $sut = new DiscountCodeAvailability($mockFinder, ...$this->filters);
0 ignored issues
show
Bug introduced by
$mockFinder of type Mockery\MockInterface is incompatible with the type Carnage\Cqrs\Persistence...del\RepositoryInterface expected by parameter $repository of ConferenceTools\Tickets\...lability::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
        $sut = new DiscountCodeAvailability(/** @scrutinizer ignore-type */ $mockFinder, ...$this->filters);
Loading history...
99
100
        self::assertEquals($expected, $sut->isAvailable($code));
101
    }
102
103
    public function provideIsAvailable()
104
    {
105
        return [
106
            [$this->config->getDiscountCodes()['50off'], false],
107
            [$this->config->getDiscountCodes()['5offone'], true],
108
            [$this->config->getDiscountCodes()['5offall'], true],
109
        ];
110
    }
111
}
112