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

AnalyticsManagerTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 1
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 82
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A testBuildPipeline() 0 72 1
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