Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom config files with virtual types only by adding generic schema locator #1410

Merged
merged 1 commit into from
Jul 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}