public function testGetConnectionListQueryBuilder() { /** @var CalendarConnectionRepository $repo */ $repo = $this->em->getRepository('OroCalendarBundle:CalendarConnection'); $qb = $repo->getConnectionListQueryBuilder(1); $this->assertEquals('SELECT calendarConnection, connectedCalendar, owner' . ' FROM Oro\\Bundle\\CalendarBundle\\Entity\\CalendarConnection calendarConnection' . ' INNER JOIN calendarConnection.calendar calendar' . ' INNER JOIN calendarConnection.connectedCalendar connectedCalendar' . ' INNER JOIN connectedCalendar.owner owner' . ' WHERE calendar.id = :id ORDER BY calendarConnection.createdAt ASC', $qb->getQuery()->getDQL()); }
public function testWalkerWithSeveralAndWhereCondition() { $query = $this->em->getRepository('Test:CmsAddress')->createQueryBuilder('address')->select('address.id')->where('address.id = 1 AND address.country = \'us\'')->getQuery(); $query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, ['Oro\\Bundle\\SecurityBundle\\ORM\\Walker\\CurrentUserWalker']); $query->setHint(CurrentUserWalker::HINT_SECURITY_CONTEXT, ['user' => 123, 'organization' => 456]); $this->assertEquals('SELECT c0_.id AS id_0' . ' FROM cms_addresses c0_' . ' WHERE c0_.id = 1 AND c0_.country = \'us\'' . ' AND c0_.user_id = 123 AND c0_.organization = 456', $query->getSQL()); }
public function testGetEventListByTimeIntervalQueryBuilderWithAdditionalFiltersAsArray() { /** @var CalendarEventRepository $repo */ $repo = $this->em->getRepository('OroCalendarBundle:CalendarEvent'); $qb = $repo->getEventListByTimeIntervalQueryBuilder(1, new \DateTime(), new \DateTime(), false, ['allDay' => true]); $this->assertEquals('SELECT c.id as calendar, e.id, e.title, e.description, e.start, e.end, e.allDay, e.createdAt, e.updatedAt' . ' FROM Oro\\Bundle\\CalendarBundle\\Entity\\CalendarEvent e' . ' INNER JOIN e.calendar c' . ' WHERE e.allDay = :allDay AND c.id = :id' . ' AND (' . '(e.start < :start AND e.end >= :start) OR ' . '(e.start <= :end AND e.end > :end) OR' . '(e.start >= :start AND e.end < :end))' . ' ORDER BY c.id, e.start ASC', $qb->getQuery()->getDQL()); $this->assertTrue($qb->getQuery()->getParameter('allDay')->getValue()); }
protected function setUp() { $reader = new AnnotationReader(); $metadataDriver = new AnnotationDriver($reader, ['Oro\\Bundle\\CalendarBundle\\Entity', 'Oro\\Bundle\\UserBundle\\Entity']); $this->em = $this->getTestEntityManager(); $this->em->getConfiguration()->setMetadataDriverImpl($metadataDriver); $this->em->getConfiguration()->setEntityNamespaces(['OroCalendarBundle' => 'Oro\\Bundle\\CalendarBundle\\Entity', 'OroUserBundle' => 'Oro\\Bundle\\UserBundle\\Entity']); }
protected function setUp() { $reader = new AnnotationReader(); $metadataDriver = new AnnotationDriver($reader, 'Oro\\Bundle\\OrganizationBundle\\Tests\\Unit\\Ownership\\Fixture\\Entity'); $this->em = $this->getTestEntityManager(); $this->em->getConfiguration()->setMetadataDriverImpl($metadataDriver); $this->em->getConfiguration()->setEntityNamespaces(['Stub' => 'Oro\\Bundle\\OrganizationBundle\\Tests\\Unit\\Ownership\\Fixture\\Entity']); }
protected function setUp() { $reader = new AnnotationReader(); $metadataDriver = new AnnotationDriver($reader, 'Oro\\Bundle\\DataAuditBundle\\Tests\\Unit\\Fixture'); $this->em = $this->getTestEntityManager(); $this->em->getConfiguration()->setEntityNamespaces(array('OroUserBundle' => 'Oro\\Bundle\\UserBundle\\Entity', 'OroDataAuditBundle' => 'Oro\\Bundle\\DataAuditBundle\\Entity')); $this->em->getConfiguration()->setMetadataDriverImpl($metadataDriver); $this->loggableAnnotationDriver = new \Oro\Bundle\DataAuditBundle\Metadata\Driver\AnnotationDriver(new AnnotationReader()); }
public function testGetAssociatedNotesQueryBuilderWithPaging() { /** @var NoteRepository $repo */ $repo = $this->em->getRepository('OroNoteBundle:Note'); $qb = $repo->getAssociatedNotesQueryBuilder('Test\\Entity', 123, 10, 50); $this->assertEquals('SELECT partial note.{id, message, owner, createdAt, updatedBy, updatedAt}, c, u' . ' FROM Oro\\Bundle\\NoteBundle\\Entity\\Note note' . ' INNER JOIN Test\\Entity e WITH note.entity_2929d33a = e' . ' LEFT JOIN note.owner c' . ' LEFT JOIN note.updatedBy u' . ' WHERE e.id IN(123)', $qb->getDQL()); $this->assertEquals(450, $qb->getFirstResult()); $this->assertEquals(50, $qb->getMaxResults()); }
public function testGetCalendarsQueryBuilder() { $organizationId = 1; /** @var SystemCalendarRepository $repo */ $repo = $this->em->getRepository('OroCalendarBundle:SystemCalendar'); $qb = $repo->getCalendarsQueryBuilder($organizationId); $this->assertEquals('SELECT sc' . ' FROM Oro\\Bundle\\CalendarBundle\\Entity\\SystemCalendar sc' . ' WHERE sc.organization = :organizationId OR sc.public = :public', $qb->getQuery()->getDQL()); $this->assertEquals($organizationId, $qb->getParameter('organizationId')->getValue()); $this->assertTrue($qb->getParameter('public')->getValue()); }
/** * @dataProvider inverseProvider */ public function testBuildComparisonExprWithUnidirectionalJoin($inverse) { $qb = $this->em->createQueryBuilder()->select('o.id, p1.id')->from('Stub:TestOrder', 'o')->leftJoin('o.products', 'p')->leftJoin('Stub:TestComment', 'c', 'WITH', 'c.products = p AND p.id = 5')->leftJoin('c.products', 'p1'); $ds = $this->getFilterDatasourceAdapter($qb); $expr = $this->builder->buildComparisonExpr($ds, 'p1.orders', 'param1', 'test', $inverse); $qb->where($expr); $result = $qb->getDQL(); $operator = $inverse ? 'NOT IN' : 'IN'; $this->assertEquals('SELECT o.id, p1.id FROM Stub:TestOrder o' . ' LEFT JOIN o.products p' . ' LEFT JOIN Stub:TestComment c WITH c.products = p AND p.id = 5' . ' LEFT JOIN c.products p1' . ' WHERE p1 ' . $operator . '(' . 'SELECT filter_test' . ' FROM ' . self::NS . 'TestProduct filter_test' . ' INNER JOIN filter_test.orders filter_test_rel' . ' WHERE filter_test_rel IN(:param1))', $result); }
public function testGetEmptyOutdatedFoldersByOriginQueryBuilder() { $origin = new ImapEmailOrigin(); /** @var ImapEmailFolderRepository $repo */ $repo = $this->em->getRepository('OroImapBundle:ImapEmailFolder'); $qb = $repo->getEmptyOutdatedFoldersByOriginQueryBuilder($origin); $query = $qb->getQuery(); $this->assertEquals('SELECT imap_folder' . ' FROM Oro\\Bundle\\ImapBundle\\Entity\\ImapEmailFolder imap_folder' . ' INNER JOIN imap_folder.folder folder' . ' LEFT JOIN folder.emailUsers emailUsers' . ' WHERE (folder.outdatedAt IS NOT NULL AND emailUsers.id IS NULL) AND folder.origin = :origin', $query->getDQL()); $this->assertSame($origin, $query->getParameter('origin')->getValue()); }
public function testGetTaskListByTimeIntervalQueryBuilder() { $entityClassName = 'Test\\Entity'; $entityIds = [1, 2, 3]; /** @var ReminderRepository $repo */ $repo = $this->em->getRepository('OroReminderBundle:Reminder'); $qb = $repo->findRemindersByEntitiesQueryBuilder($entityClassName, $entityIds); $this->assertEquals('SELECT reminder' . ' FROM Oro\\Bundle\\ReminderBundle\\Entity\\Reminder reminder' . ' WHERE reminder.relatedEntityClassName = :className AND reminder.relatedEntityId IN (:ids)', $qb->getDQL()); $this->assertEquals($entityClassName, $qb->getParameter('className')->getValue()); $this->assertEquals($entityIds, $qb->getParameter('ids')->getValue()); }
public function testGetUserCalendarsQueryBuilder() { $organizationId = 1; $userId = 123; /** @var CalendarRepository $repo */ $repo = $this->em->getRepository('OroCalendarBundle:Calendar'); $qb = $repo->getUserCalendarsQueryBuilder($organizationId, $userId); $this->assertEquals('SELECT c' . ' FROM Oro\\Bundle\\CalendarBundle\\Entity\\Calendar c' . ' WHERE c.organization = :organizationId AND c.owner = :userId', $qb->getQuery()->getDQL()); $this->assertEquals($organizationId, $qb->getParameter('organizationId')->getValue()); $this->assertEquals($userId, $qb->getParameter('userId')->getValue()); }
public function testGetTaskListByTimeIntervalQueryBuilderWithAlias() { $targetCalendarId = 123; $alias = 'test'; /** @var CalendarPropertyRepository $repo */ $repo = $this->em->getRepository('OroCalendarBundle:CalendarProperty'); $qb = $repo->getConnectionsByTargetCalendarQueryBuilder($targetCalendarId, $alias); $this->assertEquals('SELECT connection' . ' FROM Oro\\Bundle\\CalendarBundle\\Entity\\CalendarProperty connection' . ' WHERE connection.targetCalendar = :targetCalendarId AND connection.calendarAlias = :alias', $qb->getDQL()); $this->assertEquals($targetCalendarId, $qb->getParameter('targetCalendarId')->getValue()); $this->assertEquals($alias, $qb->getParameter('alias')->getValue()); }
public function testGetEmailsByMessageIdsQueryBuilder() { $origin = new ImapEmailOrigin(); $messageIds = ['msg1', 'msg2']; /** @var ImapEmailRepository $repo */ $repo = $this->em->getRepository('OroImapBundle:ImapEmail'); $qb = $repo->getEmailsByMessageIdsQueryBuilder($origin, $messageIds); $query = $qb->getQuery(); $this->assertEquals('SELECT imap_email ' . 'FROM Oro\\Bundle\\ImapBundle\\Entity\\ImapEmail imap_email ' . 'INNER JOIN imap_email.imapFolder imap_folder ' . 'INNER JOIN imap_email.email email ' . 'INNER JOIN email.emailUsers email_users ' . 'INNER JOIN email_users.folder folder ' . 'WHERE folder.origin = :origin AND email.messageId IN (:messageIds)', $query->getDQL()); $this->assertSame($origin, $query->getParameter('origin')->getValue()); $this->assertEquals($messageIds, $query->getParameter('messageIds')->getValue()); }
protected function setUp() { $reader = new AnnotationReader(); $metadataDriver = new AnnotationDriver($reader, 'Oro\\Bundle\\SoapBundle\\Tests\\Unit\\Serializer\\Fixtures\\Entity'); $this->em = $this->getTestEntityManager(); $this->em->getConfiguration()->setMetadataDriverImpl($metadataDriver); $this->em->getConfiguration()->setEntityNamespaces(['Test' => 'Oro\\Bundle\\SoapBundle\\Tests\\Unit\\Serializer\\Fixtures\\Entity']); $doctrine = $this->getMockBuilder('Doctrine\\Common\\Persistence\\ManagerRegistry')->disableOriginalConstructor()->getMock(); $doctrine->expects($this->any())->method('getManagerForClass')->will($this->returnValue($this->em)); $doctrine->expects($this->any())->method('getAliasNamespace')->will($this->returnValueMap([['Test', 'Oro\\Bundle\\SoapBundle\\Tests\\Unit\\Serializer\\Fixtures\\Entity']])); $this->configManager = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Config\\ConfigManager')->disableOriginalConstructor()->getMock(); $this->container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface'); $this->serializer = new EntitySerializer($doctrine, $this->configManager, new EntityDataAccessor(), new EntityDataTransformer($this->container)); }
protected function setUp() { $reader = new AnnotationReader(); $metadataDriver = new AnnotationDriver($reader, 'Oro\\Bundle\\EntityExtendBundle\\Tests\\Unit\\Fixtures\\Associations'); $this->em = $this->getTestEntityManager(); $this->em->getConfiguration()->setMetadataDriverImpl($metadataDriver); $this->em->getConfiguration()->setEntityNamespaces(['Test' => 'Oro\\Bundle\\EntityExtendBundle\\Tests\\Unit\\Fixtures\\Associations']); $this->configManager = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Config\\ConfigManager')->disableOriginalConstructor()->getMock(); $this->eventDispatcher = $this->getMockBuilder('Symfony\\Component\\EventDispatcher\\EventDispatcher')->disableOriginalConstructor()->getMock(); $doctrine = $this->getMockBuilder('Doctrine\\Common\\Persistence\\ManagerRegistry')->disableOriginalConstructor()->getMock(); $doctrine->expects($this->any())->method('getManagerForClass')->willReturn($this->em); $this->doctrineHelper = new DoctrineHelper($doctrine); $this->entityNameResolver = $this->getMockBuilder('Oro\\Bundle\\EntityBundle\\Provider\\EntityNameResolver')->disableOriginalConstructor()->getMock(); $this->associationManager = new AssociationManager($this->configManager, $this->eventDispatcher, $this->doctrineHelper, $this->entityNameResolver); }
public function testApplyNot() { $qb = $this->em->createQueryBuilder()->select('o.id')->from('Stub:TestEntity', 'o'); $values = [new TestEnumValue('val1', 'Value1'), new TestEnumValue('val2', 'Value2')]; $data = ['type' => ChoiceFilterType::TYPE_NOT_CONTAINS, 'value' => $values]; $params = ['null_value' => ':empty:', FilterUtility::DATA_NAME_KEY => 'o.values']; $this->filter->init('test', $params); /** @var OrmFilterDatasourceAdapter|\PHPUnit_Framework_MockObject_MockObject $ds */ $ds = $this->getMock('Oro\\Bundle\\FilterBundle\\Datasource\\Orm\\OrmFilterDatasourceAdapter', ['generateParameterName'], [$qb]); $ds->expects($this->any())->method('generateParameterName')->will($this->returnValue('param1')); $this->filter->apply($ds, $data); $result = $qb->getQuery()->getDQL(); $this->assertEquals('SELECT o.id FROM Stub:TestEntity o WHERE test NOT IN(:param1)', $result); $this->assertEquals($values, $qb->getParameter('param1')->getValue()); }
public function testGetTaskListByTimeIntervaWithExtraFieldslQueryBuilder() { $userId = 123; $startDate = new \DateTime(); $endDate = clone $startDate; $endDate->add(new \DateInterval('P1D')); $extraFields = ['status']; /** @var TaskRepository $repo */ $repo = $this->em->getRepository('OroCRMTaskBundle:Task'); $qb = $repo->getTaskListByTimeIntervalQueryBuilder($userId, $startDate, $endDate, $extraFields); $this->assertEquals('SELECT t.id, t.subject, t.description, t.dueDate, t.createdAt, t.updatedAt, t.status' . ' FROM OroCRM\\Bundle\\TaskBundle\\Tests\\Unit\\Fixtures\\Entity\\Task t' . ' WHERE t.owner = :assignedTo AND t.dueDate >= :start AND t.dueDate <= :end', $qb->getDQL()); $this->assertEquals($userId, $qb->getParameter('assignedTo')->getValue()); $this->assertEquals($startDate, $qb->getParameter('start')->getValue()); $this->assertEquals($endDate, $qb->getParameter('end')->getValue()); }
public function testIteratorWithArrayHydrationMode() { $records = [['a0' => '1'], ['a0' => '2'], ['a0' => '3']]; $actualSqls = []; $statementCounter = 0; $statements = [$this->createFetchStatementMock([['sclr0' => count($records)]]), $this->createFetchStatementMock([$records[0], $records[1], $records[2]])]; $this->getDriverConnectionMock($this->em)->expects($this->any())->method('query')->will($this->returnCallback(function ($sql) use(&$statements, &$statementCounter, &$actualSqls) { $actualSqls[$statementCounter] = $sql; $statement = $statements[$statementCounter]; $statementCounter++; return $statement; })); $source = $this->em->createQueryBuilder()->select('o')->from('Stub:Entity', 'o'); $iterator = new BufferedQueryResultIterator($source); $iterator->setHydrationMode(Query::HYDRATE_ARRAY); $this->assertEquals(count($records), $iterator->count()); $count = 0; foreach ($iterator as $record) { $this->assertEquals($records[$count]['a0'], $record['a']); $count++; } $this->assertEquals(count($records), $count); $this->assertEquals('SELECT count(e0_.a) AS sclr0 FROM Entity e0_', $actualSqls[0]); $this->assertEquals('SELECT e0_.a AS a0, e0_.b AS b1 FROM Entity e0_ LIMIT ' . BufferedQueryResultIterator::DEFAULT_BUFFER_SIZE . ' OFFSET 0', $actualSqls[1]); }
public function testIteratorInReverseDirection() { $records = [['a0' => '1'], ['a0' => '2'], ['a0' => '3']]; $actualSqls = []; $statementCounter = 0; $statements = [$this->createFetchStatementMock([['sclr0' => count($records)]]), $this->createFetchStatementMock([$records[0], $records[1]]), $this->createFetchStatementMock([$records[2]])]; $this->getDriverConnectionMock($this->em)->expects($this->any())->method('query')->will($this->returnCallback(function ($sql) use(&$statements, &$statementCounter, &$actualSqls) { $actualSqls[$statementCounter] = $sql; $statement = $statements[$statementCounter]; $statementCounter++; return $statement; })); $source = $this->em->createQueryBuilder()->select('o')->from('Stub:Entity', 'o'); $iterator = new BufferedQueryResultIterator($source); $iterator->setReverse(true); $iterator->setBufferSize(2); $this->assertEquals(count($records), $iterator->count()); $count = 0; foreach ($iterator as $record) { $this->assertInstanceOf('Oro\\Bundle\\BatchBundle\\Tests\\Unit\\ORM\\Query\\Stub\\Entity', $record); $this->assertEquals($records[$count]['a0'], $record->a); $count++; } $this->assertEquals(count($records), $count); $this->assertCount(3, $actualSqls); $this->assertEquals('SELECT count(e0_.a) AS sclr0 FROM Entity e0_', $actualSqls[0]); $this->assertEquals('SELECT e0_.a AS a0, e0_.b AS b1 FROM Entity e0_ LIMIT 2 OFFSET 2', $actualSqls[1]); $this->assertEquals('SELECT e0_.a AS a0, e0_.b AS b1 FROM Entity e0_ LIMIT 2 OFFSET 0', $actualSqls[2]); }
/** * @expectedException \RuntimeException * @expectedExceptionMessage The "Entity\NotRoot" must be the root entity. */ public function testAddFilterByTargetEntityWithInvalidActivityEntityClassSpecified() { $targetEntityClass = 'Oro\\Bundle\\ActivityBundle\\Tests\\Unit\\Fixtures\\Entity\\Target'; $targetEntityId = 123; $qb = $this->em->createQueryBuilder()->select('activity, another')->from('Test:Activity', 'activity')->from('Test:Another', 'another')->where('another.id = activity.id'); $this->manager->addFilterByTargetEntity($qb, $targetEntityClass, $targetEntityId, 'Entity\\NotRoot'); }
public function testGetInvitedUsersByParentsQueryBuilder() { $parentEventIds = [1, 2]; /** @var CalendarEventRepository $repo */ $repo = $this->em->getRepository('OroCalendarBundle:CalendarEvent'); $qb = $repo->getInvitedUsersByParentsQueryBuilder($parentEventIds); $this->assertEquals('SELECT IDENTITY(e.parent) AS parentEventId, e.id AS eventId, u.id AS userId' . ' FROM Oro\\Bundle\\CalendarBundle\\Entity\\CalendarEvent e' . ' INNER JOIN e.calendar c' . ' INNER JOIN c.owner u' . ' WHERE e.parent IN (:parentEventIds)', $qb->getQuery()->getDQL()); $this->assertEquals($parentEventIds, $qb->getQuery()->getParameter('parentEventIds')->getValue()); }
public function testApplyNullNot() { $qb = $this->em->createQueryBuilder()->select('o.id')->from('Stub:TestEntity', 'o'); $data = ['type' => ChoiceFilterType::TYPE_NOT_CONTAINS, 'value' => [':empty:']]; $params = ['null_value' => ':empty:', FilterUtility::DATA_NAME_KEY => 'o.values']; $this->filter->init('test', $params); $this->filter->apply(new OrmFilterDatasourceAdapter($qb), $data); $result = $qb->getQuery()->getDQL(); $this->assertEquals('SELECT o.id FROM Stub:TestEntity o' . ' WHERE o IN(' . 'SELECT null_filter_test' . ' FROM Stub:TestEntity null_filter_test' . ' LEFT JOIN null_filter_test.values null_filter_test_rel' . ' WHERE null_filter_test_rel IS NOT NULL)', $result); }
public function testGetClassName() { $this->assertEquals(DemoEntity::ENTITY_NAME, $this->configProvider->getClassName(DemoEntity::ENTITY_NAME)); $className = DemoEntity::ENTITY_NAME; $demoEntity = new $className(); $this->assertEquals(DemoEntity::ENTITY_NAME, $this->configProvider->getClassName($demoEntity)); $this->assertEquals(DemoEntity::ENTITY_NAME, $this->configProvider->getClassName(array($demoEntity))); $classMetadata = $this->getMockBuilder('Doctrine\\ORM\\Mapping\\ClassMetadata')->disableOriginalConstructor()->getMock(); $classMetadata->expects($this->once())->method('getName')->will($this->returnValue(DemoEntity::ENTITY_NAME)); $connectionMock = new ConnectionMock(array(), new DriverMock()); $emMock = EntityManagerMock::create($connectionMock); $persistentCollection = new PersistentCollection($emMock, $classMetadata, new ArrayCollection()); $this->assertEquals(DemoEntity::ENTITY_NAME, $this->configProvider->getClassName($persistentCollection)); $this->setExpectedException('Oro\\Bundle\\EntityConfigBundle\\Exception\\RuntimeException'); $this->assertEquals(DemoEntity::ENTITY_NAME, $this->configProvider->getClassName(array())); }
/** * @dataProvider pageCallbackDataProvider */ public function testPageCallback(array $statements, $bufferSize, $expectedPages) { $statementCounter = 0; $this->getDriverConnectionMock($this->em)->expects($this->any())->method('query')->will($this->returnCallback(function ($sql) use(&$statements, &$statementCounter, &$actualSqls) { $actualSqls[$statementCounter] = $sql; $statement = $statements[$statementCounter]; $statementCounter++; return $statement; })); $source = $this->em->createQueryBuilder()->select('o')->from('Stub:Entity', 'o'); $iterator = (new BufferedQueryResultIterator($source))->setBufferSize($bufferSize); $pages = 0; $iterator->setPageCallback(function () use(&$pages) { $pages++; }); $this->assertEquals(0, $pages); iterator_to_array($iterator); $this->assertEquals($expectedPages, $pages); }
/** * Changes a connection object for the given entity manager * * @param Connection $connection * @param EntityManagerMock $em */ protected function setDriverConnection(Connection $connection, EntityManagerMock $em) { /** @var DriverMock $driver */ $driver = $em->getConnection()->getDriver(); $driver->setDriverConnection($connection); }