PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/OData Producer for PHP/Tests/UriProcessor/QueryProcessor/ExpressionParser/ExpressionParser2Test.php

#
PHP | 110 lines | 93 code | 7 blank | 10 comment | 0 complexity | f52dd5cd90e1a9273c26f785204baab1 MD5 | raw file
  1<?php
  2use ODataProducer\Providers\Metadata\ResourceProperty;
  3require_once 'PHPUnit\Framework\Assert.php';
  4require_once 'PHPUnit\Framework\Test.php';
  5require_once 'PHPUnit\Framework\SelfDescribing.php';
  6require_once 'PHPUnit\Framework\TestCase.php';
  7require_once 'PHPUnit\Framework\TestSuite.php';
  8require_once 'ODataProducer\Common\ClassAutoLoader.php';
  9require_once (dirname(__FILE__) . "\..\..\..\Resources\NorthWindMetadata.php");
 10use ODataProducer\UriProcessor\QueryProcessor\ExpressionParser\ExpressionParser2;
 11use ODataProducer\Common\ODataException;
 12ODataProducer\Common\ClassAutoLoader::register();
 13class PHPExpressionProviderTest extends PHPUnit_Framework_TestCase
 14{
 15    private $_northWindMetadata;
 16    
 17    protected function setUp()
 18    {        
 19        $this->_northWindMetadata = CreateNorthWindMetadata3::Create();
 20    }
 21
 22    public function testparseExpression2()
 23    {
 24        try {
 25              $odataUriExpression = 'UnitPrice ge 6';
 26              $resourceType = $this->_northWindMetadata->resolveResourceSet('Order_Details')->getResourceType();
 27              $internalFilterInfo = ExpressionParser2::parseExpression2($odataUriExpression, $resourceType, null);
 28              $this->assertTrue(!is_null($internalFilterInfo));
 29              $filterInfo = $internalFilterInfo->getFilterInfo();
 30              $this->assertTrue(!is_null($filterInfo));
 31              $navigationsUsed = $filterInfo->getNavigationPropertiesUsed();
 32              //Ther is no navigation in the expression so should be null.
 33              $this->assertTrue(is_null($navigationsUsed));
 34              $filterFunction = $internalFilterInfo->getFilterFunction();
 35              $whereCode = $filterFunction->getCode();
 36              $this->assertEquals($whereCode, 'if((!(is_null($lt->UnitPrice)) && ($lt->UnitPrice >= 6))) { return true; } else { return false;}');
 37
 38              $odataUriExpression = 'Order/Customer/CustomerID eq \'ANU\' or Product/ProductID gt 123 and UnitPrice ge 6';
 39              $internalFilterInfo = ExpressionParser2::parseExpression2($odataUriExpression, $resourceType, null);
 40              $this->assertTrue(!is_null($internalFilterInfo));
 41              $filterInfo = $internalFilterInfo->getFilterInfo();
 42              $this->assertTrue(!is_null($filterInfo));
 43              $navigationsUsed = $filterInfo->getNavigationPropertiesUsed();
 44              $this->assertTrue(!is_null($navigationsUsed));
 45              $this->assertTrue(is_array($navigationsUsed));
 46              $this->assertEquals(count($navigationsUsed), 2);
 47              //Order/Customer
 48              $this->assertTrue(is_array($navigationsUsed[0]));
 49              $this->assertEquals(count($navigationsUsed[0]), 2);
 50              //Product
 51              $this->assertTrue(is_array($navigationsUsed[1]));
 52              $this->assertEquals(count($navigationsUsed[1]), 1);              
 53              //Verify 'Order/Customer'
 54              $this->assertTrue(is_object($navigationsUsed[0][0]));
 55              $this->assertTrue(is_object($navigationsUsed[0][1]));
 56              $this->assertTrue($navigationsUsed[0][0] instanceof ResourceProperty);
 57              $this->assertTrue($navigationsUsed[0][1] instanceof ResourceProperty);
 58              $this->assertEquals($navigationsUsed[0][0]->getName(), 'Order');
 59              $this->assertEquals($navigationsUsed[0][1]->getName(), 'Customer');
 60              //Verify 'Product'
 61              $this->assertTrue(is_object($navigationsUsed[1][0]));              
 62              $this->assertTrue($navigationsUsed[1][0] instanceof ResourceProperty);              
 63              $this->assertEquals($navigationsUsed[1][0]->getName(), 'Product');
 64
 65              $odataUriExpression = 'Customer/Address/LineNumber add 4 eq 8';
 66              $resourceType = $this->_northWindMetadata->resolveResourceSet('Orders')->getResourceType();
 67              $internalFilterInfo = ExpressionParser2::parseExpression2($odataUriExpression, $resourceType, null);
 68              $this->assertTrue(!is_null($internalFilterInfo));
 69              $filterInfo = $internalFilterInfo->getFilterInfo();
 70              $this->assertTrue(!is_null($filterInfo));
 71              $navigationsUsed = $filterInfo->getNavigationPropertiesUsed();
 72              //Customer
 73              $this->assertTrue(!is_null($navigationsUsed));
 74              $this->assertTrue(is_array($navigationsUsed));
 75              $this->assertEquals(count($navigationsUsed), 1);
 76              $this->assertTrue(is_array($navigationsUsed[0]));
 77              $this->assertEquals(count($navigationsUsed[0]), 1);
 78              //Verify 'Customer'
 79              $this->assertTrue(is_object($navigationsUsed[0][0]));
 80              $this->assertTrue($navigationsUsed[0][0] instanceof ResourceProperty);
 81              $this->assertEquals($navigationsUsed[0][0]->getName(), 'Customer');
 82
 83              //Test with property acess expression in function call
 84              $odataUriExpression = 'replace(Customer/CustomerID, \'LFK\', \'RTT\') eq \'ARTTI\'';              
 85              $internalFilterInfo = ExpressionParser2::parseExpression2($odataUriExpression, $resourceType, null);
 86              $this->assertTrue(!is_null($internalFilterInfo));
 87              $filterInfo = $internalFilterInfo->getFilterInfo();
 88              $this->assertTrue(!is_null($filterInfo));
 89              $navigationsUsed = $filterInfo->getNavigationPropertiesUsed();
 90              //Customer
 91              $this->assertTrue(!is_null($navigationsUsed));
 92              $this->assertTrue(is_array($navigationsUsed));
 93              $this->assertEquals(count($navigationsUsed), 1);
 94              $this->assertTrue(is_array($navigationsUsed[0]));
 95              $this->assertEquals(count($navigationsUsed[0]), 1);
 96              //Verify 'Customer'
 97              $this->assertTrue(is_object($navigationsUsed[0][0]));
 98              $this->assertTrue($navigationsUsed[0][0] instanceof ResourceProperty);
 99              $this->assertEquals($navigationsUsed[0][0]->getName(), 'Customer');
100              
101        } catch(ODataException $exception){
102            $this->fail('An unexpected ODataException has been raised');
103        }
104    }
105
106    protected function tearDown()
107    {    
108    }
109}
110?>