This repository has been archived by the owner on Sep 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add AbstractObjectList to make it easier to implement lists for objec…
…t-types
- Loading branch information
Showing
3 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |