Skip to content

Commit

Permalink
-n
Browse files Browse the repository at this point in the history
Merge pull request Sylius#7 from umpirsky/entity-alias

Added some specs for doctrine repos, generate human readable alias.
  • Loading branch information
Paweł Jędrzejewski committed Dec 6, 2012
2 parents ed00b67 + 5607e60 commit 2e4b49c
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 6 deletions.
8 changes: 4 additions & 4 deletions Doctrine/ORM/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Pagerfanta\Pagerfanta;

/**
* Doctrine ORM driver entity manager.
* Doctrine ORM driver entity repository.
*
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
*/
Expand Down Expand Up @@ -45,7 +45,7 @@ public function findAll()
return $this
->getCollectionQueryBuilder()
->getQuery()
->execute()
->getResult()
;
}

Expand Down Expand Up @@ -78,7 +78,7 @@ public function findBy(array $criteria, array $orderBy = null, $limit = null, $o

return $queryBuilder
->getQuery()
->execute()
->getResult()
;
}

Expand Down Expand Up @@ -133,6 +133,6 @@ protected function applySorting(QueryBuilder $queryBuilder, array $sorting = nul

protected function getAlias()
{
return 'o';
return lcfirst(substr(strrchr($this->getClassName(), '\\'), 1));
}
}
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@
"white-october/pagerfanta-bundle": "*"
},
"require-dev": {
"phpspec/phpspec2": "dev-develop"
"doctrine/orm": "~2.3",
"doctrine/mongodb-odm": "*",
"phpspec/phpspec2": "dev-develop"
},
"suggest": {
"doctrine/orm": "~2.3",
"doctrine/mongodb-odm": "*"
},
"config": {
"bin-dir": "bin"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class Configuration extends ObjectBehavior
{
/**
* @param Symfony\Component\HttpFoundation\Request $request
* @param Symfony\Component\HttpFoundation\Request $request
* @param Symfony\Component\HttpFoundation\ParameterBag $attributes
*/
function let($request, $attributes)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace spec\Sylius\Bundle\ResourceBundle\Doctrine\ODM\MongoDB;

use PHPSpec2\ObjectBehavior;

/**
* Doctrine ODM driver document repository spec.
*
* @author Саша Стаменковић <umpirsky@gmail.com>
*/
class DocumentRepository extends ObjectBehavior
{
/**
* @param Doctrine\ODM\MongoDB\DocumentManager $documentManager
* @param Doctrine\ODM\MongoDB\UnitOfWork $unitOfWork
* @param Doctrine\ODM\MongoDB\Mapping\ClassMetadata $class
* @param Doctrine\ODM\MongoDB\Query\Builder $queryBuilder
*/
function let($documentManager, $unitOfWork, $class, $queryBuilder)
{
$class->name = 'spec\Sylius\Bundle\ResourceBundle\Fixture\Entity\Foo';

$documentManager
->createQueryBuilder($class->name)
->willReturn($queryBuilder)
;

$this->beConstructedWith($documentManager, $unitOfWork, $class);
}

function it_should_be_initializable()
{
$this->shouldHaveType('Sylius\Bundle\ResourceBundle\Doctrine\ODM\MongoDB\DocumentRepository');
$this->shouldHaveType('Doctrine\ODM\MongoDB\DocumentRepository');
}

function it_should_create_paginator()
{
$this
->createPaginator()
->shouldHaveType('Pagerfanta\Pagerfanta')
;
}
}
124 changes: 124 additions & 0 deletions spec/Sylius/Bundle/ResourceBundle/Doctrine/ORM/EntityRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace spec\Sylius\Bundle\ResourceBundle\Doctrine\ORM;

use PHPSpec2\ObjectBehavior;

/**
* Doctrine ORM driver entity repository spec.
*
* @author Саша Стаменковић <umpirsky@gmail.com>
*/
class EntityRepository extends ObjectBehavior
{
/**
* @param Doctrine\ORM\EntityManager $entityManager
* @param Doctrine\ORM\Mapping\ClassMetadata $class
* @param Doctrine\ORM\QueryBuilder $queryBuilder
*/
function let($entityManager, $class, $queryBuilder)
{
$class->name = 'spec\Sylius\Bundle\ResourceBundle\Fixture\Entity\Foo';

$entityManager
->createQueryBuilder()
->willReturn($queryBuilder)
;

$queryBuilder
->select(ANY_ARGUMENT)
->willReturn($queryBuilder)
;
$queryBuilder
->from(ANY_ARGUMENTS)
->willReturn($queryBuilder)
;

$this->beConstructedWith($entityManager, $class);
}

function it_should_be_initializable()
{
$this->shouldHaveType('Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository');
$this->shouldHaveType('Doctrine\ORM\EntityRepository');
}

function it_should_create_new()
{
$this->createNew()->shouldHaveType('spec\Sylius\Bundle\ResourceBundle\Fixture\Entity\Foo');
}

function it_should_return_null_if_not_found()
{
$this->find(1)->shouldReturn(null);
}

/**
* @param Doctrine\ORM\QueryBuilder $queryBuilder
*/
function it_should_apply_criteria_when_finding_one_by_criteria($queryBuilder)
{
$criteria = array(
'foo' => 'bar',
'bar' => 'baz',
);

$this->it_should_apply_criteria($criteria, $queryBuilder);

$this->findOneBy($criteria)->shouldReturn(null);
}

/**
* @param Doctrine\ORM\QueryBuilder $queryBuilder
*/
function it_should_apply_criteria_when_finding_by_criteria($queryBuilder)
{
$criteria = array(
'foo' => 'bar',
'bar' => 'baz',
);

$this->it_should_apply_criteria($criteria, $queryBuilder);

$this->findBy($criteria)->shouldReturn(null);
}

function it_should_return_null_if_all_not_found()
{
$this->findAll()->shouldReturn(null);
}

function it_should_create_paginator()
{
$this
->createPaginator()
->shouldHaveType('Pagerfanta\Pagerfanta')
;
}

private function it_should_apply_criteria(array $criteria, $queryBuilder)
{
foreach ($criteria as $property => $value) {
$queryBuilder
->andWhere('foo.'.$property.' = :'.$property)
->shouldBeCalled()
->willReturn($queryBuilder)
;

$queryBuilder
->setParameter($property, $value)
->shouldBeCalled()
->willReturn($queryBuilder)
;
}
}
}
21 changes: 21 additions & 0 deletions spec/Sylius/Bundle/ResourceBundle/Fixture/Entity/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace spec\Sylius\Bundle\ResourceBundle\Fixture\Entity;

/**
* Foo entity.
*
* @author Саша Стаменковић <umpirsky@gmail.com>
*/
class Foo
{
}

0 comments on commit 2e4b49c

Please sign in to comment.