/** * Tests to make sure the array representation of the query is accurate. */ public function testArrayRepresentation() { $q = new GaDataQuery(); /* It shouldn't be possible to get the query's array representation until the minimum required properties have been set. */ $this->assertThrows(__NAMESPACE__ . '\\LogicException', array($q, 'getAsArray')); $profile = new ProfileSummary(); $profile->setID('123'); $profile->setName('foo'); $q->setProfile($profile); $this->assertThrows(__NAMESPACE__ . '\\LogicException', array($q, 'getAsArray')); $q->setStartDate('2015-03-15'); $this->assertThrows(__NAMESPACE__ . '\\LogicException', array($q, 'getAsArray')); $q->setEndDate('2015-03-31'); $this->assertThrows(__NAMESPACE__ . '\\LogicException', array($q, 'getAsArray')); $q->setMetrics(array('a_metric', 'another_metric')); $expected = array('ids' => 'ga:123', 'start-date' => '2015-03-15', 'end-date' => '2015-03-31', 'metrics' => 'ga:a_metric,ga:another_metric', 'start-index' => 1, 'max-results' => GOOGLE_ANALYTICS_API_PAGE_SIZE); $this->assertEquals($expected, $q->getAsArray()); $q->setMaxResults(234); $expected['max-results'] = 234; $this->assertEquals($expected, $q->getAsArray()); $q->setStartIndex(1000); $expected['start-index'] = 1000; $this->assertEquals($expected, $q->getAsArray()); $q->setMetrics('foo,bar,baz'); $expected['metrics'] = 'ga:foo,ga:bar,ga:baz'; $this->assertEquals($expected, $q->getAsArray()); $profile = new ProfileSummary(); $profile->setID('987987'); $profile->setName('asdf'); $q->setProfile($profile); $expected['ids'] = 'ga:987987'; $this->assertEquals($expected, $q->getAsArray()); $q->setStartDate('2000-01-01'); $q->setEndDate('2000-12-31'); $expected['start-date'] = '2000-01-01'; $expected['end-date'] = '2000-12-31'; $this->assertEquals($expected, $q->getAsArray()); $q->setSamplingLevel(GaDataQuery::SAMPLING_LEVEL_FASTER); $expected['samplingLevel'] = 'FASTER'; $this->assertEquals($expected, $q->getAsArray()); // Note the special case here $q->setSamplingLevel(GaDataQuery::SAMPLING_LEVEL_NONE); unset($expected['samplingLevel']); $this->assertEquals($expected, $q->getAsArray()); $q->setDimensions(array('asdfasdf', 'oijoij')); $expected['dimensions'] = 'ga:asdfasdf,ga:oijoij'; $this->assertEquals($expected, $q->getAsArray()); $sort = new GaDataSortOrder(); $sort->addField('foo'); $sort->addField('bar', SORT_DESC); $q->setSort($sort); $expected['sort'] = (string) $sort; $this->assertEquals($expected, $q->getAsArray()); $filter = new GaDataFilterCollection(GaDataFilterCollection::OP_AND, new GaDataFilterCollection(GaDataFilterCollection::OP_OR, new GaDataConditionalExpression('foo', GaDataConditionalExpression::OP_GE, 1)), new GaDataFilterCollection(GaDataFilterCollection::OP_OR, new GaDataConditionalExpression('bar', GaDataConditionalExpression::OP_NE, 'baz'))); $q->setFilter($filter); $expected['filters'] = (string) $filter; $this->assertEquals($expected, $q->getAsArray()); $segment = new GaDataSegment(new GaDataSegmentConditionGroup(new GaDataSegmentSimpleCondition('foo', GaDataSegmentSimpleCondition::OP_LT, 20)), GaDataSegment::SCOPE_USERS, new GaDataSegmentSequence(new GaDataSegmentSequenceCondition('bar', GaDataSegmentSequenceCondition::OP_EQ, 'baz'), new GaDataSegmentSequenceCondition('baz', GaDataSegmentSequenceCondition::OP_GT, 0, GaDataSegmentSequenceCondition::OP_FOLLOWED_BY_IMMEDIATE)), GaDataSegment::SCOPE_SESSIONS); $q->setSegment($segment); $expected['segment'] = (string) $segment; $this->assertEquals($expected, $q->getAsArray()); }