Skip to content

Commit

Permalink
Added test for basename task. (#994)
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 authored Nov 18, 2018
1 parent 39ff5a8 commit 4732b93
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 18 deletions.
33 changes: 15 additions & 18 deletions classes/phing/tasks/system/Basename.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,9 @@ class Basename extends Task
* file or directory to get base name from
* @param PhingFile $file file or directory to get base name from
*/
public function setFile($file)
public function setFile(PhingFile $file)
{
if (is_string($file)) {
$this->file = new PhingFile($file);
} else {
$this->file = $file;
}
$this->file = $file;
}

/**
Expand Down Expand Up @@ -76,21 +72,22 @@ public function main()
throw new BuildException("property attribute required", $this->getLocation());
}

if ($this->file == null) {
if ($this->file === null) {
throw new BuildException("file attribute required", $this->getLocation());
}

$value = $this->file->getName();
if ($this->suffix != null && StringHelper::endsWith($this->suffix, $value)) {
// if the suffix does not starts with a '.' and the
// char preceding the suffix is a '.', we assume the user
// wants to remove the '.' as well
$pos = strlen($value) - strlen($this->suffix) - 1;
if ($pos > 0 && $this->suffix{0} !== '.' && $value{$pos} === '.') {
$pos--;
}
$value = StringHelper::substring($value, 0, $pos);
$this->getProject()->setNewProperty(
$this->property,
$this->removeExtension($this->file->getName(), $this->suffix)
);
}

private function removeExtension(?string $s, ?string $ext)
{
if ($ext === null || !StringHelper::endsWith($ext, $s)) {
return $s;
}
$this->getProject()->setNewProperty($this->property, $value);

return rtrim(substr($s, 0, - strlen($ext)), '.');
}
}
92 changes: 92 additions & 0 deletions test/classes/phing/tasks/system/BasenameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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>.
*/

/**
* Tests the Diagnostics Task
*
* @author Siad Ardroumli <siad.ardroumli@gmail.com>
* @package phing.tasks.system
*/
class BasenameTest extends BuildFileTest
{
public function setUp()
{
$this->configureProject(
PHING_TEST_BASE . '/etc/tasks/system/BasenameTest.xml'
);
}

public function test1()
{
$this->expectBuildException("test1", '');
}


public function test2()
{
$this->expectBuildException("test2", '');
}

public function test3()
{
$this->expectBuildException("test3", '');
}

public function test4()
{
$this->executeTarget("test4");
$checkprop = $this->getProject()->getProperty("file.w.suf");
$this->assertEquals("foo.txt", $checkprop);
}

public function test5()
{
$this->executeTarget("test5");
$checkprop = $this->getProject()->getProperty("file.wo.suf");
$this->assertEquals("foo", $checkprop);
}

public function testMultipleDots()
{
$this->executeTarget("testMultipleDots");
$checkprop = $this->getProject()->getProperty("file.wo.suf");
$this->assertEquals("foo.bar", $checkprop);
}

public function testNoDots()
{
$this->executeTarget("testNoDots");
$checkprop = $this->getProject()->getProperty("file.wo.suf");
$this->assertEquals("foo.bar", $checkprop);
}

public function testValueEqualsSuffixWithDot()
{
$this->executeTarget("testValueEqualsSuffixWithDot");
$checkprop = $this->getProject()->getProperty("file.wo.suf");
$this->assertEquals("", $checkprop);
}

public function testValueEqualsSuffixWithoutDot()
{
$this->executeTarget("testValueEqualsSuffixWithoutDot");
$checkprop = $this->getProject()->getProperty("file.wo.suf");
$this->assertEquals("", $checkprop);
}
}
58 changes: 58 additions & 0 deletions test/etc/tasks/system/BasenameTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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>.
-->

<project name="BasenameTest" default="test1">

<target name="test1">
<basename/>
</target>

<target name="test2">
<basename property="propname"/>
</target>

<target name="test3">
<basename file="filename"/>
</target>

<target name="test4">
<basename property="file.w.suf" file="${user.dir}/foo.txt"/>
</target>

<target name="test5">
<basename property="file.wo.suf" file="foo.txt" suffix="txt"/>
</target>

<target name="testMultipleDots">
<basename property="file.wo.suf" file="foo.bar.txt" suffix="txt"/>
</target>

<target name="testNoDots">
<basename property="file.wo.suf" file="foo.bartxt" suffix="txt"/>
</target>

<target name="testValueEqualsSuffixWithDot">
<basename property="file.wo.suf" file=".txt" suffix=".txt"/>
</target>

<target name="testValueEqualsSuffixWithoutDot">
<basename property="file.wo.suf" file=".txt" suffix="txt"/>
</target>

</project>

0 comments on commit 4732b93

Please sign in to comment.