Completed
Pull Request — develop (#586)
by
unknown
161:59 queued 97:20
created

AnalyticsManagerTest::testBuildPipeline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 72
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 72
rs 9.102
cc 1
eloc 20
nc 1
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
 * Testing case
4
 */
5
namespace Graviton\AnalyticsBundle\Tests\Manager;
6
7
use Graviton\AnalyticsBundle\Helper\JsonMapper;
8
use Graviton\AnalyticsBundle\Manager\AnalyticsManager;
9
use Graviton\AnalyticsBundle\Model\AnalyticModel;
10
use Graviton\TestBundle\Test\RestTestCase;
11
12
/**
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class AnalyticsManagerTest extends RestTestCase
18
{
19
    /**
20
     * Testing AnalyticsManager::parseObjectDates()
21
     * Testing AnalyticsManager::buildPipeline()
22
     * Testing JsonMapper::map()
23
     *
24
     * @return void
25
     */
26
    public function testBuildPipeline()
27
    {
28
        $analyticsManager = $this->getMockBuilder(AnalyticsManager::class)
29
            ->disableOriginalConstructor()
30
            ->setMethods(array('buildPipeline', 'parseObjectDates'))
31
            ->getMock();
32
        $buildPipeline = $this->getPrivateClassMethod(AnalyticsManager::class, 'buildPipeline');
33
        $mapper = new JsonMapper();
34
        $date = new \DateTime('-4 years');
35
        $year = $date->format('Y');
36
        $expect = '[{"$match":{"created_year":{"$gte":'.$year.'}}},{"$group":{"_id":"app-count","count":{"$sum":1}}}]';
37
38
        $definitionA = json_decode(
39
            '{
40
              "collection": "App",
41
              "route": "app",
42
              "type": "object",
43
              "aggregate": {
44
                "$match": {
45
                  "created_year": {
46
                    "$gte": "PARSE_DATE(-4 years|Y)"
47
                  }
48
                },
49
                "$group": {
50
                  "_id": "app-count",
51
                  "count": {
52
                    "$sum": 1
53
                  }
54
                }
55
              },
56
              "schema": {}
57
            }
58
        '
59
        );
60
61
        $modelA = $mapper->map($definitionA, new AnalyticModel());
62
        $resultA = json_encode($buildPipeline->invokeArgs($analyticsManager, [$modelA]));
63
        $this->assertEquals($expect, $resultA);
64
65
        // Pipeline
66
        $definitionB = json_decode(
67
            '{
68
              "collection": "App",
69
              "route": "app",
70
              "type": "object",
71
              "pipeline": [
72
                {
73
                  "$match": {
74
                    "created_year": {
75
                      "$gte": "PARSE_DATE(-4 years|Y)"
76
                    }
77
                  }
78
                },
79
                {
80
                  "$group": {
81
                    "_id": "app-count",
82
                    "count": {
83
                      "$sum": 1
84
                    }
85
                  }
86
                }
87
              ],
88
              "schema": {}
89
            }
90
        '
91
        );
92
93
        $modelB = $mapper->map($definitionB, new AnalyticModel());
94
95
        $resultB = json_encode($buildPipeline->invokeArgs($analyticsManager, [$modelB]));
96
        $this->assertEquals($expect, $resultB);
97
    }
98
}
99