You are here

public function JSDelivrTest::test_getFiles in Libraries CDN API 7

@covers ::getFiles()

File

tests/src/Plugin/LibrariesCDN/JSDelivrTest.php, line 482
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_getFiles() {

  // Test 1
  $this->plugin
    ->setLibrary('jquery');
  $results = array(
    'assets' => array(
      array(
        'files' => array(
          'test1.js',
          'test1.css',
        ),
        'version' => '1.0',
      ),
      array(
        'files' => array(
          'test2.js',
          'test2.css',
        ),
        'version' => '2.0',
      ),
      array(
        'files' => array(
          'test3.js',
          'test3.css',
        ),
        'version' => '3.0',
      ),
    ),
  );
  $urls = $this->plugin
    ->getConfiguration('urls');
  $url = sprintf($urls['isAvailable'], $this->plugin
    ->getLibrary());
  $this->drupal7
    ->shouldReceive('drupal_http_request')
    ->once()
    ->with($url, array())
    ->andReturn(array(
    'code' => 200,
    'data' => json_encode($results),
  ));
  $url = sprintf($urls['getFiles'], $this->plugin
    ->getLibrary());
  $this->drupal7
    ->shouldReceive('drupal_http_request')
    ->once()
    ->with($url, array())
    ->andReturn(array(
    'code' => 200,
    'data' => json_encode($results),
  ));
  $data = $this->plugin
    ->getFiles();
  $results = $this->plugin
    ->formatData('getFiles', $results);
  $this
    ->assertCount(count($results), $data);

  // Test 2
  $this->plugin
    ->setLibrary('jquery');
  $this->plugin
    ->setURL('isAvailable', 'http://api.jsdelivr.com/fake_url?search=%s');
  $urls = $this->plugin
    ->getConfiguration('urls');
  $this->drupal7
    ->shouldReceive('drupal_http_request')
    ->once()
    ->with(sprintf($urls['isAvailable'], $this->plugin
    ->getLibrary()), array())
    ->andReturn(array(
    'code' => 404,
  ));
  $this
    ->assertEmpty($this->plugin
    ->getFiles());
}