Skip to content

Commit

Permalink
Bump Smarty and add simple happy path test
Browse files Browse the repository at this point in the history
  • Loading branch information
mrook committed Apr 18, 2024
1 parent 8551081 commit fcf978a
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 52 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"roave/security-advisories": "dev-master",
"scssphp/scssphp": "~1.12.0",
"siad007/versioncontrol_hg": "^1.0",
"smarty/smarty": "^3.1",
"smarty/smarty": "^5.0",
"squizlabs/php_codesniffer": "^3.5",
"symfony/config": "^5.2|^6.0",
"symfony/dependency-injection": "^5.2|^6.0",
Expand Down
41 changes: 24 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 18 additions & 34 deletions src/Phing/Task/Ext/Smarty/SmartyTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Phing\Task;
use Phing\Util\Properties;
use Phing\Util\StringHelper;
use Smarty\Smarty;

/**
* A phing task for generating output by using Smarty.
Expand Down Expand Up @@ -127,7 +128,7 @@ class SmartyTask extends Task
* So initial context values can be set with
* properties file.
*
* @var array
* @var Properties
*/
protected $contextProperties;

Expand Down Expand Up @@ -178,14 +179,7 @@ class SmartyTask extends Task

public function init()
{
// This check returns true for smarty 3 and false otherwise.
if (stream_resolve_include_path('SmartyBC.class.php')) {
include_once 'SmartyBC.class.php';
} else {
include_once 'Smarty.class.php';
}

if (!class_exists('Smarty')) {
if (!class_exists('Smarty\\Smarty')) {
throw new BuildException("To use SmartyTask, you must have the path to Smarty.class.php on your include_path or your \$PHP_CLASSPATH environment variable.");
}
}
Expand Down Expand Up @@ -469,7 +463,7 @@ public function setContextProperties($file)
* fed into the initial context be the
* generating process starts.
*
* @return \Properties
* @return Properties
*/
public function getContextProperties()
{
Expand All @@ -483,13 +477,13 @@ public function getContextProperties()
/**
* Creates a Smarty object.
*
* @return \Smarty initialized (cleared) Smarty context.
* @return Smarty initialized (cleared) Smarty context.
* @throws \Exception the execute method will catch
* and rethrow as a <code>BuildException</code>
*/
public function initControlContext()
{
$this->context->clear_all_assign();
$this->context->clearAllAssign();

return $this->context;
}
Expand Down Expand Up @@ -526,59 +520,49 @@ public function main()

// Setup Smarty runtime.

// Smarty uses one object to store properties and to store
// the context for the template (unlike Smarty). We setup this object, calling it
// $this->context, and then initControlContext simply zeros out
// any assigned variables.
//
// Use the smarty backwards compatibility layer if existent.
if (class_exists('SmartyBC')) {
$this->context = new \SmartyBC();
} else {
$this->context = new \Smarty();
}
$this->context = new Smarty();

if ($this->compilePath !== null) {
$this->log("Using compilePath: " . $this->compilePath);
$this->context->compile_dir = $this->compilePath;
$this->context->setCompileDir($this->compilePath);
}

if ($this->configPath !== null) {
$this->log("Using configPath: " . $this->configPath);
$this->context->config_dir = $this->configPath;
$this->context->setConfigDir($this->configPath);
}

if ($this->forceCompile !== null) {
$this->context->force_compile = $this->forceCompile;
$this->context->setForceCompile($this->forceCompile);
}

if ($this->leftDelimiter !== null) {
$this->context->left_delimiter = $this->leftDelimiter;
$this->context->setLeftDelimiter($this->leftDelimiter);
}

if ($this->rightDelimiter !== null) {
$this->context->right_delimiter = $this->rightDelimiter;
$this->context->setRightDelimiter($this->rightDelimiter);
}

if ($this->templatePath !== null) {
$this->log("Using templatePath: " . $this->templatePath);
$this->context->template_dir = $this->templatePath;
$this->context->setTemplateDir($this->templatePath);
}

$smartyCompilePath = new IOException($this->context->compile_dir);
$smartyCompilePath = new File($this->context->getCompileDir());
if (!$smartyCompilePath->exists()) {
$this->log(
"Compile directory does not exist, creating: " . $smartyCompilePath->getPath(),
Project::MSG_VERBOSE
);
if (!$smartyCompilePath->mkdirs()) {
throw new BuildException("Smarty needs a place to compile templates; specify a 'compilePath' or create " . $this->context->compile_dir);
throw new BuildException("Smarty needs a place to compile templates; specify a 'compilePath' or create " . $this->context->getCompileDir());
}
}

// Make sure the output directory exists, if it doesn't
// then create it.
$file = new IOException($this->outputDirectory);
$file = new File($this->outputDirectory);
if (!$file->exists()) {
$this->log("Output directory does not exist, creating: " . $file->getAbsolutePath());
$file->mkdirs();
Expand Down Expand Up @@ -658,11 +642,11 @@ public function main()
* <p><code>$generator</code> is not put into the context in this
* method.</p>
*
* @param \Smarty $context context to populate, as retrieved from
* @param Smarty $context context to populate, as retrieved from
* {@link #initControlContext()}.
* @return void
*/
protected function populateInitialContext(\Smarty $context)
protected function populateInitialContext(Smarty $context)
{
}

Expand Down
44 changes: 44 additions & 0 deletions tests/Phing/Test/Task/Ext/Smarty/SmartyTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

namespace Phing\Test\Task\Ext\Smarty;

use Phing\Test\Support\BuildFileTest;

class SmartyTaskTest extends BuildFileTest
{
protected function setUp(): void
{
$buildXmlFile = PHING_TEST_BASE . '/etc/tasks/ext/smarty/SmartyTaskTest.xml';
$this->configureProject($buildXmlFile);
$this->executeTarget('setup');
}

public function tearDown(): void
{
$this->executeTarget('clean');
}

public function testRenderSimpleTemplate(): void
{
$this->executeTarget(__FUNCTION__);
$this->assertStringEqualsFile(PHING_TEST_BASE . "/etc/tasks/ext/smarty/tmp/test.txt", "Foo\n");
}
}
23 changes: 23 additions & 0 deletions tests/etc/tasks/ext/smarty/SmartyTaskTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="smarty-test" default="invalid">
<target name="invalid">
<fail>This file should only be run via a testcase.</fail>
</target>

<property name="tmp.dir" value="tmp"/>
<resolvepath propertyName="tmp.dir.resolved" file="${tmp.dir}"/>

<target name="setup">
<mkdir dir="${tmp.dir}"/>
</target>

<target name="clean">
<delete dir="${tmp.dir}"/>
</target>

<target name="testRenderSimpleTemplate">
<smarty controlTemplate="test.tpl" compilePath="${tmp.dir.resolved}" outputDirectory="${tmp.dir.resolved}" outputFile="test.txt" templatePath=".">
</smarty>
</target>

</project>
2 changes: 2 additions & 0 deletions tests/etc/tasks/ext/smarty/test.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{* Smarty *}
Foo

0 comments on commit fcf978a

Please sign in to comment.