class XmlTest in Views XML Backend 8
@coversDefaultClass \Drupal\views_xml_backend\Plugin\views\query\Xml @group views_xml_backend
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\views_xml_backend\Unit\ViewsXmlBackendTestBase
- class \Drupal\Tests\views_xml_backend\Unit\Plugin\views\query\XmlTest
- class \Drupal\Tests\views_xml_backend\Unit\ViewsXmlBackendTestBase
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\queryView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
ViewsXmlBackendTestBase:: |
protected | property | ||
ViewsXmlBackendTestBase:: |
protected | property | ||
ViewsXmlBackendTestBase:: |
protected | function | ||
ViewsXmlBackendTestBase:: |
protected | function | ||
XmlTest:: |
protected | property | The XML query object. | |
XmlTest:: |
protected | function | ||
XmlTest:: |
public | function |
Overrides ViewsXmlBackendTestBase:: |
|
XmlTest:: |
public | function | ||
XmlTest:: |
public | function | ||
XmlTest:: |
public | function |