tests/UnitTests/POData/UriProcessor/QueryProcessor/OrderByParser/OrderByParserTest.php
/**
     * A primitive property of a complex type can be used as sort key
     */
A primitive property of a complex type can be used as sort key
A primitive property of a complex type can be used as sort key
public function testOrderByPrimitiveInAComplex()
    {
        $northWindMetadata = NorthWindMetadata::Create();
        $configuration = new ServiceConfiguration($northWindMetadata);
        $configuration->setEntitySetAccessRule('*', EntitySetRights::ALL);
        $providersWrapper = new ProvidersWrapper(
                                          $northWindMetadata, 
	                                        $this->mockQueryProvider,
                                          $configuration, 
                                          false
                                         );
        $resourceSetWrapper = $providersWrapper->resolveResourceSet('Customers');
        $resourceType = $resourceSetWrapper->getResourceType();
        $orderBy = 'Address/HouseNumber';
        $internalOrderInfo = OrderByParser::parseOrderByClause($resourceSetWrapper, $resourceType, $orderBy, $providersWrapper);
        $orderByInfo = $internalOrderInfo->getOrderByInfo();
        $this->assertNull($orderByInfo->getNavigationPropertiesUsed());
        $orderByPathSegments = $orderByInfo->getOrderByPathSegments();
        $this->assertEquals(count($orderByPathSegments), 1);
        $this->assertTrue($orderByPathSegments[0]->isAscending());
        $subPathSegments = $orderByPathSegments[0]->getSubPathSegments();
        $this->assertNotNull($subPathSegments);
        $this->assertEquals(count($subPathSegments), 2);
        $this->assertEquals($subPathSegments[0]->getName(), 'Address');
        $this->assertEquals($subPathSegments[1]->getName(), 'HouseNumber');
        $this->assertTrue($subPathSegments[0]->getResourceProperty() instanceof ResourceProperty);
        $this->assertTrue($subPathSegments[1]->getResourceProperty() instanceof ResourceProperty);
        $subSorters = $internalOrderInfo->getSubSorterFunctions();
        $this->assertNotNull($subSorters);
        $this->assertEquals(count($subSorters), 1);
        $this->assertEquals($subSorters[0]->getParametersAsString(), '$CustomersA, $CustomersB');
        $subSorterName = $subSorters[0]->getReference();
        $sorter = $internalOrderInfo->getSorterFunction();
        $this->assertNotNull($sorter);
        $mainSorterName = $sorter->getReference();
        $this->assertEquals($subSorterName, $mainSorterName);
        $customer1 = new Customer2();
        $customer2 = new Customer2();
        $customer1->Address = null;
        $customer2->Address = null;
        $result = $mainSorterName($customer1, $customer2);
        $this->assertEquals($result, 0);
        $customer1->Address = new Address4();
        $customer1->Address->HouseNumber = null;
        $customer2->Address = new Address4();
        $customer2->Address->HouseNumber = '123';
        $result = $mainSorterName($customer1, $customer2);
        $this->assertEquals($result, -1);
        $customer1->Address = new Address4();
        $customer1->Address->HouseNumber = '123';
        $customer2->Address = new Address4();
        $customer2->Address->HouseNumber = null;
        $result = $mainSorterName($customer1, $customer2);
        $this->assertEquals($result, 1);
        $customer1->Address = new Address4();
        $customer1->Address->HouseNumber = 'ABC';
        $customer2->Address = new Address4();
        $customer2->Address->HouseNumber = 'DEF';
        $result = $mainSorterName($customer1, $customer2);
        $this->assertLessThan(0, $result);
        $customer1->Address = new Address4();
        $customer1->Address->HouseNumber = 'XYZ';
        $customer2->Address = new Address4();
        $customer2->Address->HouseNumber = 'PQR';
        $result = $mainSorterName($customer1, $customer2);
        $this->assertGreaterThan(0, $result);
        $customer1->Address = new Address4();
        $customer1->Address->HouseNumber = 'MN';
        $customer2->Address = new Address4();
        $customer2->Address->HouseNumber = 'MN';
        $result = $mainSorterName($customer1, $customer2);
        $this->assertEquals($result, 0);
    }
public function testOrderByPrimitiveInAComplex ( ) { $ northWindMetadata = NorthWindMetadata :: Create ( ) ; $ configuration = new ServiceConfiguration ( $ northWindMetadata ) ; $ configuration -> setEntitySetAccessRule ( '*' , EntitySetRights :: ALL ) ; $ providersWrapper = new ProvidersWrapper ( $ northWindMetadata , $ this -> mockQueryProvider , $ configuration , false ) ; $ resourceSetWrapper = $ providersWrapper -> resolveResourceSet ( 'Customers' ) ; $ resourceType = $ resourceSetWrapper -> getResourceType ( ) ; $ orderBy = 'Address/HouseNumber' ; $ internalOrderInfo = OrderByParser :: parseOrderByClause ( $ resourceSetWrapper , $ resourceType , $ orderBy , $ providersWrapper ) ; $ orderByInfo = $ internalOrderInfo -> getOrderByInfo ( ) ; $ this -> assertNull ( $ orderByInfo -> getNavigationPropertiesUsed ( ) ) ; $ orderByPathSegments = $ orderByInfo -> getOrderByPathSegments ( ) ; $ this -> assertEquals ( count ( $ orderByPathSegments ) , 1 ) ; $ this -> assertTrue ( $ orderByPathSegments [ 0 ] -> isAscending ( ) ) ; $ subPathSegments = $ orderByPathSegments [ 0 ] -> getSubPathSegments ( ) ; $ this -> assertNotNull ( $ subPathSegments ) ; $ this -> assertEquals ( count ( $ subPathSegments ) , 2 ) ; $ this -> assertEquals ( $ subPathSegments [ 0 ] -> getName ( ) , 'Address' ) ; $ this -> assertEquals ( $ subPathSegments [ 1 ] -> getName ( ) , 'HouseNumber' ) ; $ this -> assertTrue ( $ subPathSegments [ 0 ] -> getResourceProperty ( ) instanceof ResourceProperty ) ; $ this -> assertTrue ( $ subPathSegments [ 1 ] -> getResourceProperty ( ) instanceof ResourceProperty ) ; $ subSorters = $ internalOrderInfo -> getSubSorterFunctions ( ) ; $ this -> assertNotNull ( $ subSorters ) ; $ this -> assertEquals ( count ( $ subSorters ) , 1 ) ; $ this -> assertEquals ( $ subSorters [ 0 ] -> getParametersAsString ( ) , '$CustomersA, $CustomersB' ) ; $ subSorterName = $ subSorters [ 0 ] -> getReference ( ) ; $ sorter = $ internalOrderInfo -> getSorterFunction ( ) ; $ this -> assertNotNull ( $ sorter ) ; $ mainSorterName = $ sorter -> getReference ( ) ; $ this -> assertEquals ( $ subSorterName , $ mainSorterName ) ; $ customer1 = new Customer2 ( ) ; $ customer2 = new Customer2 ( ) ; $ customer1 -> Address = null ; $ customer2 -> Address = null ; $ result = $ mainSorterName ( $ customer1 , $ customer2 ) ; $ this -> assertEquals ( $ result , 0 ) ; $ customer1 -> Address = new Address4 ( ) ; $ customer1 -> Address -> HouseNumber = null ; $ customer2 -> Address = new Address4 ( ) ; $ customer2 -> Address -> HouseNumber = '123' ; $ result = $ mainSorterName ( $ customer1 , $ customer2 ) ; $ this -> assertEquals ( $ result , - 1 ) ; $ customer1 -> Address = new Address4 ( ) ; $ customer1 -> Address -> HouseNumber = '123' ; $ customer2 -> Address = new Address4 ( ) ; $ customer2 -> Address -> HouseNumber = null ; $ result = $ mainSorterName ( $ customer1 , $ customer2 ) ; $ this -> assertEquals ( $ result , 1 ) ; $ customer1 -> Address = new Address4 ( ) ; $ customer1 -> Address -> HouseNumber = 'ABC' ; $ customer2 -> Address = new Address4 ( ) ; $ customer2 -> Address -> HouseNumber = 'DEF' ; $ result = $ mainSorterName ( $ customer1 , $ customer2 ) ; $ this -> assertLessThan ( 0 , $ result ) ; $ customer1 -> Address = new Address4 ( ) ; $ customer1 -> Address -> HouseNumber = 'XYZ' ; $ customer2 -> Address = new Address4 ( ) ; $ customer2 -> Address -> HouseNumber = 'PQR' ; $ result = $ mainSorterName ( $ customer1 , $ customer2 ) ; $ this -> assertGreaterThan ( 0 , $ result ) ; $ customer1 -> Address = new Address4 ( ) ; $ customer1 -> Address -> HouseNumber = 'MN' ; $ customer2 -> Address = new Address4 ( ) ; $ customer2 -> Address -> HouseNumber = 'MN' ; $ result = $ mainSorterName ( $ customer1 , $ customer2 ) ; $ this -> assertEquals ( $ result , 0 ) ; }
A primitive property of a complex type can be used as sort key
A primitive property of a complex type can be used as sort key
//IMetadataProvider implementation //Service configuuration //There is no navigation (resource reference) property in the orderby path so getNavigationPropertiesUsed should //return null (not empty array) //There should be one orderby path segment with two sub path segments //no sort order, so default to asc //there are two sub path 'Address' and 'HouseNumber' //There is only one sub sorter function //Parmater to this sub sort must be CustomersA, CustomersB //since there is only one sub sorter, that will be the main sorter //asset this by comapring the anonymous function names //check code inside the anonymous function (see the generated function code) /**
            $flag1 = is_null($CustomersA) || is_null($CustomersA->Address) || is_null($CustomersA->Address->HouseNumber);
			$flag2 = is_null($CustomersB) || is_null($CustomersB->Address) || is_null($CustomersB->Address->HouseNumber);
			if($flag1 && $flag2) {
                return 0;
			} else if ($flag1) {
                return 1*-1;
			} else if ($flag2) {
                return 1*1;
			}

			$result = strcmp($CustomersA->Address->HouseNumber, $CustomersB->Address->HouseNumber);
			return 1*$result;
         */ //When any properties in the orderby path become null for both parameters then they are equal //When any properties in the orderby path become null for one parameter //and if the sort key for second parametr is not null, then second is greater //reverse, second is lesser //Try with not-null value for both sort key //'ABC' < 'DEF' //'XYZ' > 'PQR' //'MN' == ''MN'