ConciergeControllerTest::listUnprintedTickets()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 76
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 8.9667
c 0
b 0
f 0
cc 3
eloc 62
nc 4
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @author    Markus Tacker <[email protected]>
5
 * @copyright 2013-2016 Verein zur Förderung der Netzkultur im Rhein-Main-Gebiet e.V. | http://netzkultur-rheinmain.de/
6
 */
7
8
namespace BCRM\PrintBundle\Tests\Functional;
9
10
use BCRM\BackendBundle\Entity\Event\Registration;
11
use BCRM\BackendBundle\Entity\Event\Ticket;
12
use BCRM\BackendBundle\Entity\Payment;
13
use BCRM\WebBundle\Tests\Functional\Base;
14
use Symfony\Component\BrowserKit\Cookie;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17
class ConciergeControllerTest extends Base
18
{
19
    /**
20
     * The setUpBeforeClass() and tearDownAfterClass() template methods are called before the first test of the test
21
     * case class is run and after the last test of the test case class is run, respectively.
22
     */
23
    public static function setUpBeforeClass()
24
    {
25
        static::resetDatabase();
26
    }
27
28
    /**
29
     * @test
30
     * @group functional
31
     */
32
    public function listUnprintedTickets()
33
    {
34
        $client    = static::createClient();
35
        $container = $client->getContainer();
36
37
        /* @var $em \Doctrine\Common\Persistence\ObjectManager */
38
        $em    = $container
39
            ->get('doctrine')
40
            ->getManager();
41
        $event = $em->getRepository('BCRMBackendBundle:Event\Event')->findAll()[0];
42
43
        // Create registrations
44
        for ($i = 0; $i < 5; $i++) {
45
            $payment = new Payment();
46
            $payment->setTransactionId('payment' . $i);
47
            $payment->setMethod('cash');
48
            $em->persist($payment);
49
            $registration = new Registration();
50
            $registration->setUuid($i);
51
            $registration->setEmail(sprintf('john.doe.198%[email protected]', $i));
52
            $registration->setName(sprintf('John Doe %d', $i));
53
            $registration->setEvent($event);
54
            $registration->setSaturday(false);
55
            $registration->setSunday(true);
56
            $registration->setTags(sprintf('#johndoe%d', $i));
57
            $registration->setPayment($payment);
58
            $em->persist($registration);
59
        }
60
61
        // Create tickets
62
        for ($i = 0; $i < 5; $i++) {
63
            $checkedIn    = $i > 1;
64
            $sundayTicket = new Ticket();
65
            $sundayTicket->setEmail(sprintf('john.doe.198%[email protected]', $i));
66
            $sundayTicket->setName(sprintf('John Doe %d', $i));
67
            $sundayTicket->setEvent($event);
68
            $sundayTicket->setDay(Ticket::DAY_SUNDAY);
69
            $sundayTicket->setCode(sprintf('PRNT%d', $i));
70
            $sundayTicket->setNotified(true);
71
            $sundayTicket->setCheckedIn($checkedIn);
72
            $em->persist($sundayTicket);
73
            $saturdayTicket = new Ticket();
74
            $saturdayTicket->setEmail(sprintf('john.doe.198%[email protected]', $i));
75
            $saturdayTicket->setName(sprintf('John Doe %d', $i));
76
            $saturdayTicket->setEvent($event);
77
            $saturdayTicket->setDay(Ticket::DAY_SATURDAY);
78
            $saturdayTicket->setCode(sprintf('NOPRNT%d', $i));
79
            $saturdayTicket->setNotified(true);
80
            $saturdayTicket->setCheckedIn($checkedIn);
81
            $em->persist($saturdayTicket);
82
        }
83
        $em->flush();
84
85
        $client = static::createClient(array(), array(
86
            'PHP_AUTH_USER' => 'concierge',
87
            'PHP_AUTH_PW'   => 'letmein',
88
        ));
89
        $client->request('GET', '/api/printing/queue');
90
        $response = $client->getResponse();
91
        $this->assertEquals(200, $response->getStatusCode());
92
        $this->assertEquals("application/json", $response->headers->get('Content-Type'));
93
        $this->assertEquals("utf-8", $response->getCharset());
94
        $queue = json_decode($response->getContent());
95
        $this->assertEquals(3, count($queue->items));
96
97
        $ticket = $queue->items[0];
98
        $this->assertObjectHasAttribute('name', $ticket);
99
        $this->assertObjectHasAttribute('tags', $ticket);
100
        $this->assertObjectHasAttribute('code', $ticket);
101
        $this->assertObjectHasAttribute('day', $ticket);
102
        $this->assertEquals('#' . strtolower(str_replace(' ', '', $ticket->name)), $ticket->tags);
103
        $this->assertEquals(1, preg_match('/^PRNT[0-9]+$/', $ticket->code));
104
        $this->assertEquals(2, $ticket->day);
105
106
        return $queue->items;
107
    }
108
109
    /**
110
     * @depends listUnprintedTickets
111
     * @test
112
     * @group   functional
113
     *
114
     * @param array $items
115
     */
116
    public function printTicket(array $items)
117
    {
118
        $client = static::createClient(array(), array(
119
            'PHP_AUTH_USER' => 'concierge',
120
            'PHP_AUTH_PW'   => 'letmein',
121
        ));
122
123
        // Print a ticket
124
        $client->request('PATCH', $items[0]->{'@subject'});
125
        $response = $client->getResponse();
126
        $this->assertEquals(200, $response->getStatusCode());
127
128
        // Queue should not contain item
129
        $client->request('GET', '/api/printing/queue');
130
        $response = $client->getResponse();
131
        $queue2   = json_decode($response->getContent());
132
        $this->assertFalse(in_array($items[0]->{'@subject'}, array_map(function ($item) {
133
            return $item->{'@subject'};
134
        }, $queue2->items)));
135
136
        return $items;
137
    }
138
139
    /**
140
     * @test
141
     * @group   functional
142
     * @depends printTicket
143
     *
144
     * @param array $items
145
     */
146
    public function ticketsCanBeReprinted(array $items)
147
    {
148
        $client    = static::createClient(array(), array(
149
            'PHP_AUTH_USER' => 'concierge',
150
            'PHP_AUTH_PW'   => 'letmein',
151
        ));
152
        $container = $client->getContainer();
153
154
        // Delete printed state
155
        $client->request('DELETE', $items[0]->{'@subject'});
156
        $response = $client->getResponse();
157
        $this->assertEquals(200, $response->getStatusCode());
158
159
        /* @var $em \Doctrine\Common\Persistence\ObjectManager */
160
        $em = $container
161
            ->get('doctrine')
162
            ->getManager();
163
164
        /* @var $ticket Ticket */
165
        $ticket = $em->getRepository('BCRMBackendBundle:Event\Ticket')->findOneBy(array('code' => $items[0]->code));
166
        $this->assertFalse($ticket->isPrinted());
167
    }
168
}
169