TicketAvailabilityTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A provideIsAvailable() 0 12 1
A __construct() 0 47 2
A testFetchAllAvailableTickets() 0 11 1
A testIsAvailable() 0 8 1
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 Doctrine\Common\Collections\ArrayCollection;
8
use Mockery\Adapter\Phpunit\MockeryTestCase;
9
use ConferenceTools\Tickets\Domain\ReadModel\TicketCounts\TicketCounter;
10
use ConferenceTools\Tickets\Domain\Service\Configuration;
11
use Mockery as m;
12
13
class TicketAvailabilityTest extends MockeryTestCase
14
{
15
    /**
16
     * @var Configuration
17
     */
18
    private $config;
19
20
    /**
21
     * @var ArrayCollection
22
     */
23
    private $ticketCounters;
24
25
    /**
26
     * @var FilterInterface[]
27
     */
28
    private $filters;
29
30
    public function __construct($name = null, array $data = array(), $dataName = '')
31
    {
32
        parent::__construct($name, $data, $dataName);
33
        $settings = [
34
            'tickets' => [
35
                'early' => ['name' => 'Early Bird', 'cost' => 5000, 'available' => 75],
36
                'std' => ['name' => 'Standard', 'cost' => 10000, 'available' => 150],
37
                'free' => ['name' => 'Free', 'cost' => 0, 'available' => 100, 'metadata' => ['private' => true]],
38
                'expired' => ['name' => 'Expired', 'cost' => 1000, 'available' => 100, 'metadata' => [
39
                    'availableFrom' =>(new \DateTime)->sub(new \DateInterval('P1D')),
40
                    'availableTo' =>(new \DateTime)->sub(new \DateInterval('P1D')),
41
                ]],
42
                'after_early' => ['name' => 'After Early', 'cost' => 7500, 'available' => 100, 'metadata' => [
43
                    'after' => ['early']
44
                ]],
45
                'after_expired' => ['name' => 'After Expired', 'cost' => 2500, 'available' => 100, 'metadata' => [
46
                    'after' => ['expired']
47
                ]],
48
                'soldout' => ['name' => 'Sold out', 'cost' => 1500, 'available' => 0, 'metadata' => [
49
                ]],
50
                'after_soldout' => ['name' => 'After Sold out', 'cost' => 3500, 'available' => 100, 'metadata' => [
51
                    'after' => ['soldout']
52
                ]],
53
            ],
54
            'financial' => [
55
                'taxRate' => 10,
56
                'currency' => 'GBP',
57
                'displayTax' => true
58
            ]
59
        ];
60
        $this->config = Configuration::fromArray($settings);
61
62
        $ticketCounters = [];
63
        foreach (array_keys($settings['tickets']) as $identifier) {
64
            $ticketCounters[$identifier] = new TicketCounter(
65
                $this->config->getTicketType($identifier),
66
                $this->config->getAvailableTickets($identifier)
67
            );
68
        }
69
70
        $this->ticketCounters = new ArrayCollection($ticketCounters);
71
72
        $this->filters = [
73
            new Filters\IsAvailable(),
74
            new Filters\AfterSoldOut($this->config),
75
            new Filters\ByDate($this->config),
76
            new Filters\IsPrivate($this->config)
77
        ];
78
    }
79
80
    public function testFetchAllAvailableTickets()
81
    {
82
        $mockFinder = m::mock(RepositoryInterface::class);
83
        $mockFinder->shouldReceive('matching')->andReturn($this->ticketCounters);
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

83
        $mockFinder->/** @scrutinizer ignore-call */ 
84
                     shouldReceive('matching')->andReturn($this->ticketCounters);

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...
84
85
        $sut = new TicketAvailability($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

85
        $sut = new TicketAvailability(/** @scrutinizer ignore-type */ $mockFinder, ...$this->filters);
Loading history...
86
87
        $result = $sut->fetchAllAvailableTickets();
88
89
        self::assertTrue($result->count() === 4, 'The expected number of tickets was not returned');
90
        self::assertFalse($result->contains($this->ticketCounters['free']), 'Free tickets were included in the result');
91
    }
92
93
    /**
94
     * @dataProvider provideIsAvailable
95
     *
96
     * @param $ticketType
97
     * @param $quantity
98
     * @param $expected
99
     */
100
    public function testIsAvailable($ticketType, $quantity, $expected)
101
    {
102
        $mockFinder = m::mock(RepositoryInterface::class);
103
        $mockFinder->shouldReceive('matching')->andReturn($this->ticketCounters);
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

103
        $mockFinder->/** @scrutinizer ignore-call */ 
104
                     shouldReceive('matching')->andReturn($this->ticketCounters);

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...
104
105
        $sut = new TicketAvailability($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

105
        $sut = new TicketAvailability(/** @scrutinizer ignore-type */ $mockFinder, ...$this->filters);
Loading history...
106
107
        self::assertEquals($expected, $sut->isAvailable($ticketType, $quantity));
108
    }
109
110
    public function provideIsAvailable()
111
    {
112
        return [
113
            [$this->config->getTicketType('free'), 1, false],
114
            [$this->config->getTicketType('soldout'), 1, false],
115
            [$this->config->getTicketType('expired'), 1, false],
116
            [$this->config->getTicketType('after_early'), 1, false],
117
            [$this->config->getTicketType('early'), 76, false],
118
            [$this->config->getTicketType('after_expired'), 1, true],
119
            [$this->config->getTicketType('after_soldout'), 1, true],
120
            [$this->config->getTicketType('std'), 1, true],
121
            [$this->config->getTicketType('early'), 1, true],
122
        ];
123
    }
124
}
125