コード例 #1
0
ファイル: CursorTest.php プロジェクト: hitmeister/api-sdk-php
 public function testIterateEmptyResult()
 {
     $this->transport->shouldReceive('performRequest')->once()->withArgs(['GET', 'categories/', ['limit' => 10, 'offset' => 100], \Mockery::any(), \Mockery::any()])->andReturn(['headers' => ['Hm-Collection-Range' => ['0-0/100']], 'json' => []]);
     $this->endpoint->setParams(['offset' => 100, 'limit' => 10]);
     $cursor = new Cursor($this->endpoint, '\\Hitmeister\\Component\\Api\\Transfers\\CategoryTransfer');
     // Iterate again (should not call performRequest)
     $count = 0;
     foreach ($cursor as $i => $item) {
         $count++;
     }
     $this->assertEquals(0, $count);
 }
コード例 #2
0
 /**
  * @param AbstractEndpoint $endpoint
  * @param int              $id
  * @return array|null
  */
 protected function performWithId(AbstractEndpoint $endpoint, $id)
 {
     if ($endpoint instanceof IdAware) {
         $endpoint->setId($id);
     }
     try {
         $result = $endpoint->performRequest();
     } catch (ResourceNotFoundException $e) {
         return null;
     }
     Response::checkBody($result);
     return $result['json'];
 }
コード例 #3
0
 public function testGetSet()
 {
     $builder = new FindBuilder($this->endpoint, '\\stdClass');
     $builder->setParams(['one' => 1, 'two' => 2, 'nothing' => null]);
     $this->assertEquals(['one' => 1, 'two' => 2], $builder->getParams());
     $builder->setParams(['one' => 1, 'two' => null]);
     $this->assertEquals(['one' => 1], $builder->getParams());
     // Reset
     $builder->setParams([]);
     $builder->addParam('one', null);
     $this->assertArrayNotHasKey('one', $builder->getParams());
     $builder->addParam('two', 2);
     $this->assertEquals(2, $builder->getParam('two'));
     $builder->addParam('two', null);
     $this->assertArrayNotHasKey('two', $builder->getParams());
     $this->assertEquals(100, $builder->getParam('two', 100));
     // Reset
     $builder->setParams([]);
     $builder->setOffset(100);
     $this->assertEquals(100, $builder->getOffset());
     $builder->setLimit(40);
     $this->assertEquals(40, $builder->getLimit());
     $builder->setSort('date:asc');
     $this->assertEquals('date:asc', $builder->getSort());
     $builder->setOffset();
     $this->assertEquals(0, $builder->getOffset());
     $builder->setLimit();
     $this->assertEquals(null, $builder->getLimit());
     $builder->setSort(null);
     $this->assertEquals(null, $builder->getSort());
     // Reset
     $builder->setParams([]);
     $d = time() + 100;
     $dt = time() - 100;
     /** @var \Mockery\Mock $mock */
     $mock = \Mockery::mock('alias:\\Hitmeister\\Component\\Api\\Helper\\Request');
     $mock->shouldReceive('formatDate')->withArgs([$d])->once()->andReturn('date');
     $mock->shouldReceive('formatDateTime')->withArgs([$dt])->once()->andReturn('date_time');
     $builder->addDateParam('date', $d);
     $this->assertEquals('date', $builder->getParam('date'));
     $builder->addDateTimeParam('date_time', $dt);
     $this->assertEquals('date_time', $builder->getParam('date_time'));
     // Reset
     $builder->setParams(['one' => 1, 'two' => 2]);
     $this->endpoint->shouldReceive('getParamWhiteList')->andReturn(['one', 'two']);
     $cursor = $builder->find();
     $endpoint = $cursor->getEndpoint();
     $this->assertEquals(['one' => 1, 'two' => 2], $endpoint->getParams());
 }
コード例 #4
0
ファイル: Cursor.php プロジェクト: hitmeister/api-sdk-php
 /**
  *
  */
 private function fetchData()
 {
     if (!$this->apiHasNext) {
         return;
     }
     // Set limits defined by API
     $this->apiParams['limit'] = $this->apiLimit;
     $this->apiParams['offset'] = $this->apiOffset;
     // Adjust limits by user
     if (null !== $this->userLimit) {
         $expectedCount = $this->userOffset + $this->userLimit;
         $newCount = $this->apiLimit + $this->apiOffset;
         if ($newCount > $expectedCount) {
             $dx = $newCount - $expectedCount;
             $this->apiParams['limit'] -= $dx;
             if (!$this->apiParams['limit']) {
                 $this->apiHasNext = false;
                 return;
             }
         }
     }
     $this->endpoint->setParams($this->apiParams);
     $resultRequest = $this->endpoint->performRequest();
     Response::checkBody($resultRequest);
     $cursorData = Response::extractCursorPosition($resultRequest);
     $this->total = $cursorData['total'];
     // The end
     if ($cursorData['end'] == $cursorData['total'] || null !== $this->userOffset && null !== $this->userLimit && $cursorData['end'] >= $this->userOffset + $this->userLimit || 0 === count($resultRequest['json'])) {
         $this->apiHasNext = false;
     } else {
         $this->apiOffset += $this->apiLimit;
     }
     $this->rawData = array_merge($this->rawData, $resultRequest['json']);
 }
コード例 #5
0
 public function testGetParamsWithOptions()
 {
     $params = ['status' => 'new', 'client' => ['test_me' => 'hard']];
     $this->endpoint->shouldReceive('getParamWhiteList')->andReturn(['status']);
     $this->endpoint->setParams($params);
     $this->assertEquals($params, $this->endpoint->getParams());
 }
コード例 #6
0
 /**
  * @return Cursor
  */
 public function find()
 {
     $this->endpoint->setParams($this->params);
     return new Cursor($this->endpoint, $this->transferClass);
 }