public function testBuild() { $segment = $this->getSegment(); $em = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock(); $repo = $this->getMockBuilder('Oro\\Bundle\\SegmentBundle\\Entity\\Repository\\SegmentSnapshotRepository')->disableOriginalConstructor()->getMock(); $repo->expects($this->once())->method('getIdentifiersSelectQueryBuilder')->with($segment)->will($this->returnValue(new QueryBuilder($em))); $em->expects($this->once())->method('getRepository')->with('OroSegmentBundle:SegmentSnapshot')->will($this->returnValue($repo)); $em->expects($this->any())->method('createQuery')->will($this->returnValue(new Query($em))); $staticSegmentQB = new StaticSegmentQueryBuilder($em); $staticSegmentQB->build($segment); }
public function testBuild() { $segment = $this->getSegment(); $configuration = $this->getMockBuilder('Doctrine\\ORM\\Configuration')->disableOriginalConstructor()->getMock(); $configuration->expects($this->once())->method('getDefaultQueryHints')->will($this->returnValue([])); $configuration->expects($this->once())->method('isSecondLevelCacheEnabled')->will($this->returnValue(false)); $em = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock(); $em->expects($this->exactly(2))->method('getConfiguration')->will($this->returnValue($configuration)); $repo = $this->getMockBuilder('Oro\\Bundle\\SegmentBundle\\Entity\\Repository\\SegmentSnapshotRepository')->disableOriginalConstructor()->getMock(); $repo->expects($this->once())->method('getIdentifiersSelectQueryBuilder')->with($segment)->will($this->returnValue(new QueryBuilder($em))); $em->expects($this->once())->method('getRepository')->with('OroSegmentBundle:SegmentSnapshot')->will($this->returnValue($repo)); $em->expects($this->any())->method('createQuery')->will($this->returnValue(new Query($em))); $staticSegmentQB = new StaticSegmentQueryBuilder($em); $staticSegmentQB->build($segment); }
public function testStaticApply() { $staticSegmentStub = new Segment(); $staticSegmentStub->setType(new SegmentType(SegmentType::TYPE_STATIC)); $staticSegmentStub->setEntity('Oro\\Bundle\\SegmentBundle\\Tests\\Unit\\Stub\\Entity\\CmsUser'); $filterData = ['value' => $staticSegmentStub]; $em = $this->getEM(); $qb = $em->createQueryBuilder()->select(['t1.name'])->from('OroSegmentBundle:CmsUser', 't1'); $queryBuilder = new QueryBuilder($em); $queryBuilder->select(['ts1.id'])->from('OroSegmentBundle:SegmentSnapshot', 'ts1')->andWhere('ts1.segmentId = :segment')->setParameter('segment', self::TEST_PARAM_VALUE); $ds = new OrmFilterDatasourceAdapter($qb); $this->staticSegmentQueryBuilder->expects(static::once())->method('getQueryBuilder')->with($staticSegmentStub)->will(static::returnValue($queryBuilder)); $this->filter->init('someName', [FilterUtility::DATA_NAME_KEY => self::TEST_FIELD_NAME]); $this->filter->apply($ds, $filterData); $expectedResult = ['SELECT t1.name FROM OroSegmentBundle:CmsUser t1 WHERE', 'EXISTS(SELECT ts1.id FROM OroSegmentBundle:SegmentSnapshot ts1 ' . 'WHERE ts1.segmentId = :segment AND ts1.integerEntityId = t1.id)']; $expectedResult = implode(' ', $expectedResult); static::assertEquals($expectedResult, $ds->getQueryBuilder()->getDQL()); $params = $ds->getQueryBuilder()->getParameters(); static::assertCount(1, $params, 'Should pass params to main query builder'); static::assertEquals(self::TEST_PARAM_VALUE, $params[0]->getValue()); }
public function testStaticApply() { $staticSegmentStub = new Segment(); $staticSegmentStub->setType(new SegmentType(SegmentType::TYPE_STATIC)); $filterData = ['value' => $staticSegmentStub]; $subquery = 'SELECT ts1.entity_id FROM OroSegmentBundle:SegmentSnapshot ts1 WHERE ts1.segmentId = :segment'; $em = $this->getTestEntityManager(); $qb = new QueryBuilder($em); $qb->select(['t1.name'])->from('OroSegmentBundle:CmsUser', 't1'); $ds = new OrmFilterDatasourceAdapter($qb); $query = new Query($em); $query->setDQL($subquery); $query->setParameter('segment', $staticSegmentStub); $this->staticSegmentQueryBuilder->expects($this->once())->method('build')->with($staticSegmentStub)->will($this->returnValue($query)); $this->filter->init('someName', [FilterUtility::DATA_NAME_KEY => self::TEST_FIELD_NAME]); $this->filter->apply($ds, $filterData); $expectedResult = ['SELECT t1.name FROM OroSegmentBundle:CmsUser t1 WHERE t1.id', 'IN(SELECT ts1.entity_id FROM OroSegmentBundle:SegmentSnapshot ts1 WHERE ts1.segmentId = :segment)']; $expectedResult = implode(' ', $expectedResult); $this->assertEquals($expectedResult, $ds->getQueryBuilder()->getDQL()); $params = $ds->getQueryBuilder()->getParameters(); $this->assertCount(1, $params, 'Should pass params to main query builder'); $this->assertEquals($staticSegmentStub, $params[0]->getValue()); }