2013-12-05 02:21:31 +01:00
|
|
|
<?php
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
namespace SilverStripe\Core\Tests\Injector;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Injector\AopProxyService;
|
2016-12-16 05:34:21 +01:00
|
|
|
use SilverStripe\Core\Tests\Injector\AopProxyServiceTest\BeforeAfterCallTestAspect;
|
|
|
|
use SilverStripe\Core\Tests\Injector\AopProxyServiceTest\ProxyTestObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* @author <marcus@silverstripe.com.au>
|
|
|
|
* @license BSD License http://www.silverstripe.org/bsd-license
|
|
|
|
*/
|
|
|
|
class AopProxyServiceTest extends SapphireTest
|
|
|
|
{
|
|
|
|
public function testBeforeMethodsCalled()
|
|
|
|
{
|
|
|
|
$proxy = new AopProxyService();
|
|
|
|
$aspect = new BeforeAfterCallTestAspect();
|
|
|
|
$proxy->beforeCall = array(
|
|
|
|
'myMethod' => $aspect
|
|
|
|
);
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$proxy->proxied = new ProxyTestObject();
|
|
|
|
|
|
|
|
$result = $proxy->myMethod();
|
|
|
|
|
|
|
|
$this->assertEquals('myMethod', $aspect->called);
|
|
|
|
$this->assertEquals(42, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBeforeMethodBlocks()
|
|
|
|
{
|
|
|
|
$proxy = new AopProxyService();
|
|
|
|
$aspect = new BeforeAfterCallTestAspect();
|
|
|
|
$aspect->block = true;
|
|
|
|
|
|
|
|
$proxy->beforeCall = array(
|
|
|
|
'myMethod' => $aspect
|
|
|
|
);
|
|
|
|
|
|
|
|
$proxy->proxied = new ProxyTestObject();
|
|
|
|
|
|
|
|
$result = $proxy->myMethod();
|
|
|
|
|
|
|
|
$this->assertEquals('myMethod', $aspect->called);
|
|
|
|
|
|
|
|
// the actual underlying method will NOT have been called
|
|
|
|
$this->assertNull($result);
|
|
|
|
|
|
|
|
// set up an alternative return value
|
|
|
|
$aspect->alternateReturn = 84;
|
|
|
|
|
|
|
|
$result = $proxy->myMethod();
|
2013-12-05 02:21:31 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals('myMethod', $aspect->called);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// the actual underlying method will NOT have been called,
|
|
|
|
// instead the alternative return value
|
|
|
|
$this->assertEquals(84, $result);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testAfterCall()
|
|
|
|
{
|
|
|
|
$proxy = new AopProxyService();
|
|
|
|
$aspect = new BeforeAfterCallTestAspect();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$proxy->afterCall = array(
|
|
|
|
'myMethod' => $aspect
|
|
|
|
);
|
2013-12-05 02:21:31 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$proxy->proxied = new ProxyTestObject();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$aspect->modifier = function ($value) {
|
|
|
|
return $value * 2;
|
|
|
|
};
|
2013-12-05 02:21:31 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $proxy->myMethod();
|
|
|
|
$this->assertEquals(84, $result);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|