Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorator base class for object manager decorators #229

Merged
merged 1 commit into from Jan 20, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
140 changes: 140 additions & 0 deletions lib/Doctrine/Common/Persistence/ObjectManagerDecorator.php
@@ -0,0 +1,140 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\Common\Persistence;

/**
* Base class to simplify ObjectManager decorators
*
* @license http://opensource.org/licenses/MIT MIT
* @link www.doctrine-project.org
* @since 2.4
* @author Lars Strojny <lars@strojny.net>
*/
abstract class ObjectManagerDecorator implements ObjectManager
{
/**
* @var ObjectManager
*/
protected $wrapped;

/**
* {@inheritdoc}
*/
public function find($className, $id)
{
return $this->wrapped->find($className, $id);
}

/**
* {@inheritdoc}
*/
public function persist($object)
{
return $this->wrapped->persist($object);
}

/**
* {@inheritdoc}
*/
public function remove($object)
{
return $this->wrapped->remove($object);
}

/**
* {@inheritdoc}
*/
public function merge($object)
{
return $this->wrapped->merge($object);
}

/**
* {@inheritdoc}
*/
public function clear($objectName = null)
{
return $this->wrapped->clear($objectName);
}

/**
* {@inheritdoc}
*/
public function detach($object)
{
return $this->wrapped->detach($object);
}

/**
* {@inheritdoc}
*/
public function refresh($object)
{
return $this->wrapped->refresh($object);
}

/**
* {@inheritdoc}
*/
public function flush()
{
return $this->wrapped->flush();
}

/**
* {@inheritdoc}
*/
public function getRepository($className)
{
return $this->wrapped->getRepository($className);
}

/**
* {@inheritdoc}
*/
public function getClassMetadata($className)
{
return $this->wrapped->getClassMetadata($className);
}

/**
* {@inheritdoc}
*/
public function getMetadataFactory()
{
return $this->wrapped->getMetadataFactory();
}

/**
* {@inheritdoc}
*/
public function initializeObject($obj)
{
return $this->wrapped->initializeObject($obj);
}

/**
* {@inheritdoc}
*/
public function contains($object)
{
return $this->wrapped->contains($object);
}
}
@@ -0,0 +1,60 @@
<?php

namespace Doctrine\Tests\Common\Persistence;

use Doctrine\Common\Persistence\ObjectManagerDecorator;
use Doctrine\Common\Persistence\ObjectManager;

class NullObjectManagerDecorator extends ObjectManagerDecorator
{
public function __construct(ObjectManager $wrapped)
{
$this->wrapped = $wrapped;
}
}

class ObjectManagerDecoratorTest extends \PHPUnit_Framework_TestCase
{
private $wrapped;
private $decorated;

public function setUp()
{
$this->wrapped = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
$this->decorated = new NullObjectManagerDecorator($this->wrapped);
}

public function getMethodParameters()
{
$class = new \ReflectionClass('Doctrine\Common\Persistence\ObjectManager');

$methods = array();
foreach ($class->getMethods() as $method) {
if ($method->getNumberOfRequiredParameters() === 0) {
$methods[] = array($method->getName(), array());
} elseif ($method->getNumberOfRequiredParameters() > 0) {
$methods[] = array($method->getName(), array_fill(0, $method->getNumberOfRequiredParameters(), 'req') ?: array());
}
if ($method->getNumberOfParameters() != $method->getNumberOfRequiredParameters()) {
$methods[] = array($method->getName(), array_fill(0, $method->getNumberOfParameters(), 'all') ?: array());
}
}

return $methods;
}

/**
* @dataProvider getMethodParameters
*/
public function testAllMethodCallsAreDelegatedToTheWrappedInstance($method, array $parameters)
{
$stub = $this->wrapped
->expects($this->once())
->method($method)
->will($this->returnValue('INNER VALUE FROM ' . $method));

call_user_func_array(array($stub, 'with'), $parameters);

$this->assertSame('INNER VALUE FROM ' . $method, call_user_func_array(array($this->decorated, $method), $parameters));
}
}