Skip to content

Instantly share code, notes, and snippets.

@VanTanev
Created May 21, 2013 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VanTanev/5622561 to your computer and use it in GitHub Desktop.
Save VanTanev/5622561 to your computer and use it in GitHub Desktop.
<?php
namespace DG\CommonBundle\Model;
/**
* QueryFactory to satisfy PHPSpec
*
* @author Ivan Plamenov Tanev aka CraftyShadow <vankata.t@gmail.com>
*/
class QueryFactory
{
protected $ns_prefix;
public function setNamespacePrefix($ns_prefix)
{
$this->ns_prefix = $ns_prefix;
}
/**
* @param string $name
* @param string $modelAlias
* @param Criteria $criteria
*
* @return ModelCriteria
*/
public function create($name, $modelAlias = null, $criteria = null)
{
if (false === strpos($name, '\\')) {
$name = $this->ns_prefix . '\\' . $name;
}
return call_user_func(array($name, 'create'), $modelAlias, $criteria);
}
public function __call($name, $arguments)
{
if (0 === strpos($name, 'create')) {
return $this->create(
substr($name, 6),
isset($arguments[0]) ? $arguments[0] : null,
isset($arguments[1]) ? $arguments[1] : null
);
}
throw new \Exception('Call to undefined method: ' . $name);
}
}
<?php
use DG\GradiniBundle\Model\Webservice\QueryFactory;
class UsesQueries
{
/** @var QueryFactory **/
protected $qf;
public function __construct(QueryFactory $qf)
{
$this->qf = $qf;
}
public function doSomething()
{
//$q is the proper type and has autocompletion for all methods of RegionQuery
$q = $this->qf->createRegionQuery();
$q->find();
}
}
<?php
namespace spec\DG\GradiniBundle\Model\Webservice;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class QueryFactorySpec extends ObjectBehavior
{
/**
* @param DG\GradiniBundle\Model\Webservice\QueryFactory $qf;
*/
public function let($qf)
{
$this->beConstructedWith($qf)
}
/**
* @param DG\GradiniBundle\Model\Webservice\QueryFactory $qf;
* @param DG\GradiniBundle\Model\Webservice\RegionQuery $q;
*/
public function it_tests_do_something($qf, $q)
{
$q->find()->willReturn(array('whatever'));
$qf->getRegionQuery()->willReturn($q);
}
}
<?php
namespace DG\GradiniBundle\Model\Webservice;
use DG\CommonBundle\Model\QueryFactory as BaseQueryFactory;
/**
* QueryFactory for the GradiniBundle Webservice Model
*
* @author Ivan Plamenov Tanev aka CraftyShadow <vankata.t@gmail.com>
*/
class QueryFactory extends BaseQueryFactory
{
protected $ns_prefix = 'DG\\GradiniBundle\\Model\\Webservice';
/**
* @param string $modelAlias
* @param \Criteria|\ModelCriteria $criteria
* @return RegionQuery
*/
public function createRegionQuery($modelAlias = null, $criteria = null)
{
return $this->create('RegionQuery', $modelAlias, $criteria);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment