Skip to content

Commit

Permalink
Adding support for placeholder replacement in the LicenseHeaderFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshalsall committed Jun 14, 2015
1 parent 896af15 commit 1323514
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/JamesHalsall/Licenser/Factory/LicenseHeaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ public function __construct(\Twig_Environment $twig)
/**
* Creates a license header string from a license name
*
* @param string $licenseName The license name
* @param string $licenseName The license name
* @param array $replacements Values to use replacing placeholders in the license header (indexed by their placeholder name)
*
* @throws \InvalidArgumentException If the license name doesn't exist
*
* @return string
*/
public function createFromLicenseName($licenseName)
public function createFromLicenseName($licenseName, array $replacements = array())
{
$replacements['thisYear'] = date('Y');

try {
return $this->twig->render($licenseName);
return $this->twig->render($licenseName, $replacements);
} catch (\Twig_Error_Loader $e) {
throw new \InvalidArgumentException('Invalid license name provided');
}
Expand Down
13 changes: 10 additions & 3 deletions tests/Licenser/Factory/LicenseHeaderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ class LicenseHeaderFactoryTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider getLicenseNameFixtures
*/
public function testCreateFromLicenseName($licenseName, $expectedThatLicenseExists = true)
public function testCreateFromLicenseName($licenseName, array $replacements = array(), $expectedThatLicenseExists = true)
{
if (false === $expectedThatLicenseExists) {
$this->setExpectedException('\InvalidArgumentException');
$this->getFactory()->createFromLicenseName($licenseName);
} else {
$licenseHeader = $this->getFactory()->createFromLicenseName($licenseName);
$licenseHeader = $this->getFactory()->createFromLicenseName($licenseName, $replacements);
$this->assertNotEmpty($licenseHeader);
$this->assertContains(date('Y'), $licenseHeader);

foreach ($replacements as $replacement) {
$this->assertContains($replacement, $licenseHeader);
}
}
}

Expand All @@ -31,7 +36,9 @@ public function getLicenseNameFixtures()
return array(
array('mit'),
array('apache-2.0'),
array('gplv3', false)
array('gplv3', array(), false),
array('mit', array('owners' => 'james.t.halsall@googlemail.com')),
array('mit', array('owners' => 'james.t.halsall@googlemail.com, james.t.halsall@gmail.com'))
);
}

Expand Down

0 comments on commit 1323514

Please sign in to comment.