-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/2.x'
* origin/2.x: Fix constructor invocation to know about special parent class names, fix #425
- Loading branch information
Showing
2 changed files
with
111 additions
and
4 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
97 changes: 97 additions & 0 deletions
97
tests/Go/Aop/Framework/ReflectionConstructorInvocationTest.php
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,97 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
/* | ||
* Go! AOP framework | ||
* | ||
* @copyright Copyright 2019, Lisachenko Alexander <[email protected]> | ||
* | ||
* This source file is subject to the license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Go\Aop\Framework; | ||
|
||
use Go\Core\AspectContainer; | ||
|
||
class ReflectionConstructorInvocationTest extends AbstractInterceptorTest | ||
{ | ||
public function testCanCreateObjectDuringInvocation(): void | ||
{ | ||
$invocation = new ReflectionConstructorInvocation(\Exception::class, 'unused', []); | ||
$result = $invocation->__invoke(); | ||
$this->assertInstanceOf(\Exception::class, $result); | ||
} | ||
|
||
public function testKnowsAboutSpecialClassSuffix(): void | ||
{ | ||
$specialName = \Exception::class . AspectContainer::AOP_PROXIED_SUFFIX; | ||
$invocation = new ReflectionConstructorInvocation($specialName, 'unused', []); | ||
$result = $invocation->__invoke(); | ||
$this->assertInstanceOf(\Exception::class, $result); | ||
} | ||
|
||
public function testCanExecuteAdvicesDuringConstruct(): void | ||
{ | ||
$sequence = []; | ||
$advice = $this->getAdvice($sequence); | ||
$before = new BeforeInterceptor($advice); | ||
$invocation = new ReflectionConstructorInvocation(\Exception::class, 'unused', [$before]); | ||
$this->assertEmpty($sequence); | ||
$invocation->__invoke(['Message', 100]); | ||
$this->assertContains('advice', $sequence); | ||
} | ||
|
||
public function testStringRepresentation(): void | ||
{ | ||
$invocation = new ReflectionConstructorInvocation(\Exception::class, 'unused', []); | ||
$name = (string)$invocation; | ||
|
||
$this->assertEquals('initialization(Exception)', $name); | ||
} | ||
|
||
public function testReturnsConstructor(): void | ||
{ | ||
$invocation = new ReflectionConstructorInvocation(\Exception::class, 'unused', []); | ||
$ctor = $invocation->getConstructor(); | ||
$this->assertInstanceOf(\ReflectionMethod::class, $ctor); | ||
$this->assertEquals('__construct', $ctor->name); | ||
} | ||
|
||
public function testReturnsThis(): void | ||
{ | ||
$invocation = new ReflectionConstructorInvocation(\Exception::class, 'unused', []); | ||
$instance = $invocation->getThis(); | ||
$this->assertNull($instance); | ||
$object = $invocation->__invoke(['Some error', 100]); | ||
$this->assertEquals($object, $invocation->getThis()); | ||
} | ||
|
||
public function testCanCreateAnInstanceEvenWithNonPublicConstructor(): void | ||
{ | ||
try { | ||
$testClassInstance = new class('Test') { | ||
public $message; | ||
|
||
private function __construct(string $message) | ||
{ | ||
$this->message = $message; | ||
} | ||
}; | ||
$loadedClass = get_class($testClassInstance); | ||
} catch (\Error $e) { | ||
// let's look for all class names to find our anonymous one | ||
foreach (get_declared_classes() as $loadedClass) { | ||
$refClass = new \ReflectionClass($loadedClass); | ||
if ($refClass->getFileName() === __FILE__ && strpos($refClass->getName(), 'anonymous') !== false) { | ||
// loadedClass will contain our anonymous class | ||
break; | ||
} | ||
} | ||
} | ||
$testClassName = $loadedClass; | ||
$invocation = new ReflectionConstructorInvocation($testClassName, 'unused', []); | ||
$result = $invocation->__invoke(['Hello']); | ||
$this->assertInstanceOf($testClassName, $result); | ||
$this->assertSame('Hello', $result->message); | ||
} | ||
} |