decorator php,装饰器模式(Decorator)

namespace DesignPatterns\Structural\Decorator\Tests;

use DesignPatterns\Structural\Decorator;

/**

* DecoratorTest 用于测试装饰器模式

*/

class DecoratorTest extends \PHPUnit_Framework_TestCase

{

protected $service;

protected function setUp()

{

$this->service = new Decorator\Webservice(array('foo' => 'bar'));

}

public function testJsonDecorator()

{

// Wrap service with a JSON decorator for renderers

$service = new Decorator\RenderInJson($this->service);

// Our Renderer will now output JSON instead of an array

$this->assertEquals('{"foo":"bar"}', $service->renderData());

}

public function testXmlDecorator()

{

// Wrap service with a XML decorator for renderers

$service = new Decorator\RenderInXml($this->service);

// Our Renderer will now output XML instead of an array

$xml = '<?xml version="1.0"?>bar';

$this->assertXmlStringEqualsXmlString($xml, $service->renderData());

}

/**

* The first key-point of this pattern :

*/

public function testDecoratorMustImplementsRenderer()

{

$className = 'DesignPatterns\Structural\Decorator\Decorator';

$interfaceName = 'DesignPatterns\Structural\Decorator\RendererInterface';

$this->assertTrue(is_subclass_of($className, $interfaceName));

}

/**

* Second key-point of this pattern : the decorator is type-hinted

*

* @expectedException \PHPUnit_Framework_Error

*/

public function testDecoratorTypeHinted()

{

if (version_compare(PHP_VERSION, '7', '>=')) {

throw new \PHPUnit_Framework_Error('Skip test for PHP 7', 0, __FILE__, __LINE__);

}

$this->getMockForAbstractClass('DesignPatterns\Structural\Decorator\Decorator', array(new \stdClass()));

}

/**

* Second key-point of this pattern : the decorator is type-hinted

*

* @requires PHP 7

* @expectedException TypeError

*/

public function testDecoratorTypeHintedForPhp7()

{

$this->getMockForAbstractClass('DesignPatterns\Structural\Decorator\Decorator', array(new \stdClass()));

}

/**

* The decorator implements and wraps the same interface

*/

public function testDecoratorOnlyAcceptRenderer()

{

$mock = $this->getMock('DesignPatterns\Structural\Decorator\RendererInterface');

$dec = $this->getMockForAbstractClass('DesignPatterns\Structural\Decorator\Decorator', array($mock));

$this->assertNotNull($dec);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值