Skip to content
Permalink
Browse files

wip

  • Loading branch information
rodolfoberrios committed Jan 14, 2020
1 parent dad5472 commit ad8fb7236b8b8370072bdb4e20ce0f68c819d5e9
@@ -13,8 +13,8 @@

namespace Chevere\Components\Number\Tests;

use Chevere\Components\Number\Number;
use InvalidArgumentException;
use Chevere\Components\Number\Number;
use PHPUnit\Framework\TestCase;
use stdClass;

@@ -95,7 +95,9 @@ public function testConstructBooleanArgument(): void
public function testConstructResourceArgument(): void
{
$this->expectException(InvalidArgumentException::class);
new Number(fopen(__FILE__, 'r'));
$resource = fopen(__FILE__, 'r');
new Number($resource);
fclose($resource);
}

public function testConstruct(): void
@@ -16,39 +16,31 @@
use Chevere\Components\Type\Type;
use Chevere\Components\Type\Interfaces\TypeInterface;
use PHPUnit\Framework\TestCase;
use stdClass;

final class TypeTest extends TestCase
{
public function testTypes(): void
{
$resource = fopen(__FILE__, 'r');
foreach ([
TypeInterface::BOOLEAN => true,
TypeInterface::INTEGER => 1,
TypeInterface::FLOAT => 13.13,
TypeInterface::STRING => 'test',
TypeInterface::ARRAY => ['test'],
TypeInterface::OBJECT => new self(),
TypeInterface::OBJECT => new stdClass,
TypeInterface::CALLABLE => 'phpinfo',
TypeInterface::ITERABLE => [4, 2, 1, 3],
TypeInterface::NULL => null,
TypeInterface::RESOURCE => $resource,
] as $key => $val) {
$type = new Type($key);
$this->assertSame($key, $type->primitive());
$this->assertSame($key, $type->typeHinting());
$this->assertTrue($type->validate($val));
}
}

public function testResource(): void
{
$type = new Type(TypeInterface::RESOURCE);
$resource = fopen(__FILE__, 'r');
$this->assertSame(TypeInterface::RESOURCE, $type->primitive());
$this->assertSame(TypeInterface::RESOURCE, $type->typeHinting());
$this->assertTrue($type->validate($resource));
if (false !== $resource) {
fclose($resource);
}
fclose($resource);
}

public function testClassName(): void
@@ -81,11 +81,6 @@ public function dontDump(): array;
*/
public function var();

/**
* Provides access to the instance var type.
*/
public function type(): string;

/**
* Return an instance with the specified $indent.
*
@@ -14,10 +14,13 @@
namespace Chevere\Components\VarDump\Processors;

use Chevere\Components\Message\Message;
use Chevere\Components\Type\Interfaces\TypeInterface;
use Chevere\Components\Type\Type;
use Chevere\Components\VarDump\Interfaces\ProcessorInterface;
use Chevere\Components\VarDump\Interfaces\VarDumpInterface;
use InvalidArgumentException;
use TypeError;
use function ChevereFn\varType;

abstract class AbstractProcessor implements ProcessorInterface
{
@@ -41,15 +44,18 @@ abstract class AbstractProcessor implements ProcessorInterface
*/
abstract protected function process(): void;

abstract public function type(): string;

final private function assertType(): void
{
if ($this->type() !== $this->varDump->type()) {
$type = new Type($this->type());
if (!$type->validate($this->varDump->var())) {
throw new InvalidArgumentException(
(new Message('Instance of %className% expects a type %expected% for the return value of %method%, type %provided% returned'))
->code('%className%', static::class)
->code('%expected%', $this->type())
->code('%method%', get_class($this->varDump) . '::var()')
->code('%provided%', $this->varDump->type())
->code('%provided%', varType($this->varDump->var()))
->toString()
);
}
@@ -13,11 +13,10 @@

namespace Chevere\Components\VarDump\Processors;

use Chevere\Components\Type\Interfaces\TypeInterface;
use ReflectionObject;
use ReflectionProperty;
use Throwable;
use Chevere\Components\VarDump\Interfaces\ProcessorInterface;
use Chevere\Components\Type\Interfaces\TypeInterface;
use Chevere\Components\VarDump\VarDump;
use Chevere\Components\VarDump\Interfaces\VarDumpInterface;
use function ChevereFn\stringStartsWith;
@@ -32,6 +32,7 @@ public function testConstruct(): void
{
$var = fopen(__FILE__, 'r');
$processor = new ResourceProcessor($this->getVarDump($var));
fclose($var);
$this->assertSame('type=stream', $processor->info());
$this->assertStringStartsWith('Resource id #', $processor->val());
}
@@ -96,11 +96,6 @@ public function var()
return $this->var;
}

public function type(): string
{
return $this->type;
}

/**
* {@inheritdoc}
*/
@@ -23,15 +23,19 @@ final class VariableExportTest extends TestCase
public function testCreateNotExportable(): void
{
$this->expectException(VariableExportException::class);
new VariableExport(fopen(__FILE__, 'r'));
$var = fopen(__FILE__, 'r');
new VariableExport($var);
fclose($var);
}

public function testCreateContainsNotExportable(): void
{
$object = new stdClass();
$object->array = [1, 2, 3, fopen(__FILE__, 'r')];
$resource = fopen(__FILE__, 'r');
$object->array = [1, 2, 3, $resource];
$this->expectException(VariableExportException::class);
new VariableExport($object);
fclose($resource);
}

public function testConstruct(): void

0 comments on commit ad8fb72

Please sign in to comment.
You can’t perform that action at this time.