diff options
author | diosmosis <benaka@piwik.pro> | 2015-09-17 23:48:49 +0300 |
---|---|---|
committer | diosmosis <benaka@piwik.pro> | 2015-09-23 10:57:32 +0300 |
commit | 86a2dc7378f76afcc22b3c7aedcb85febcc7e4b2 (patch) | |
tree | 68316bc4b88fc202ea371638c55c80c7e761c55a /plugins/SegmentEditor/tests/Unit | |
parent | bcbb7bcf3452ca369eb85e09174f1b16361c63c2 (diff) |
Add unit test for new SegmentQueryDecorator test.
Diffstat (limited to 'plugins/SegmentEditor/tests/Unit')
-rw-r--r-- | plugins/SegmentEditor/tests/Unit/SegmentQueryDecoratorTest.php | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/plugins/SegmentEditor/tests/Unit/SegmentQueryDecoratorTest.php b/plugins/SegmentEditor/tests/Unit/SegmentQueryDecoratorTest.php new file mode 100644 index 0000000000..9fe85b6701 --- /dev/null +++ b/plugins/SegmentEditor/tests/Unit/SegmentQueryDecoratorTest.php @@ -0,0 +1,87 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\SegmentEditor\tests\Unit; + +use Piwik\Plugins\SegmentEditor\SegmentQueryDecorator; +use Piwik\Segment\SegmentExpression; + +/** + * @group SegmentEditor + * @group SegmentEditor_Unit + */ +class SegmentQueryDecoratorTest extends \PHPUnit_Framework_TestCase +{ + public static $storedSegments = array( + array('definition' => 'countryCode==abc', 'idsegment' => 1), + array('definition' => 'region!=FL', 'idsegment' => 2), + array('definition' => 'browserCode==def;visitCount>2', 'idsegment' => 3), + array('definition' => 'region!=FL', 'idsegment' => 4), + ); + + /** + * @var SegmentQueryDecorator + */ + private $decorator; + + public function setUp() + { + parent::setUp(); + + $mockApi = $this->getMockSegmentEditorApi(); + $this->decorator = new SegmentQueryDecorator($mockApi); + } + + public function test_getSelectQueryString_DoesNotDecorateSql_WhenNoSegmentUsed() + { + $expression = new SegmentExpression(''); + $expression->parseSubExpressions(); + + $query = $this->decorator->getSelectQueryString($expression, '*', 'log_visit', '', array(), '', '', ''); + + $this->assertStringStartsNotWith('/* idSegments', $query['sql']); + } + + public function test_getSelectQueryString_DoesNotDecorateSql_WhenNoSegmentMatchesUsedSegment() + { + $expression = new SegmentExpression('referrerName==ooga'); + $expression->parseSubExpressions(); + + $query = $this->decorator->getSelectQueryString($expression, '*', 'log_visit', '', array(), '', '', ''); + + $this->assertStringStartsNotWith('/* idSegments', $query['sql']); + } + + public function test_getSelectQueryString_DecoratesSql_WhenOneSegmentMatchesUsedSegment() + { + $expression = new SegmentExpression('browserCode==def;visitCount>2'); + $expression->parseSubExpressions(); + + $query = $this->decorator->getSelectQueryString($expression, '*', 'log_visit', '', array(), '', '', ''); + + $this->assertStringStartsWith('/* idSegments = [3] */', $query['sql']); + } + + public function test_getSelectQueryString_DecoratesSql_WhenMultipleStoredSegmentsMatchUsedSegment() + { + $expression = new SegmentExpression('region!=FL'); + $expression->parseSubExpressions(); + + $query = $this->decorator->getSelectQueryString($expression, '*', 'log_visit', '', array(), '', '', ''); + + $this->assertStringStartsWith('/* idSegments = [2, 4] */', $query['sql']); + } + + private function getMockSegmentEditorApi() + { + $mock = $this->getMock('Piwik\Plugins\SegmentEditor\API', array('getSegmentsToAutoArchive'), array(), '', + $callOriginalConstructor = false); + $mock->expects($this->any())->method('getSegmentsToAutoArchive')->willReturn(self::$storedSegments); + return $mock; + } +} |