Skip to content

Commit

Permalink
Update traits for iterarable interface
Browse files Browse the repository at this point in the history
DataContainerDecoratorTrait has been made iterable.
DataContainerIteratorAggregateTrait has been made deprecated. The traits
can no longer be used at the same time because they conflict with each
other.
  • Loading branch information
Ashoka de Wit committed Sep 3, 2018
1 parent 74e4342 commit 011ca51
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/DataContainerDecoratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Mediact\DataContainer;

use Traversable;

/**
* Implements DataContainerInterface
*/
Expand Down Expand Up @@ -194,6 +196,17 @@ public function move(string $pattern, string $replacement)
->move($pattern, $replacement);
}

/**
* Retrieve an external iterator.
*
* @return Traversable
*/
public function getIterator(): Traversable
{
return $this->getStorage()
->getIterator();
}

/**
* Prepare the object for cloning.
*
Expand Down
2 changes: 2 additions & 0 deletions src/DataContainerIteratorAggregateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

/**
* Implements IteratorAggregate
*
* @deprecated IteratorAggregate is implemented by DataContainerDecoratorTrait.
*/
trait DataContainerIteratorAggregateTrait
{
Expand Down
21 changes: 21 additions & 0 deletions tests/DataContainerDecoratorTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Mediact\DataContainer\Tests;

use ArrayIterator;
use Mediact\DataContainer\DataContainer;
use Mediact\DataContainer\DataContainerInterface;
use Mediact\DataContainer\Tests\TestDouble\DataContainerImplementationDouble;
Expand Down Expand Up @@ -262,6 +263,26 @@ public function testMove()
$double->move($pattern, $destination);
}

/**
* @return void
*
* @covers ::getIterator
*/
public function testGetIterator()
{
$data = ['some_data'];
$iterator = new ArrayIterator($data);
$storage = $this->createMock(DataContainerInterface::class);

$storage
->expects(self::once())
->method('getIterator')
->willReturn($iterator);

$double = new DataContainerImplementationDouble($storage);
$this->assertSame($data, iterator_to_array($double));
}

/**
* @return void
*
Expand Down
32 changes: 30 additions & 2 deletions tests/DataContainerIteratorAggregateTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

namespace Mediact\DataContainer\Tests;

use IteratorAggregate;
use Mediact\DataContainer\DataContainerInterface;
use Mediact\DataContainer\Tests\TestDouble\DataContainerImplementationDouble;
use Mediact\DataContainer\DataContainerIteratorAggregateTrait;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -30,7 +31,34 @@ public function testGetIterator()
->method('all')
->willReturn($data);

$double = new DataContainerImplementationDouble($storage);
$double = new class ($storage) implements IteratorAggregate
{
use DataContainerIteratorAggregateTrait;

/** @var DataContainerInterface */
private $storage;

/**
* Constructor.
*
* @param DataContainerInterface $storage
*/
public function __construct(DataContainerInterface $storage)
{
$this->storage = $storage;
}

/**
* Get the storage.
*
* @return DataContainerInterface
*/
protected function getStorage(): DataContainerInterface
{
return $this->storage;
}
};

$this->assertEquals($data, iterator_to_array($double));
}
}
3 changes: 1 addition & 2 deletions tests/TestDouble/DataContainerImplementationDouble.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
use Mediact\DataContainer\DataContainerInterface;
use Mediact\DataContainer\DataContainerIteratorAggregateTrait;

class DataContainerImplementationDouble implements DataContainerInterface, IteratorAggregate
class DataContainerImplementationDouble implements DataContainerInterface
{
use DataContainerDecoratorTrait;
use DataContainerIteratorAggregateTrait;

/**
* Constructor.
Expand Down
41 changes: 41 additions & 0 deletions tests/TestDouble/IterableImplementationDouble.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
*/

namespace Mediact\DataContainer\Tests\TestDouble;

use IteratorAggregate;
use Mediact\DataContainer\DataContainer;
use Mediact\DataContainer\DataContainerDecoratorTrait;
use Mediact\DataContainer\DataContainerInterface;
use Mediact\DataContainer\DataContainerIteratorAggregateTrait;

class IterableImplementationDouble implements IteratorAggregate
{
use DataContainerIteratorAggregateTrait;

/** @var DataContainerInterface */
private $storage;

/**
* Constructor.
*
* @param DataContainerInterface $storage
*/
public function __construct(DataContainerInterface $storage)
{
$this->storage = $storage;
}

/**
* Get the storage.
*
* @return DataContainerInterface
*/
protected function getStorage(): DataContainerInterface
{
return $this->storage;
}
}

0 comments on commit 011ca51

Please sign in to comment.