class Components_Helper_Templates
{
/**
+ * The source location.
+ *
+ * @var string
+ */
+ private $_source;
+
+ /**
* The target location.
*
* @var string
*/
public function __construct($source, $target)
{
+ if (file_exists($source . '.template')) {
+ $this->_source = $source . '.template';
+ } else {
+ throw new Components_Exception("No template at $source!");
+ }
$this->_target = $target;
}
*/
public function write(array $parameters = array())
{
- file_put_contents($this->_target, '');
+ $source = file_get_contents($this->_source);
+ file_put_contents($this->_target, vsprintf($source, $parameters));
}
}
\ No newline at end of file
{
public function testWrite()
{
+ $source = dirname(__FILE__) . '/../../../fixture/templates/simple';
$target = $this->getTemporaryDirectory() . '/target';
- $templates = new Components_Helper_Templates('', $target, array());
+ $templates = new Components_Helper_Templates($source, $target);
$templates->write();
$this->assertTrue(file_exists($target));
}
+
+ public function testSource()
+ {
+ $source = dirname(__FILE__) . '/../../../fixture/templates/simple';
+ $target = $this->getTemporaryDirectory() . '/target';
+ $templates = new Components_Helper_Templates($source, $target);
+ $templates->write();
+ $this->assertEquals("SIMPLE\n", file_get_contents($target));
+ }
+
+ /**
+ * @expectedException Components_Exception
+ */
+ public function testMissingSource()
+ {
+ $source = dirname(__FILE__) . '/NO_SUCH_TEMPLATE';
+ $templates = new Components_Helper_Templates($source, '');
+ }
+
+ public function testVariables()
+ {
+ $source = dirname(__FILE__) . '/../../../fixture/templates/variables';
+ $target = $this->getTemporaryDirectory() . '/target';
+ $templates = new Components_Helper_Templates($source, $target);
+ $templates->write(array('1' => 'One', '2' => 'Two'));
+ $this->assertEquals("One : Two\n", file_get_contents($target));
+ }
}
\ No newline at end of file