/** * {@inheritdoc} */ public function getDefinition($id) { if (!$this->hasDefinition($id)) { throw new LibraryDefinitionNotFoundException($id); } return $this->serializer->decode($this->getSerializedDefinition($id)); }
/** * Returns the library definition for the given ID. * * @param string $id * The library ID to retrieve the definition for. * * @return array * The library definition array parsed from the definition JSON file. * * @throws \Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException */ public function getDefinition($id) { if (!$this->hasDefinition($id)) { throw new LibraryDefinitionNotFoundException($id); } return $this->serializer->decode(file_get_contents($this->getFileUri($id))); }
public function assertPairs($expected_pairs) { $result = $this->connection->select('key_value_sorted', 't')->fields('t', array('name', 'value'))->condition('collection', $this->collection)->condition('name', array_keys($expected_pairs), 'IN')->execute()->fetchAllAssoc('name'); $expected_count = count($expected_pairs); $this->assertIdentical(count($result), $expected_count, "Query affected {$expected_count} records."); foreach ($expected_pairs as $key => $value) { $this->assertIdentical($this->serializer->decode($result[$key]->value), $value, "Key {$key} have value {$value}"); } }
/** * {@inheritdoc} */ public function getRange($start, $stop = NULL) { $query = $this->connection->select($this->table, 't')->fields('t', array('value'))->condition('collection', $this->collection)->condition('name', $start, '>='); if ($stop !== NULL) { $query->condition('name', $stop, '<='); } $result = $query->orderBy('name', 'ASC')->execute(); $values = array(); foreach ($result as $item) { $values[] = $this->serializer->decode($item->value); } return $values; }
/** * Sends a request with the configuration provided. * * @param string $path * An optional path relative to the base uri in configuration. * @param [] $body * An optional body to attach to the request. * @param string $method * The method to use on the request, defaults to POST. * * @return mixed * An array of the body of the response from the server. * * @throws \Exception * When something goes wrong with the request (i.e 403). */ protected function doRequest($path = '', $body = [], $method = 'POST') { $settings = $this->configFactory->get('embridge.settings'); $uri = $settings->get('uri'); $uri = sprintf('%s/%s', $uri, $path); $options = ['timeout' => $settings->get('timeout'), 'cookies' => $this->cookieJar]; if (!empty($body['json'])) { $options['json'] = $body['json']; } elseif (!empty($body['multipart'])) { $options['multipart'] = $body['multipart']; } try { $response = $this->httpClient->request($method, $uri, $options); } catch (RequestException $e) { $response = $e->getResponse(); if ($response === NULL) { throw new \Exception('Error connecting to EMDB backend: ' . $e->getMessage()); } if ($response->getStatusCode() == 403) { throw new \Exception('Failed to authenticate with EMDB, please check your settings.'); } } if ($response->getStatusCode() != '200') { throw new \Exception('Unexpected response: [' . $response->getStatusCode() . '] ' . $response->getReasonPhrase()); } $body = $this->jsonEncoder->decode((string) $response->getBody()); if (empty($body['response']['status']) || $body['response']['status'] != 'ok') { throw new \Exception(sprintf('The request to EnterMedia failed.')); } return $body; }
/** * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::getAll(). */ public function getAll() { $result = $this->connection->query('SELECT name, value FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection', array(':collection' => $this->collection)); $values = array(); foreach ($result as $item) { if ($item) { $values[$item->name] = $this->serializer->decode($item->value); } } return $values; }
/** * Tests search() success. * * @covers ::login * @covers ::upload * @covers ::doRequest * * @test */ public function searchCorrectlyPassesParametersToRequest() { $mock_login_response = $this->getMockBuilder('\\GuzzleHttp\\Psr7\\Response')->disableOriginalConstructor()->getMock(); $mock_login_response->expects($this->once())->method('getStatusCode')->willReturn(200); $mock_login_response->expects($this->once())->method('getBody')->willReturn(file_get_contents('expected/login-expected-good-response.json', TRUE)); // This sucks, returnValueMap wasn't working though. $this->client->expects($this->at(0))->method('request')->with('POST', self::EXAMPLE_LOGIN_URL, $this->defaultLoginOptions)->willReturn($mock_login_response); $mock_search_response = $this->getMockBuilder('\\GuzzleHttp\\Psr7\\Response')->disableOriginalConstructor()->getMock(); $mock_search_response->expects($this->once())->method('getStatusCode')->willReturn(200); $search_response_body = file_get_contents('expected/search-expected-good-response.json', TRUE); $mock_search_response->expects($this->once())->method('getBody')->willReturn($search_response_body); $options = $this->defaultOptions; $body = ['page' => 2, 'hitsperpage' => 10, 'showfilters' => "true", 'query' => ['terms' => [['field' => 'name', 'operator' => 'matches', 'value' => 'test*']]]]; $options['json'] = $body; $this->client->expects($this->at(1))->method('request')->with('POST', self::EXAMPLE_SEARCH_URL, $options)->willReturn($mock_search_response); $decoded_body = $this->serializer->decode($search_response_body); $filters = [['field' => 'name', 'operator' => 'matches', 'value' => 'test*']]; $this->assertEquals($decoded_body, $this->emdbClient->search(2, 10, $filters)); }
public static function decode($raw) { return static::$mock->decode($raw); }
/** * Returns the library definition for the given ID. * * @param string $id * The library ID to retrieve the definition for. * * @return array * The library definition array parsed from the definition JSON file. */ protected function getDefinition($id) { return $this->serializer->decode(file_get_contents($this->getFileUri($id))); }