You are here

public function JSDelivrTest::test_search in Libraries CDN API 7

@covers ::search()

File

tests/src/Plugin/LibrariesCDN/JSDelivrTest.php, line 337
Contains \Drupal\Tests\Libraries_cdn\Plugin\LibrariesCDN\JSDelivrTest.

Class

JSDelivrTest
@coversDefaultClass \Drupal\libraries_cdn\Plugin\LibrariesCDN\JSDelivr

Namespace

Drupal\Tests\libraries_cdn\Plugin\LibrariesCDN

Code

public function test_search() {

  // Test 1
  $this->plugin
    ->setLibrary('jquery');
  $results = array(
    array(
      'name' => 'test1',
    ),
    array(
      'name' => 'test2',
    ),
    array(
      'name' => 'test3',
    ),
  );
  $urls = $this->plugin
    ->getConfiguration('urls');
  $url = sprintf($urls['isAvailable'], $this->plugin
    ->getLibrary());
  $this->drupal7
    ->shouldReceive('drupal_http_request')
    ->with($url, array())
    ->andReturn(array(
    'code' => 200,
    'data' => json_encode($results),
  ));
  $url = sprintf($urls['search'], $this->plugin
    ->getLibrary());
  $this->drupal7
    ->shouldReceive('drupal_http_request')
    ->with($url, array())
    ->andReturn(array(
    'code' => 200,
    'data' => json_encode($results),
  ));
  $data = $this->plugin
    ->search($this->plugin
    ->getLibrary());
  $results = $this->plugin
    ->formatData('search', $results);
  foreach ($data as $key => $result) {
    $this
      ->assertEquals($results[$key]['name'], $result['name']);
  }

  // Test 2
  $this->plugin
    ->setLibrary('It\'s a trap');
  $urls = $this->plugin
    ->getConfiguration('urls');
  $url = sprintf($urls['isAvailable'], $this->plugin
    ->getLibrary());
  $this->drupal7
    ->shouldReceive('drupal_http_request')
    ->with($url, array())
    ->andReturn(array(
    'code' => 200,
    'data' => json_encode($results),
  ));
  $url = sprintf($urls['search'], $this->plugin
    ->getLibrary());
  $this->drupal7
    ->shouldReceive('drupal_http_request')
    ->with($url, array())
    ->andReturn(array(
    'code' => 500,
  ));
  $data = $this->plugin
    ->search($this->plugin
    ->getLibrary());
  $this
    ->assertEmpty($data);
}