Skip to content

Commit

Permalink
Allow custom config files with virtual types only via generic schema …
Browse files Browse the repository at this point in the history
…locator

Using custom configuration files requires a number of class implementations.
Most of these can be satisfied by using generic implementations found within
the Magento\Framework\Config namespace and injectable parameters.

* Magento\Framework\Config\DataInterface
  => Magento\Framework\Config\Data
* Magento\Framework\Config\ReaderInterface
  => Magento\Framework\Config\Reader\Filesystem
* Magento\Framework\Config\ConverterInterface
  => Magento\Framework\Config\Converter\Dom

The one that is missing is a generic implementation of the schema locator
interface Magento\Framework\Config\SchemaLocatorInterface

This PR adds such a generic implementation.
  • Loading branch information
Vinai committed Jun 25, 2015
1 parent 8158c8c commit dd4e82a
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
67 changes: 67 additions & 0 deletions lib/internal/Magento/Framework/Config/GenericSchemaLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Magento\Framework\Config;

use Magento\Framework\Module\Dir\Reader as ModuleDirReader;

/**
* Class GenericSchemaLocator
*/
class GenericSchemaLocator implements SchemaLocatorInterface
{
/**
* @var ModuleDirReader
*/
private $moduleDirReader;

/**
* @var string
*/
private $moduleName;

/**
* @var string
*/
private $perFileSchema;

/**
* @var string|null
*/
private $schema;

/**
* @param ModuleDirReader $reader
* @param string $moduleName
* @param string $schema
* @param string|null $perFileSchema
*/
public function __construct(ModuleDirReader $reader, $moduleName, $schema, $perFileSchema = null)
{
$this->moduleDirReader = $reader;
$this->moduleName = $moduleName;
$this->schema = $schema;
$this->perFileSchema = $perFileSchema;
}

/**
* Get path to merged config schema
*
* @return string|null
*/
public function getSchema()
{
return $this->moduleDirReader->getModuleDir('etc', $this->moduleName) . '/' . $this->schema;
}

/**
* Get path to per file validation schema
*
* @return string|null
*/
public function getPerFileSchema()
{
return is_null($this->perFileSchema) ?
null :
$this->moduleDirReader->getModuleDir('etc', $this->moduleName) . '/' . $this->perFileSchema;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php


namespace Magento\Framework\Config\Test\Unit;

use Magento\Framework\Config\GenericSchemaLocator;
use Magento\Framework\Config\SchemaLocatorInterface;
use Magento\Framework\Module\Dir\Reader as ModuleDirReader;

/**
* @covers \Magento\Framework\Config\GenericSchemaLocator
*/
class GenericSchemaLocatorTest extends \PHPUnit_Framework_TestCase
{
/**
* @var string
*/
private $testSchemaFileName = 'test-example.xsd';

/**
* @var GenericSchemaLocator
*/
private $schemaLocator;

/**
* @var ModuleDirReader|\PHPUnit_Framework_MockObject_MockObject
*/
private $moduleReaderMock;

private function createNewSchemaLocatorInstance(ModuleDirReader $reader, $moduleName, $mergeSchema, $perFileSchema)
{
return new GenericSchemaLocator($reader, $moduleName, $mergeSchema, $perFileSchema);
}

protected function setUp()
{
$this->moduleReaderMock = $this->getMock(ModuleDirReader::class, [], [], '', false);
$this->schemaLocator = $this->createNewSchemaLocatorInstance(
$this->moduleReaderMock,
'Test_ModuleName',
$this->testSchemaFileName,
null
);
}

public function testItIsAnInstanceOfSchemaLocatorInterface()
{
$this->assertInstanceOf(SchemaLocatorInterface::class, $this->schemaLocator);
}

public function testItReturnsThePathToTheSpecifiedModuleXsd()
{
$this->moduleReaderMock->expects($this->any())->method('getModuleDir')->willReturn('....');
$this->assertSame('..../' . $this->testSchemaFileName, $this->schemaLocator->getSchema());
}

public function testItReturnsNullAsTheDefaultPerFileSchema()
{
$this->assertNull($this->schemaLocator->getPerFileSchema());
}

public function testItReturnsThePathToThePerFileSchema()
{
$this->moduleReaderMock->expects($this->any())->method('getModuleDir')->willReturn('....');
$schemaLocator = $this->createNewSchemaLocatorInstance(
$this->moduleReaderMock,
'Test_ModuleName',
'some other file name',
$this->testSchemaFileName
);
$this->assertSame('..../' . $this->testSchemaFileName, $schemaLocator->getPerFileSchema());
}
}

0 comments on commit dd4e82a

Please sign in to comment.