Completed
Push — master ( 16b58d...ac063d )
by Matthew
09:38
created

TrendsControllerTest::runTimingsActionTests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 61
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
dl 0
loc 61
rs 9.5147
c 1
b 1
f 1
cc 1
eloc 50
nc 1
nop 1

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
namespace Dtc\QueueBundle\Tests\Controller;
4
5
use Dtc\QueueBundle\Controller\TrendsController;
6
use Dtc\QueueBundle\ORM\JobTimingManager;
7
use Dtc\QueueBundle\Model\BaseJob;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\HttpFoundation\Request;
10
11
class TrendsControllerTest extends TestCase
12
{
13
    use ControllerTrait;
14
15
    public function testTimingsAction()
16
    {
17
        $container = $this->getContainerOrm();
18
        $this->runTimingsActionTests($container);
19
        $container = $this->getContainerOdm();
20
        $this->runTimingsActionTests($container);
21
    }
22
23
    public function runTimingsActionTests($container)
24
    {
25
        $trendsController = new TrendsController();
26
        $trendsController->setContainer($container);
27
28
        $dateTimeStr = '2017-07-01T4:04:04Z';
29
        $dateTime = \DateTime::createFromFormat(DATE_ISO8601, $dateTimeStr);
30
        self::assertNotFalse($dateTime);
31
32
        /** @var JobTimingManager $jobTimingManager */
33
        $jobTimingManager = $container->get('dtc_queue.job_timing_manager');
34
        $jobTimingManager->recordTiming(BaseJob::STATUS_SUCCESS, $dateTime);
0 ignored issues
show
Security Bug introduced by
It seems like $dateTime defined by \DateTime::createFromFor..._ISO8601, $dateTimeStr) on line 29 can also be of type false; however, Dtc\QueueBundle\Model\Jo...Manager::recordTiming() does only seem to accept null|object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
35
        $jobTimingManager->recordTiming(BaseJob::STATUS_SUCCESS, $dateTime);
0 ignored issues
show
Security Bug introduced by
It seems like $dateTime defined by \DateTime::createFromFor..._ISO8601, $dateTimeStr) on line 29 can also be of type false; however, Dtc\QueueBundle\Model\Jo...Manager::recordTiming() does only seem to accept null|object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
36
        $jobTimingManager->recordTiming(BaseJob::STATUS_ERROR, $dateTime);
0 ignored issues
show
Security Bug introduced by
It seems like $dateTime defined by \DateTime::createFromFor..._ISO8601, $dateTimeStr) on line 29 can also be of type false; however, Dtc\QueueBundle\Model\Jo...Manager::recordTiming() does only seem to accept null|object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
37
38
        $request = new Request();
39
        $request->query->set('type', 'HOUR');
40
        $request->query->set('end', '2017-07-01T5:05:00.0Z');
41
        $timings = $trendsController->getTimingsAction($request);
42
        $content = $timings->getContent();
43
44
        self::assertNotEmpty($content);
45
        $contentDecoded = json_decode($content, true);
46
        self::assertCount(3, $contentDecoded);
47
        self::assertEquals([3], $contentDecoded['timings_data_0']);
48
        self::assertEquals(['2017-07-01 04'], $contentDecoded['timings_dates']);
49
        self::assertEquals(['2017-07-01T04:00:00+00:00'], $contentDecoded['timings_dates_rfc3339']);
50
51
        $request = new Request();
52
        $request->query->set('type', 'MINUTE');
53
        $request->query->set('end', '2017-07-01T4:05:04.0Z');
54
        $timings = $trendsController->getTimingsAction($request);
55
        $content = $timings->getContent();
56
        $contentDecoded = json_decode($content, true);
57
        self::assertEquals(['2017-07-01 04:04'], $contentDecoded['timings_dates']);
58
        self::assertEquals(['2017-07-01T04:04:00+00:00'], $contentDecoded['timings_dates_rfc3339']);
59
60
        $request = new Request();
61
        $request->query->set('type', 'DAY');
62
        $request->query->set('end', '2017-07-01T4:05:04.0Z');
63
        $timings = $trendsController->getTimingsAction($request);
64
        $content = $timings->getContent();
65
        $contentDecoded = json_decode($content, true);
66
        self::assertEquals(['2017-07-01'], $contentDecoded['timings_dates']);
67
68
        $request = new Request();
69
        $request->query->set('type', 'MONTH');
70
        $request->query->set('end', '2017-07-01T4:05:04.0Z');
71
        $timings = $trendsController->getTimingsAction($request);
72
        $content = $timings->getContent();
73
        $contentDecoded = json_decode($content, true);
74
        self::assertEquals(['2017-07'], $contentDecoded['timings_dates']);
75
76
        $request = new Request();
77
        $request->query->set('type', 'YEAR');
78
        $request->query->set('end', '2017-07-01T4:05:04.0Z');
79
        $timings = $trendsController->getTimingsAction($request);
80
        $content = $timings->getContent();
81
        $contentDecoded = json_decode($content, true);
82
        self::assertEquals(['2017'], $contentDecoded['timings_dates']);
83
    }
84
}
85