[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/externals/restful/tests/RESTful/ -> CoreTest.php (source)

   1  <?php
   2  
   3  namespace RESTful\Test;
   4  
   5  \RESTful\Bootstrap::init();
   6  \Httpful\Bootstrap::init();
   7  
   8  use RESTful\URISpec;
   9  use RESTful\Client;
  10  use RESTful\Registry;
  11  use RESTful\Fields;
  12  use RESTful\Query;
  13  use RESTful\Page;
  14  
  15  class Settings
  16  {
  17      public static $url_root = 'http://api.example.com';
  18  
  19      public static $agent = 'example-php';
  20  
  21      public static $version = '0.1.0';
  22  
  23      public static $api_key = null;
  24  }
  25  
  26  class Resource extends \RESTful\Resource
  27  {
  28      public static $fields, $f;
  29  
  30      protected static $_client, $_registry, $_uri_spec;
  31  
  32      public static function init()
  33      {
  34          self::$_client = new Client('Settings');
  35          self::$_registry = new Registry();
  36          self::$f = self::$fields = new Fields();
  37      }
  38  
  39      public static function getClient()
  40      {
  41          $class = get_called_class();
  42          return $class::$_client;
  43      }
  44  
  45      public static function getRegistry()
  46      {
  47          $class = get_called_class();
  48          return $class::$_registry;
  49      }
  50  
  51      public static function getURISpec()
  52      {
  53          $class = get_called_class();
  54          return $class::$_uri_spec;
  55      }
  56  }
  57  
  58  Resource::init();
  59  
  60  class A extends Resource
  61  {
  62      protected static $_uri_spec = null;
  63  
  64      public static function init()
  65      {
  66          self::$_uri_spec = new URISpec('as', 'id', '/');
  67          self::$_registry->add(get_called_class());
  68      }
  69  }
  70  
  71  A::init();
  72  
  73  class B extends Resource
  74  {
  75      protected static $_uri_spec = null;
  76  
  77      public static function init()
  78      {
  79          self::$_uri_spec = new URISpec('bs', 'id', '/');
  80          self::$_registry->add(get_called_class());
  81      }
  82  }
  83  
  84  B::init();
  85  
  86  class URISpecTest extends \PHPUnit_Framework_TestCase
  87  {
  88      public function testNoRoot()
  89      {
  90          $uri_spec = new URISpec('grapes', 'seed');
  91          $this->assertEquals($uri_spec->collection_uri, null);
  92  
  93          $result = $uri_spec->match('/some/raisins');
  94          $this->assertEquals($result, null);
  95  
  96          $result = $uri_spec->match('/some/grapes');
  97          $this->assertEquals($result, array('collection' => true));
  98  
  99          $result = $uri_spec->match('/some/grapes/1234');
 100          $expected = array(
 101              'collection' => false,
 102              'ids' => array('seed' => '1234')
 103              );
 104          $this->assertEquals($expected, $result);
 105      }
 106  
 107      public function testSingleId()
 108      {
 109          $uri_spec = new URISpec('tomatoes', 'stem', '/v1');
 110          $this->assertNotEquals($uri_spec->collection_uri, null);
 111  
 112          $result = $uri_spec->match('/some/tomatoes/that/are/green');
 113          $this->assertEquals($result, null);
 114  
 115          $result = $uri_spec->match('/some/tomatoes');
 116          $this->assertEquals($result, array('collection' => true));
 117  
 118          $result = $uri_spec->match('/some/tomatoes/4321');
 119          $expected = array(
 120              'collection' => false,
 121              'ids' => array('stem' => '4321')
 122              );
 123          $this->assertEquals($expected, $result);
 124      }
 125  
 126      public function testMultipleIds()
 127      {
 128          $uri_spec = new URISpec('tomatoes', array('stem', 'root'), '/v1');
 129          $this->assertNotEquals($uri_spec->collection_uri, null);
 130  
 131          $result = $uri_spec->match('/some/tomatoes/that/are/green');
 132          $this->assertEquals($result, null);
 133  
 134          $result = $uri_spec->match('/some/tomatoes');
 135          $this->assertEquals($result, array('collection' => true));
 136  
 137          $result = $uri_spec->match('/some/tomatoes/4321/1234');
 138          $expected = array(
 139              'collection' => false,
 140              'ids' => array('stem' => '4321', 'root' => '1234')
 141              );
 142          $this->assertEquals($expected, $result);
 143      }
 144  }
 145  
 146  class QueryTest extends \PHPUnit_Framework_TestCase
 147  {
 148      public function testParse()
 149      {
 150          $uri = '/some/uri?field2=123&sort=field5%2Cdesc&limit=101&field3.field4%5Bcontains%5D=hi';
 151          $query = new Query('Resource', $uri);
 152          $expected = array(
 153              'field2' => array('123'),
 154              'field3.field4[contains]' => array('hi')
 155              );
 156          $this->assertEquals($query->filters, $expected);
 157          $expected = array('field5,desc');
 158          $this->assertEquals($query->sorts, $expected);
 159          $this->assertEquals($query->size, 101);
 160      }
 161  
 162      public function testBuild()
 163      {
 164          $query = new Query('Resource', '/some/uri');
 165          $query->filter(Resource::$f->name->eq('Wonka Chocs'))
 166                ->filter(Resource::$f->support_email->endswith('gmail.com'))
 167                ->filter(Resource::$f->variable_fee_percentage->gte(3.5))
 168                ->sort(Resource::$f->name->asc())
 169                ->sort(Resource::$f->variable_fee_percentage->desc())
 170                ->limit(101);
 171          $this->assertEquals(
 172              $query->filters,
 173              array(
 174                  'name' => array('Wonka Chocs'),
 175                  'support_email[contains]' => array('gmail.com'),
 176                  'variable_fee_percentage[>=]'=> array(3.5)
 177                  )
 178              );
 179          $this->assertEquals(
 180              $query->sorts,
 181              array('name,asc', 'variable_fee_percentage,desc')
 182              );
 183          $this->assertEquals(
 184              $query->size,
 185              101
 186              );
 187      }
 188  }
 189  
 190  class PageTest extends \PHPUnit_Framework_TestCase
 191  {
 192      public function testConstruct()
 193      {
 194          $data = new \stdClass();
 195          $data->first_uri = 'some/first/uri';
 196          $data->previous_uri = 'some/previous/uri';
 197          $data->next_uri = null;
 198          $data->last_uri = 'some/last/uri';
 199          $data->limit= 25;
 200          $data->offset = 0;
 201          $data->total = 101;
 202          $data->items = array();
 203  
 204          $page = new Page(
 205              'Resource',
 206              '/some/uri',
 207              $data
 208              );
 209  
 210          $this->assertEquals($page->resource, 'Resource');
 211          $this->assertEquals($page->total, 101);
 212          $this->assertEquals($page->items, array());
 213          $this->assertTrue($page->hasPrevious());
 214          $this->assertFalse($page->hasNext());
 215      }
 216  }
 217  
 218  class ResourceTest extends \PHPUnit_Framework_TestCase
 219  {
 220      public function testQuery()
 221      {
 222          $query = A::query();
 223          $this->assertEquals(get_class($query), 'RESTful\Query');
 224      }
 225  
 226      public function testObjectify()
 227      {
 228          $a = new A(array(
 229              'uri' => '/as/123',
 230              'field1' => 123,
 231              'b' => array(
 232                  'uri' => '/bs/321',
 233                  'field2' => 321
 234              ))
 235          );
 236          $this->assertEquals(get_class($a), 'RESTful\Test\A');
 237          $this->assertEquals($a->field1, 123);
 238          $this->assertEquals(get_class($a->b), 'RESTful\Test\B');
 239          $this->assertEquals($a->b->field2, 321);
 240      }
 241  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1