Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Aurolieen/Path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Aurolieen;

/**
* Assessment 4. Path
*
* @note I have omitted invalid input checks as per the instructions that the input will always be valid.
*
* Class Path
* @package Aurolieen
*/
class Path
{
public $currentPath;
private $nodes = [];

/**
* Path constructor.
* Traverses the path pointer to the provided base path.
*
* @param string $path The base path.
*/
function __construct($path)
{
$this->cd($path);
}

/**
* Moves the path pointer to the specified new location and updates the string representation of the current path.
*
* @param string $newPath New path to traverse to.
*/
public function cd($newPath)
{
$newNodes = explode('/', $newPath);
foreach ($newNodes as $index => $node) {
switch ($node)
{
case '': // If the index equals 0, reset to the root otherwise do nothing.
if ($index === 0) $this->nodes = [];
break;
case '..': // Move up one node.
array_pop($this->nodes);
break;
default: // Add a new node.
array_push($this->nodes, $node);
break;
}
}
$this->currentPath = '/' . implode('/', $this->nodes);
}
}

$path = new Path('/a/b/c/d');
$path->cd('../x');
echo $path->currentPath;
75 changes: 75 additions & 0 deletions Aurolieen/tests/PathTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Aurolieen\Tests;

use Aurolieen\Path;
use PHPUnit\Framework\TestCase;

/**
* Test suite for the Path assessment class.
*
* Class PathTest
* @package Aurolieen\Tests
*/
class PathTest extends TestCase
{
/**
* Tests that the constructor sets the currentPath attribute to the same string as the input path.
*/
public function testBasePath()
{
$base = new Path('/a/b/c/d');
$this->assertEquals('/a/b/c/d', $base->currentPath);
}

/**
* Tests that absolute paths get appended to the current path.
*
* @depends testBasePath
*/
public function testAbsolutePaths()
{
$base = new Path('/');
$base->cd('a/b/c/d');
$this->assertEquals('/a/b/c/d', $base->currentPath);
$base = new Path('/a/b/c/d');
$base->cd('x');
$this->assertEquals('/a/b/c/d/x', $base->currentPath);
}

/**
* Tests that relative paths traverse up the path when needed.
*
* @depends testBasePath
*/
public function testRelativePaths()
{
$base = new Path('/');
$base->cd('a/b/../d');
$this->assertEquals('/a/d', $base->currentPath);
$base = new Path('/a/b/c/d');
$base->cd('../x');
$this->assertEquals('/a/b/c/x', $base->currentPath);
$base = new Path('/a/b/c/d');
$base->cd('../../../../x');
$this->assertEquals('/x', $base->currentPath);
}

/**
* Tests that some of the weirder valid paths produce the correct results.
*
* @depends testBasePath
*/
public function testOddPaths()
{
$base = new Path('/');
$base->cd('///////////////////a');
$this->assertEquals('/a', $base->currentPath);
$base = new Path('/');
$base->cd('../x/../x/../a');
$this->assertEquals('/a', $base->currentPath);
$base = new Path('/');
$base->cd('a/..//..//a//b//cde');
$this->assertEquals('/a/b/cde', $base->currentPath);
}
}