-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
212 additions
and
2 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
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
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 declare(strict_types=1); | ||
/* | ||
* This file is part of sebastian/exporter. | ||
* | ||
* (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace SebastianBergmann\Exporter; | ||
|
||
interface ObjectExporter | ||
{ | ||
public function handles(object $object): bool; | ||
|
||
public function export(object $object, Exporter $exporter, int $indentation): string; | ||
} |
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,51 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of sebastian/exporter. | ||
* | ||
* (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace SebastianBergmann\Exporter; | ||
|
||
final class ObjectExporterChain implements ObjectExporter | ||
{ | ||
/** | ||
* @psalm-var non-empty-list<ObjectExporter> | ||
*/ | ||
private array $exporter; | ||
|
||
/** | ||
* @psalm-param non-empty-list<ObjectExporter> $exporter | ||
*/ | ||
public function __construct(array $exporter) | ||
{ | ||
$this->exporter = $exporter; | ||
} | ||
|
||
public function handles(object $object): bool | ||
{ | ||
foreach ($this->exporter as $exporter) { | ||
if ($exporter->handles($object)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @throws ObjectNotSupportedException | ||
*/ | ||
public function export(object $object, Exporter $exporter, int $indentation): string | ||
{ | ||
foreach ($this->exporter as $objectExporter) { | ||
if ($objectExporter->handles($object)) { | ||
return $objectExporter->export($object, $exporter, $indentation); | ||
} | ||
} | ||
|
||
throw new ObjectNotSupportedException; | ||
} | ||
} |
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,16 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of sebastian/exporter. | ||
* | ||
* (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace SebastianBergmann\Exporter; | ||
|
||
use Throwable; | ||
|
||
interface Exception extends Throwable | ||
{ | ||
} |
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,16 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of sebastian/exporter. | ||
* | ||
* (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace SebastianBergmann\Exporter; | ||
|
||
use RuntimeException; | ||
|
||
final class ObjectNotSupportedException extends RuntimeException implements Exception | ||
{ | ||
} |
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
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,64 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of sebastian/exporter. | ||
* | ||
* (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace SebastianBergmann\Exporter; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Small; | ||
use PHPUnit\Framework\Attributes\UsesClass; | ||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
|
||
#[CoversClass(ObjectExporterChain::class)] | ||
#[UsesClass(Exporter::class)] | ||
#[Small] | ||
final class ObjectExporterChainTest extends TestCase | ||
{ | ||
public function testCanBeQueriedWhetherChainedExporterHandlesAnObject(): void | ||
{ | ||
$firstExporter = $this->createStub(ObjectExporter::class); | ||
$firstExporter->method('handles')->willReturn(false); | ||
|
||
$secondExporter = $this->createStub(ObjectExporter::class); | ||
$secondExporter->method('handles')->willReturn(true); | ||
|
||
$chain = new ObjectExporterChain([$firstExporter]); | ||
$this->assertFalse($chain->handles(new stdClass)); | ||
|
||
$chain = new ObjectExporterChain([$firstExporter, $secondExporter]); | ||
$this->assertTrue($chain->handles(new stdClass)); | ||
} | ||
|
||
public function testDelegatesExportingToFirstExporterThatHandlesAnObject(): void | ||
{ | ||
$firstExporter = $this->createStub(ObjectExporter::class); | ||
$firstExporter->method('handles')->willReturn(false); | ||
$firstExporter->method('export')->willThrowException(new ObjectNotSupportedException); | ||
|
||
$secondExporter = $this->createStub(ObjectExporter::class); | ||
$secondExporter->method('handles')->willReturn(true); | ||
$secondExporter->method('export')->willReturn('string'); | ||
|
||
$chain = new ObjectExporterChain([$firstExporter, $secondExporter]); | ||
|
||
$this->assertSame('string', $chain->export(new stdClass, new Exporter, 0)); | ||
} | ||
|
||
public function testCannotExportObjectWhenNoExporterHandlesIt(): void | ||
{ | ||
$firstExporter = $this->createStub(ObjectExporter::class); | ||
$firstExporter->method('handles')->willReturn(false); | ||
|
||
$chain = new ObjectExporterChain([$firstExporter]); | ||
|
||
$this->expectException(ObjectNotSupportedException::class); | ||
|
||
$this->assertSame('string', $chain->export(new stdClass, new Exporter, 0)); | ||
} | ||
} |