You are here

class XmlTest in Views XML Backend 8

@coversDefaultClass \Drupal\views_xml_backend\Plugin\views\query\Xml @group views_xml_backend

Hierarchy

Expanded class hierarchy of XmlTest

File

tests/src/Unit/Plugin/views/query/XmlTest.php, line 26
Contains \Drupal\Tests\views_xml_backend\Unit\Plugin\views\query\XmlTest.

Namespace

Drupal\Tests\views_xml_backend\Unit\Plugin\views\query
View source
class XmlTest extends ViewsXmlBackendTestBase {

  /**
   * The XML query object.
   *
   * @var \Drupal\views_xml_backend\Plugin\views\query\Xml
   */
  protected $query;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    if (!defined('FILE_MODIFY_PERMISSIONS')) {
      define('FILE_MODIFY_PERMISSIONS', 2);
    }
    if (!defined('FILE_CREATE_DIRECTORY')) {
      define('FILE_CREATE_DIRECTORY', 1);
    }
    if (!defined('FILE_EXISTS_REPLACE')) {
      define('FILE_EXISTS_REPLACE', 1);
    }
    vfsStream::setup('vxb');
    new Settings([
      'views_xml_backend_cache_directory' => 'vfs://vxb/filecache',
    ]);

    // Create a mock and queue two responses.
    $mock = new MockHandler([
      new Response(200, [
        'X-Foo' => 'Bar',
      ]),
    ]);
    $handler = HandlerStack::create($mock);
    $this->client = new Client([
      'handler' => $handler,
    ]);
    $this->nullCache = new NullBackend('bin');
    $this->nullLogger = new NullLogger();
    $this->messenger = new TestMessenger();
  }
  public function testExecuteWithEmptyRowXpathDisplaysMessage() {
    $query = $this
      ->getNewQueryObject([]);
    $view = $this
      ->getMockedView();
    $query
      ->init($view, $this
      ->getMockedDisplay());
    $query
      ->build($view);
    $query
      ->execute($view);
    $this
      ->assertSame(1, count($this->messenger
      ->getMessages()));
    $this
      ->assertSame(0, count($view->result));
  }
  public function testBasicXmlParsing() {
    $xml = <<<XML
<foo>
  <bar><value>1</value></bar>
  <bar><value>2</value></bar>
  <bar><value>3</value></bar>
  <bar><value>4</value></bar>
</foo>
XML;
    $mock = new MockHandler([
      new Response(200, [], $xml),
    ]);
    $handler = HandlerStack::create($mock);
    $this->client = new Client([
      'handler' => $handler,
    ]);
    $query = $this
      ->getNewQueryObject([
      'xml_file' => 'http://example.com',
      'row_xpath' => '/foo/bar',
    ]);
    $view = $this
      ->getMockedView();

    // Fake the fields.
    $view->field['field_1'] = (object) [
      'options' => [
        'xpath_selector' => 'value',
      ],
    ];
    $query
      ->build($view);
    $query
      ->execute($view);

    // Check for no errors.
    $this
      ->assertSame(0, count($this->messenger
      ->getMessages()));
    $this
      ->assertSame(4, count($view->result));
    foreach ($view->result as $index => $row) {
      $this
        ->assertSame($index, $row->index);
      $value = (string) ($index + 1);
      $this
        ->assertSame([
        0 => $value,
      ], $row->field_1);
    }
  }
  public function testAddExtraFieldsWorks() {
    $xml = <<<XML
<foo>
  <bar><value>1</value></bar>
  <bar><value>2</value></bar>
  <bar><value>3</value></bar>
  <bar><value>4</value></bar>
</foo>
XML;
    $mock = new MockHandler([
      new Response(200, [], $xml),
    ]);
    $handler = HandlerStack::create($mock);
    $this->client = new Client([
      'handler' => $handler,
    ]);
    $query = $this
      ->getNewQueryObject([
      'xml_file' => 'http://example.com',
      'row_xpath' => '/foo/bar',
    ]);
    $view = $this
      ->getMockedView();
    $query
      ->addField('field_1', 'value');
    $query
      ->build($view);
    $query
      ->execute($view);

    // Check for no errors.
    $this
      ->assertSame(0, count($this->messenger
      ->getMessages()));
    $this
      ->assertSame(4, count($view->result));
    foreach ($view->result as $index => $row) {
      $this
        ->assertSame($index, $row->index);
      $value = (string) ($index + 1);
      $this
        ->assertSame([
        0 => $value,
      ], $row->field_1);
    }
  }
  protected function getNewQueryObject(array $options) {
    $query = new Xml([], '', [], $this->client, $this->nullCache, $this->nullLogger, $this->messenger);
    $query
      ->setStringTranslation($this
      ->getStringTranslationStub());
    $query
      ->init($this
      ->getMockedView(), $this
      ->getMockedDisplay(), $options);
    return $query;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
ViewsXmlBackendTestBase::$display protected property
ViewsXmlBackendTestBase::$view protected property
ViewsXmlBackendTestBase::getMockedDisplay protected function
ViewsXmlBackendTestBase::getMockedView protected function
XmlTest::$query protected property The XML query object.
XmlTest::getNewQueryObject protected function
XmlTest::setUp public function Overrides ViewsXmlBackendTestBase::setUp
XmlTest::testAddExtraFieldsWorks public function
XmlTest::testBasicXmlParsing public function
XmlTest::testExecuteWithEmptyRowXpathDisplaysMessage public function