Passed
Pull Request — master (#82)
by Chris
06:20 queued 02:55
created

testFetchAllAvailableTickets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 11
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 Doctrine\Common\Collections\ArrayCollection;
8
use Mockery\Adapter\Phpunit\MockeryTestCase;
9
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...
10
use ConferenceTools\Tickets\Domain\ReadModel\TicketCounts\TicketCounter;
11
use ConferenceTools\Tickets\Domain\Service\Configuration;
12
use Mockery as m;
13
14
class TicketAvailabilityTest extends MockeryTestCase
15
{
16
    /**
17
     * @var Configuration
18
     */
19
    private $config;
20
21
    /**
22
     * @var ArrayCollection
23
     */
24
    private $ticketCounters;
25
26
    /**
27
     * @var FilterInterface[]
28
     */
29
    private $filters;
30
31
    public function __construct($name = null, array $data = array(), $dataName = '')
32
    {
33
        parent::__construct($name, $data, $dataName);
34
        $this->config = Configuration::fromArray([
35
            'tickets' => [
36
                'early' => ['name' => 'Early Bird', 'cost' => 5000, 'available' => 75],
37
                'std' => ['name' => 'Standard', 'cost' => 10000, 'available' => 150],
38
                'free' => ['name' => 'Free', 'cost' => 0, 'available' => 100, 'metadata' => ['private' => true]]
39
            ],
40
            'financial' => [
41
                'taxRate' => 10,
42
                'currency' => 'GBP',
43
                'displayTax' => true
44
            ]
45
        ]);
46
        $ticketCounters['early'] = new TicketCounter(
0 ignored issues
show
Comprehensibility Best Practice introduced by
$ticketCounters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ticketCounters = array(); before regardless.
Loading history...
47
            $this->config->getTicketType('early'),
48
            $this->config->getAvailableTickets('early')
49
        );
50
51
        $ticketCounters['std'] = new TicketCounter(
52
            $this->config->getTicketType('std'),
53
            $this->config->getAvailableTickets('std')
54
        );
55
56
        $ticketCounters['free'] = new TicketCounter(
57
            $this->config->getTicketType('free'),
58
            $this->config->getAvailableTickets('free')
59
        );
60
61
        $this->ticketCounters = new ArrayCollection($ticketCounters);
62
63
        $this->filters = [
64
            new Filters\IsAvailable(),
65
            new Filters\AfterSoldOut($this->config),
66
            new Filters\ByDate($this->config),
67
            new Filters\IsPrivate($this->config)
68
        ];
69
    }
70
71
    public function testFetchAllAvailableTickets()
72
    {
73
        $mockFinder = m::mock(RepositoryInterface::class);
74
        $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

74
        $mockFinder->/** @scrutinizer ignore-call */ 
75
                     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...
75
76
        $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

76
        $sut = new TicketAvailability(/** @scrutinizer ignore-type */ $mockFinder, ...$this->filters);
Loading history...
77
78
        $result = $sut->fetchAllAvailableTickets();
79
80
        self::assertTrue($result->count() === 2, 'The expected number of tickets was not returned');
81
        self::assertFalse($result->contains($this->ticketCounters['free']), 'Free tickets were included in the result');
82
    }
83
84
    /**
85
     * @dataProvider provideIsAvailable
86
     *
87
     * @param $ticketType
88
     * @param $quantity
89
     * @param $expected
90
     */
91
    public function testIsAvailable($ticketType, $quantity, $expected)
92
    {
93
        $mockFinder = m::mock(RepositoryInterface::class);
94
        $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

94
        $mockFinder->/** @scrutinizer ignore-call */ 
95
                     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...
95
96
        $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

96
        $sut = new TicketAvailability(/** @scrutinizer ignore-type */ $mockFinder, ...$this->filters);
Loading history...
97
98
        self::assertEquals($expected, $sut->isAvailable($ticketType, $quantity));
99
    }
100
101
    public function provideIsAvailable()
102
    {
103
        return [
104
            [$this->config->getTicketType('free'), 1, false],
105
            [$this->config->getTicketType('std'), 1, true],
106
            [$this->config->getTicketType('early'), 76, false],
107
        ];
108
    }
109
}
110