Skip to content

Commit

Permalink
TDD Libraries\Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
R11T committed Dec 30, 2014
1 parent a39be6d commit 6ab304b
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 15 deletions.
108 changes: 100 additions & 8 deletions App/Libraries/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,135 @@
*/
namespace App\Libraries;

use \App\Libraries\Interfaces;

/**
*
*
* Collection of displayable item
*
* @since 0.2
* @author Romain L.
* @see \Tests\Units\App\Libraries\Collection
*/
class Collection implements \Iterator
class Collection implements \Iterator, Interfaces\IStackable, Interfaces\IMeasurable
{
public function __construct(\Displayable $item)
/**
* Collection of items
*
* @var array
*
* @access private
*/
private $items = [];

/**
* Offset of the current element
*
* @var int
*
* @access private
*/
private $position;

/**
* Construct a new Collection, adding a new item
*
* @param \ISoftwareItemDisplayable $item
*
* @access public
*/
public function __construct(Interfaces\IDisplayable $item)
{
$this->push($item);
$this->position = 0;
}

/**
* Add an element on the top of the stack
*
* @param Interfaces\IDisplayable $item
*
* @return void
* @access public
*/
public function push(Interfaces\IDisplayable $item)
{
$this->items[] = $item;
}

/**
* Get current element
*
* @return string
* @access public
*/
public function current()
{
return ($this->array[$this->position])->display();
return $this->items[$this->position];
}

/**
* Get current key
*
* @return int
* @access public
*/
public function key()
{
return $this->position;
}

function next()
/**
* Increments position
*
* @return void
* @access public
*/
public function next()
{
++$this->position;
}

/**
* Rewinds position
*
* @return void
* @access public
*/
public function rewind()
{
$this->position = 0;
}

/**
* Checks if an item exists at the current position
*
* @return bool
* @access public
*/
public function valid()
{
return isset($this->items[$this->position]);
}

public function push()
/**
* Extract lastest added item and returns it
*
* @return Interfaces\IDisplayable|null if $items is empty
* @access public
*/
public function pop()
{
return array_pop($this->items);
}

public function pop()
/**
* Returns number of item in items
*
* @return int
* @access public
*/
public function length()
{
return count($this->items);
}
}
4 changes: 2 additions & 2 deletions App/Libraries/Interfaces/IDisplayable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
interface IDisplayable
{
/**
* Display element's content
* Displays element's content
*
* @return mixed
* @return string
* @access public
*/
public function display();
Expand Down
23 changes: 23 additions & 0 deletions App/Libraries/Interfaces/IMeasurable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* @licence GPL-v2
*/
namespace App\Libraries\Interfaces;

/**
* Define an element as measurable
*
* @since 0.2
* @author Romain L.
*/
interface IMeasurable
{
/**
* Return element's length
*
* @return int
* @access public
*/
public function length();
}

8 changes: 4 additions & 4 deletions App/Libraries/Io/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
class Response
{
/**
* Print data
* Displays data
*
* @param \App\Libraries\Interfaces\ISoftwareItem
* @param \Iterator
*
* @return void
* @access public
*/
public function display(\Iterator $items)
{
foreach ($items as $item) {
echo $item->current() . "\n";
foreach ($items as $key => $item) {
echo $item->display() . "\n";
}
/**
* Collectable $items
Expand Down
121 changes: 121 additions & 0 deletions Tests/Units/App/Libraries/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/**
* @licence GPL-v2
*/
namespace Tests\Units\App\Libraries;

use \Tests\Units\TestCase;
use \App\Libraries\Collection as _Collection;

/**
* Unit testing on collection of displayable item
*
* @since 0.2
* @author Romain L.
* @see \App\Libraries\Collection
*/
class Collection extends TestCase
{
/**
* Tests constructing a collection
*
* @since 0.2
* @author Romain L.
* @see \App\Libraries\Collection
*/
public function test__construct()
{
$this->mockGenerator->orphanize('__construct');
$item = new \mock\App\Item\Browser;
$item->getMockController()->display = 'Rose Tyler';

$collection = new _Collection($item);

$this->integer($collection->key())->isIdenticalTo(0);
$this->string($collection->current()->display())->isIdenticalTo('Rose Tyler');
}

/**
* Tests incrementing position
*
* @return void
* @access public
*/
public function testNext()
{
$this->mockGenerator->orphanize('__construct');
$item1 = new \mock\App\Item\Browser;
$item1->getMockController()->display = 'Rose Tyler';
$collection = new _Collection($item1);
$item2 = new \mock\App\Item\Browser;
$item2->getMockController()->display = 'Martha Jones';
$collection->push($item2);

$collection->next();

$this->string($collection->current()->display())->isIdenticalTo('Martha Jones');
}

/**
* Test rewinding
*
* @return void
* @access public
*/
public function testRewind()
{
$this->mockGenerator->orphanize('__construct');
$item1 = new \mock\App\Item\Browser;
$item1->getMockController()->display = 'Rose Tyler';
$collection = new _Collection($item1);
$item2 = new \mock\App\Item\Browser;
$item2->getMockController()->display = 'Martha Jones';
$collection->push($item2);
$collection->next();

$collection->rewind();

$this->string($collection->current()->display())->isIdenticalTo('Rose Tyler');
}

/**
* Tests checking item existence
*
* @return void
* @access public
*/
public function testValid()
{
$this->mockGenerator->orphanize('__construct');
$item = new \mock\App\Item\Browser;
$item->getMockController()->display = 'Rose Tyler';
$collection = new _Collection($item);
$collection->next();

$valid = $collection->valid();

$this->boolean($valid)->isFalse();
}

/**
* Tests poping collection
*
* @return void
* @access public
*/
public function testPop()
{
$this->mockGenerator->orphanize('__construct');
$item1 = new \mock\App\Item\Browser;
$item1->getMockController()->display = 'Rose Tyler';
$collection = new _Collection($item1);
$item2 = new \mock\App\Item\Browser;
$item2->getMockController()->display = 'Martha Jones';
$collection->push($item2);

$itemX = $collection->pop();

$this->object($itemX)->isInstanceOf('\App\Item\Browser');
$this->integer($collection->length())->isIdenticalTo(1);
}
}
2 changes: 1 addition & 1 deletion latest-softwares
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ try {

try {
$app = (new \App\Main)->run();
\App\Singleton::response()->display();
\App\Singleton::response()->display($app);
} catch (\Exception $e) {
if (!is_object(\App\Singleton::response())) {
\App\Singleton::response((new \App\Libraries\Io\Response()));
Expand Down

0 comments on commit 6ab304b

Please sign in to comment.