Commit d13e6797 authored by Daniel Hürtgen's avatar Daniel Hürtgen

Merge branch 'feature/naming' into 'develop'

Feature: Naming

See merge request !40
parents 0c01a280 209dbcf2
Pipeline #341 passed with stage
in 5 minutes and 26 seconds
......@@ -57,8 +57,8 @@ class Standard implements StandardInterface
);
}
$this->path = realpath($path);
$this->name = basename($this->path);
$this->ruleSetXmlPath = $path . DIRECTORY_SEPARATOR . static::RULESET_FILENAME;
$this->name = $this->getNameFromRuleSet($this->ruleSetXmlPath);
if (!is_readable($this->ruleSetXmlPath)) {
throw new InvalidStandardException(
......@@ -67,6 +67,28 @@ class Standard implements StandardInterface
}
}
/**
* Fetch PHPCodeSniffer standard name from ruleset.xml.
*
* @param string $ruleSetXmlPath The absolute path to ruleset.xml.
* @return string The name of the PHPCodeSniffer standard.
*/
protected function getNameFromRuleSet($ruleSetXmlPath)
{
try {
$ruleSet = new \SimpleXMLElement(file_get_contents($ruleSetXmlPath));
$attributes = $ruleSet->attributes();
$name = trim($attributes['name']);
if ($name !== '') {
return $name;
}
} catch (\Exception $e) {
// Nothing todo, use folder name.
}
return basename(dirname($ruleSetXmlPath));
}
/**
* Get the name of the PHPCodeSniffer standard.
*
......
<?xml version="1.0"?>
<ruleset name="CustomStandard">
</ruleset>
<?xml version="1.0"?>
<ruleset name="">
</ruleset>
......@@ -64,6 +64,8 @@ class FinderTest extends \PHPUnit_Framework_TestCase
$path . DIRECTORY_SEPARATOR . 'Standard1',
$path . DIRECTORY_SEPARATOR . 'Standard2',
$path . DIRECTORY_SEPARATOR . 'Standard3',
$path . DIRECTORY_SEPARATOR . 'standard_with_ruleset',
$path . DIRECTORY_SEPARATOR . 'standard_with_ruleset_without_name',
);
$this->standardsFactoryMock
......
......@@ -28,18 +28,24 @@ use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\Standa
*/
class StandardTest extends \PHPUnit_Framework_TestCase
{
public function testStandard()
/**
* @dataProvider differentStandards
*/
public function testStandard($standardName, $expectedName)
{
$name = 'Standard1';
$path = implode(
DIRECTORY_SEPARATOR,
array(__DIR__, '..', '..', '..', '..', '..', 'Fixtures', 'Standards', $name)
array(__DIR__, '..', '..', '..', '..', '..', 'Fixtures', 'Standards', $standardName)
);
$ruleSetXmlPath = $path . DIRECTORY_SEPARATOR . 'ruleset.xml';
$standard = new Standard($path);
$this->assertSame($name, $standard->getName(), 'The standard instance did not return the expected name.');
$this->assertSame(
$expectedName,
$standard->getName(),
'The standard instance did not return the expected name.'
);
$this->assertSame(
realpath($path),
$standard->getPath(),
......@@ -52,6 +58,24 @@ class StandardTest extends \PHPUnit_Framework_TestCase
);
}
public function differentStandards()
{
return array(
'No Ruleset' => array(
'Standard1',
'Standard1',
),
'Ruleset with name' => array(
'standard_with_ruleset',
'CustomStandard',
),
'Ruleset without name' => array(
'standard_with_ruleset_without_name',
'standard_with_ruleset_without_name',
),
);
}
/**
* @expectedException \Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\Exception\StandardPathAccessDeniedException
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment