/** * Gets the projection part of the pipeline. * * @param string $field */ private function getProjection($field) { $projection = new Projection(); $projection->includeField($field); $originalCondition = Condition::getConditionByIfArray(['$gte' => ['$' . $field, []]], null, '$' . $field); $unWrappedCondition = Condition::getConditionByIfArray(['$gte' => ['$' . $field, []]], '$' . $field, [1]); $projection->includeOperationField($field . '_original', $originalCondition); $projection->includeOperationField($field . '_unwrapped', $unWrappedCondition); return $projection; }
/** * Test projecting week numbers. */ public function testProjectWeekNumber() { $testData = [['foo' => new MongoDate(strtotime('2015W10')), 'expected' => strftime('%U', strtotime('2015W10'))], ['foo' => new MongoDate(strtotime('2015W20')), 'expected' => strftime('%U', strtotime('2015W20'))], ['foo' => new MongoDate(strtotime('2015W25')), 'expected' => strftime('%U', strtotime('2015W25'))]]; foreach ($testData as $test) { $this->collection->save($test); } $week = new WeekOperation(); $week->setDatefield('foo'); $projection = new Projection(); $projection->includeField('expected'); $projection->includeOperationField('weeknumber', $week); $result = $this->collection->aggregate([$projection->getStage()]); $this->assertEquals(3, count($result['result'])); foreach ($result['result'] as $record) { $this->assertEquals($record['expected'], $record['weeknumber']); } }
/** * Test if fields are added. */ public function testFieldsAreAdded() { $testData = [['foo' => 'foo'], ['foo' => 'foo'], ['foo' => 'bar'], ['foo' => 'bar'], ['foo' => 'bar']]; foreach ($testData as $test) { $this->collection->save($test); } $projection = new Projection(); $projection->includeField('foo'); $condition = Condition::getConditionByIfArray(['$eq' => ['$foo', 'foo']], true, false); $projection->includeOperationField('isFoo', $condition); $result = $this->collection->aggregate([$projection->getStage()]); $this->assertEquals(5, count($result['result'])); foreach ($result['result'] as $res) { if ($res['foo'] === 'foo') { $this->assertTrue($res['isFoo']); } else { $this->assertFalse($res['isFoo']); } } }