Skip to content

Commit

Permalink
chore: increase coverage to 100 (#6)
Browse files Browse the repository at this point in the history
* fixes self constant parameter default types
* allows magic methods overriding

Co-authored-by: Jorge Brisa <[email protected]>
  • Loading branch information
sangarbe and jorbriib committed Jan 19, 2021
1 parent f1b6b59 commit 5266eb7
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 54 deletions.
11 changes: 9 additions & 2 deletions lib/ProxyClassFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static function buildMethods(array $methods): string
{
$publicStringMethods = array();
foreach ($methods as $method) {
if (false !== strpos($method->getName(), '__')) {
if (false !== strpos($method->getName(), '__construct')) {
continue;
}

Expand Down Expand Up @@ -132,7 +132,14 @@ private static function buildParameterDefaultValue(ReflectionParameter $paramete

$defaultValue = '=';
if ($parameter->isDefaultValueConstant()) {
$defaultValue .= '\\'.$parameter->getDefaultValueConstantName();
$defaultValueConstantName = $parameter->getDefaultValueConstantName();
list($type) = explode('::', $defaultValueConstantName);

if (!\in_array($type, array('self', 'static'))) {
$defaultValue .= '\\';
}

$defaultValue .= $defaultValueConstantName;

return $defaultValue;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Dummy
{
const SOME_VALUE = 'dummy-value';

public function hello(): string
{
return 'hello';
Expand Down
40 changes: 40 additions & 0 deletions tests/InstanceSpy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Golossus\LazyProxyLoading\Tests;

class InstanceSpy
{
private $method;
private $input;

/**
* InstanceSpy constructor.
*/
public function __construct()
{
}

public function setVars($method, $input = null)
{
$this->method = $method;
$this->input = $input;
}

/**
* @return mixed
*/
public function getMethod()
{
return $this->method;
}

/**
* @return mixed
*/
public function getInput()
{
return $this->input;
}
}
81 changes: 49 additions & 32 deletions tests/ProxyClassFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,70 +12,79 @@ class ProxyClassFactoryTest extends TestCase
public function testCreate()
{
/** @var TestSpy $proxy */
$proxy = ProxyClassFactory::create(TestSpy::class, function () {
return new TestSpy();
$instanceSpy = new InstanceSpy();
$proxy = ProxyClassFactory::create(TestSpy::class, function () use ($instanceSpy) {
return new TestSpy($instanceSpy);
});

$this->assertInstanceOf(TestSpy::class, $proxy);

$proxy->noReturnType();
$this->assertEquals('noReturnType', $proxy::$method);
$this->assertNull($proxy::$input);
$this->assertEquals('noReturnType', $instanceSpy->getMethod());
$this->assertNull($instanceSpy->getInput());

$proxy->voidReturnType();
$this->assertEquals('voidReturnType', $proxy::$method);
$this->assertNull($proxy::$input);
$this->assertEquals('voidReturnType', $instanceSpy->getMethod());
$this->assertNull($instanceSpy->getInput());

$output = $proxy->nullableBuiltInReturnType();
$this->assertEquals('nullableBuiltInReturnType', $proxy::$method);
$this->assertNull($proxy::$input);
$this->assertEquals('nullableBuiltInReturnType', $instanceSpy->getMethod());
$this->assertNull($instanceSpy->getInput());
$this->assertEquals($output, 1);

$output = $proxy->notNullableBuiltInReturnType();
$this->assertEquals('notNullableBuiltInReturnType', $proxy::$method);
$this->assertNull($proxy::$input);
$this->assertEquals('notNullableBuiltInReturnType', $instanceSpy->getMethod());
$this->assertNull($instanceSpy->getInput());
$this->assertEquals($output, true);

$output = $proxy->notNullableNotBuiltInReturnType();
$this->assertEquals('notNullableNotBuiltInReturnType', $proxy::$method);
$this->assertNull($proxy::$input);
$this->assertEquals('notNullableNotBuiltInReturnType', $instanceSpy->getMethod());
$this->assertNull($instanceSpy->getInput());
$this->assertEquals($output, new Dummy());

$output = $proxy->nullableNotBuiltInReturnType();
$this->assertEquals('nullableNotBuiltInReturnType', $proxy::$method);
$this->assertNull($proxy::$input);
$this->assertEquals('nullableNotBuiltInReturnType', $instanceSpy->getMethod());
$this->assertNull($instanceSpy->getInput());
$this->assertEquals($output, new Dummy());

$proxy->noTypeHintingParamType(10);
$this->assertEquals('noTypeHintingParamType', $proxy::$method);
$this->assertEquals(10, $proxy::$input);
$this->assertEquals('noTypeHintingParamType', $instanceSpy->getMethod());
$this->assertEquals(10, $instanceSpy->getInput());

$proxy->nullableBuiltInParamType(null);
$this->assertEquals('nullableBuiltInParamType', $proxy::$method);
$this->assertNull($proxy::$input);
$this->assertEquals('nullableBuiltInParamType', $instanceSpy->getMethod());
$this->assertNull($instanceSpy->getInput());

$proxy->notNullableBuiltInParamType(true);
$this->assertEquals('notNullableBuiltInParamType', $proxy::$method);
$this->assertTrue($proxy::$input);
$this->assertEquals('notNullableBuiltInParamType', $instanceSpy->getMethod());
$this->assertTrue($instanceSpy->getInput());

$proxy->notNullableNotBuiltInParamType(new Dummy());
$this->assertEquals('notNullableNotBuiltInParamType', $proxy::$method);
$this->assertEquals(new Dummy(), $proxy::$input);
$this->assertEquals('notNullableNotBuiltInParamType', $instanceSpy->getMethod());
$this->assertEquals(new Dummy(), $instanceSpy->getInput());

$proxy->nullableNotBuiltInParamType(null);
$this->assertEquals('nullableNotBuiltInParamType', $proxy::$method);
$this->assertNull($proxy::$input);
$this->assertEquals('nullableNotBuiltInParamType', $instanceSpy->getMethod());
$this->assertNull($instanceSpy->getInput());

$proxy->nullableDefaultParamType();
$this->assertEquals('nullableDefaultParamType', $proxy::$method);
$this->assertNull($proxy::$input);
$this->assertEquals('nullableDefaultParamType', $instanceSpy->getMethod());
$this->assertNull($instanceSpy->getInput());

$proxy->notNullableDefaultParamType();
$this->assertEquals('notNullableDefaultParamType', $proxy::$method);
$this->assertEquals(10, $proxy::$input);
$this->assertEquals('notNullableDefaultParamType', $instanceSpy->getMethod());
$this->assertEquals(10, $instanceSpy->getInput());

$proxy->notNullableExternalConstantDefaultParamType();
$this->assertEquals('notNullableExternalConstantDefaultParamType', $instanceSpy->getMethod());
$this->assertEquals(Dummy::SOME_VALUE, $instanceSpy->getInput());

$proxy->notNullableSelfConstantDefaultParamType();
$this->assertEquals('notNullableSelfConstantDefaultParamType', $instanceSpy->getMethod());
$this->assertEquals($proxy::SOME_VALUE, $instanceSpy->getInput());

$proxy->paramList(10, null, true, new Dummy(), null, new Dummy());
$this->assertEquals('paramList', $proxy::$method);
$this->assertEquals('paramList', $instanceSpy->getMethod());
$this->assertEquals(array(
10,
null,
Expand All @@ -84,13 +93,21 @@ public function testCreate()
null,
new Dummy(),
10,
), $proxy::$input);
), $instanceSpy->getInput());

$callable = function () {
return 10;
};
$proxy->withCallableParameter($callable);
$this->assertEquals('withCallableParameter', $proxy::$method);
$this->assertEquals($callable(), ($proxy::$input)());
$this->assertEquals('withCallableParameter', $instanceSpy->getMethod());
$this->assertEquals($callable(), ($instanceSpy->getInput())());

$proxy();
$this->assertEquals('__invoke', $instanceSpy->getMethod());
$this->assertNull($instanceSpy->getInput());

$clonedProxy = clone $proxy;
$this->assertEquals('__clone', $instanceSpy->getMethod());
$this->assertNull($instanceSpy->getInput());
}
}
66 changes: 46 additions & 20 deletions tests/TestSpy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,95 +6,121 @@

class TestSpy
{
public static $method;
public static $input;
const SOME_VALUE = 'self-value';

private static function setVars($method, $input = null)
/**
* @var InstanceSpy
*/
private $instanceSpy;

/**
* TestSpy constructor.
*/
public function __construct(InstanceSpy $instanceSpy)
{
static::$method = $method;
static::$input = $input;
$this->instanceSpy = $instanceSpy;
}

public function noReturnType()
{
static::setVars('noReturnType');
$this->instanceSpy->setVars('noReturnType');
}

public function voidReturnType(): void
{
static::setVars('voidReturnType');
$this->instanceSpy->setVars('voidReturnType');
}

public function nullableBuiltInReturnType(): ?int
{
static::setVars('nullableBuiltInReturnType');
$this->instanceSpy->setVars('nullableBuiltInReturnType');

return 1;
}

public function notNullableBuiltInReturnType(): bool
{
static::setVars('notNullableBuiltInReturnType');
$this->instanceSpy->setVars('notNullableBuiltInReturnType');

return true;
}

public function notNullableNotBuiltInReturnType(): Dummy
{
static::setVars('notNullableNotBuiltInReturnType');
$this->instanceSpy->setVars('notNullableNotBuiltInReturnType');

return new Dummy();
}

public function nullableNotBuiltInReturnType(): ?Dummy
{
static::setVars('nullableNotBuiltInReturnType');
$this->instanceSpy->setVars('nullableNotBuiltInReturnType');

return new Dummy();
}

public function noTypeHintingParamType($value)
{
static::setVars('noTypeHintingParamType', $value);
$this->instanceSpy->setVars('noTypeHintingParamType', $value);
}

public function nullableBuiltInParamType(?int $value)
{
static::setVars('nullableBuiltInParamType', $value);
$this->instanceSpy->setVars('nullableBuiltInParamType', $value);
}

public function notNullableBuiltInParamType(bool $value)
{
static::setVars('notNullableBuiltInParamType', $value);
$this->instanceSpy->setVars('notNullableBuiltInParamType', $value);
}

public function notNullableNotBuiltInParamType(Dummy $value)
{
static::setVars('notNullableNotBuiltInParamType', $value);
$this->instanceSpy->setVars('notNullableNotBuiltInParamType', $value);
}

public function nullableNotBuiltInParamType(?Dummy $value)
{
static::setVars('nullableNotBuiltInParamType', $value);
$this->instanceSpy->setVars('nullableNotBuiltInParamType', $value);
}

public function nullableDefaultParamType(?Dummy $juan = null)
{
static::setVars('nullableDefaultParamType', $juan);
$this->instanceSpy->setVars('nullableDefaultParamType', $juan);
}

public function notNullableDefaultParamType(int $value = 10)
{
static::setVars('notNullableDefaultParamType', $value);
$this->instanceSpy->setVars('notNullableDefaultParamType', $value);
}

public function notNullableExternalConstantDefaultParamType(string $value = Dummy::SOME_VALUE)
{
$this->instanceSpy->setVars('notNullableExternalConstantDefaultParamType', $value);
}

public function notNullableSelfConstantDefaultParamType(string $value = self::SOME_VALUE)
{
$this->instanceSpy->setVars('notNullableSelfConstantDefaultParamType', $value);
}

public function paramList($value1, ?int $value2, bool $value3, Dummy $value4, ?Dummy $value5, ?Dummy $value6 = null, int $value7 = 10)
{
static::setVars('paramList', \func_get_args());
$this->instanceSpy->setVars('paramList', \func_get_args());
}

public function withCallableParameter(callable $param)
{
static::setVars('withCallableParameter', $param);
$this->instanceSpy->setVars('withCallableParameter', $param);
}

public function __invoke()
{
$this->instanceSpy->setVars('__invoke');
}

public function __clone()
{
$this->instanceSpy->setVars('__clone');
}
}

0 comments on commit 5266eb7

Please sign in to comment.