Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Commit

Permalink
add AbstractObjectList to make it easier to implement lists for objec…
Browse files Browse the repository at this point in the history
…t-types
  • Loading branch information
Hansel23 committed Jun 15, 2016
1 parent 65097ac commit 9818c75
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/AbstractObjectList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
namespace Hansel23\GenericLists;

/**
* Class AbstractObjectList
*
* @package Hansel23\GenericLists
*/
abstract class AbstractObjectList extends AbstractList
{
/**
* @param $item
*
* @return bool
*/
protected function isItemValid( $item )
{
$validItemType = $this->getItemType();

return $item instanceof $validItemType;
}

/**
* @param array $objects
*
* @return static
*/
public static function fromArray( array $objects )
{
$list = new static();

foreach( $objects as $object )
{
$list->add( $object );
}

return $list;
}
}
80 changes: 80 additions & 0 deletions tests/unit/AbstractObjectListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
namespace Hansel23\GenericLists\Tests\Unit;

use Hansel23\GenericLists\StringList;
use Hansel23\GenericLists\Tests\Unit\Fixtures\Stringable;
use Hansel23\GenericLists\Tests\Unit\Fixtures\TestType;
use Hansel23\GenericLists\Tests\Unit\Fixtures\TestableList;

class AbstractObjectListTest extends \Codeception\TestCase\Test
{
public function InvalidItemProvider()
{
return [
[ 1, 1.0, true, false, new \stdClass(), new Stringable(), [ ], 'string' ],
];
}

/**
* @dataProvider InvalidItemProvider
*
* @expectedException \Hansel23\GenericLists\Exceptions\InvalidTypeException
*/
public function testIfInvalidItemsThrowsException( $item )
{
$stringList = new TestableList();
$stringList->add( $item );
}

public function ValidItemProvider()
{
return [
[ new TestType( 'true' ), new TestType( 1 ), new TestType( 1.0 ), new TestType( false ) ],
];
}

/**
* @dataProvider ValidItemProvider
*/
public function testAddingValidItems( $item )
{
$stringList = new TestableList();
$stringList->add( $item );

$this->assertNotEquals( -1, $stringList->indexOf( $item ) );
}

public function ValidStringArrayProvider()
{
return [
[ [ new TestType( 'true' ), new TestType( 1 ) ], [ new TestType( 1.0 ), new TestType( false ) ] ],
];
}

/**
* @dataProvider ValidStringArrayProvider
*/
public function testCreatingStringListFromArray( array $stringArray )
{
$stringList = TestableList::fromArray( $stringArray );

$this->assertEquals( count( $stringArray ), $stringList->count() );
$this->assertEquals( $stringArray, $stringList->toArray() );
}

public function InvalidArrayProvider()
{
return [
[ [ 1, false ], [ 'string', 'one more string', true ], [ [ ] ] ],
];
}

/**
* @dataProvider InvalidArrayProvider
* @expectedException \Hansel23\GenericLists\Exceptions\InvalidTypeException
*/
public function testIfCreatingFromInvalidArrayThrowsException( array $invalidArray )
{
TestableList::fromArray( $invalidArray );
}
}
17 changes: 17 additions & 0 deletions tests/unit/Fixtures/TestableList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace Hansel23\GenericLists\Tests\Unit\Fixtures;

use Hansel23\GenericLists\AbstractObjectList;

/**
* Class TestableList
*
* @package Hansel23\GenericLists\Tests\Unit\Fixtures
*/
final class TestableList extends AbstractObjectList
{
public function getItemType()
{
return Testable::class;
}
}

0 comments on commit 9818c75

Please sign in to comment.