Skip to content

Commit

Permalink
Merge pull request #35 from bakura10/develop
Browse files Browse the repository at this point in the history
Ajout tests unitaires + ajout/modification de repositories
  • Loading branch information
bakura10 committed Oct 30, 2012
2 parents ad4484f + 5d7e9d3 commit d6749e8
Show file tree
Hide file tree
Showing 26 changed files with 742 additions and 112 deletions.
11 changes: 7 additions & 4 deletions Module.php
Expand Up @@ -132,8 +132,10 @@ public function getServiceConfig()
return new Service\CategoryService($categoryMapper);
},
'ZfrForum\Service\PostService' => function($serviceManager) {
$postMapper = $serviceManager->get('ZfrForum\Mapper\PostMapperInterface');
return new Service\PostService($postMapper);
$postMapper = $serviceManager->get('ZfrForum\Mapper\PostMapperInterface');
$reportMapper = $serviceManager->get('ZfrForum\Mapper\ReportMapperInterface');
$authentication = $serviceManager->get('Zend\Authentication\AuthenticationService');
return new Service\PostService($postMapper, $reportMapper, $authentication);
},
'ZfrForum\Service\RankService' => function($serviceManager) {
$rankMapper = $serviceManager->get('ZfrForum\Mapper\RankMapperInterface');
Expand All @@ -144,8 +146,9 @@ public function getServiceConfig()
return new Service\SettingsService($settingsMapper);
},
'ZfrForum\Service\ThreadService' => function($serviceManager) {
$threadMapper = $serviceManager->get('ZfrForum\Mapper\ThreadMapperInterface');
return new Service\ThreadService($threadMapper);
$threadMapper = $serviceManager->get('ZfrForum\Mapper\ThreadMapperInterface');
$authentication = $serviceManager->get('Zend\Authentication\AuthenticationService');
return new Service\ThreadService($threadMapper, $authentication);
},
'ZfrForum\Service\UserBanService' => function($serviceManager) {
$userBanMapper = $serviceManager->get('ZfrForum\Mapper\UserBanMapperInterface');
Expand Down
9 changes: 9 additions & 0 deletions config/module.config.php
Expand Up @@ -6,6 +6,15 @@
*/
'router' => include 'module.config.routes.php',

/**
* Override ZfcUser options
*/
'zfcuser' => array(
// We don't want the original User entity to be generated as we have our own extended class
// for ZfrForum
'enable_default_entities' => false
),

/**
* Doctrine configuration
*/
Expand Down
19 changes: 8 additions & 11 deletions src/ZfrForum/Entity/Category.php
Expand Up @@ -54,7 +54,7 @@ class Category
*
* @ORM\Column(type="string", length=128)
*/
protected $name;
protected $name = '';

/**
* @var string
Expand All @@ -68,7 +68,7 @@ class Category
*
* @ORM\Column(type="smallint")
*/
protected $depth = 1;
protected $depth = 0;

/**
* @var int
Expand Down Expand Up @@ -96,20 +96,17 @@ public function getId()
}

/**
* Set the parent category (null if none)
* Set the parent category
*
* @param Category $parent
* @return Category
*/
public function setParent(Category $parent = null)
public function setParent(Category $parent)
{
$this->parent = $parent;

if ($parent !== null) {
$this->setDepth($parent->getDepth() + 1)
->setLeftBound($parent->getRightBound())
->setRightBound($parent->getRightBound() + 1);
}
$this->setDepth($parent->getDepth() + 1)
->setLeftBound($parent->getRightBound())
->setRightBound($parent->getRightBound() + 1);

return $this;
}
Expand All @@ -131,7 +128,7 @@ public function getParent()
*/
public function hasParent()
{
return !($this->parent === null);
return ($this->depth > 1);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ZfrForum/Entity/Post.php
Expand Up @@ -76,7 +76,7 @@ class Post
/**
* @var DateTime
*
* @ORM\Column(type="datetime")
* @ORM\Column(type="datetime", nullable=true)
*/
protected $lastModifiedAt;

Expand Down
6 changes: 4 additions & 2 deletions src/ZfrForum/Entity/Report.php
Expand Up @@ -23,7 +23,7 @@
use ZfrForum\Entity\UserInterface;

/**
* @ORM\Entity(readOnly=true)
* @ORM\Entity(repositoryClass="ZfrForum\Repository\ReportRepository", readOnly=true)
* @ORM\Table(name="Reports", uniqueConstraints={
* @ORM\UniqueConstraint(name="UNIQ_C38372B2B6BD307F", columns={"post_id", "reportedBy_id"})
* })
Expand Down Expand Up @@ -148,11 +148,13 @@ public function getReportedAt()
/**
* Set the description of the report
*
* @param string $description
* @param string $description
* @return Report
*/
public function setDescription($description)
{
$this->description = (string) $description;
return $this;
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/ZfrForum/Entity/Thread.php
Expand Up @@ -71,7 +71,7 @@ class Thread
/**
* @var Collection
*
* @ORM\OneToMany(targetEntity="ZfrForum\Entity\Post", mappedBy="thread", fetch="EXTRA_LAZY")
* @ORM\OneToMany(targetEntity="ZfrForum\Entity\Post", mappedBy="thread", cascade={"persist"}, fetch="EXTRA_LAZY")
* @ORM\OrderBy({"sentAt"="ASC"})
*/
protected $posts;
Expand All @@ -85,9 +85,12 @@ class Thread

/**
* @var Collection
* TODO: remettre UserInterface
*
* @ORM\ManyToMany(targetEntity="ZfrForum\Entity\UserInterface", fetch="EXTRA_LAZY")
* @ORM\JoinTable(name="ThreadsFollowers")
* @ORM\ManyToMany(targetEntity="ZfrForum\Entity\User", fetch="EXTRA_LAZY")
* @ORM\JoinTable(name="ThreadsFollowers",
* inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}
* )
*/
protected $followers;

Expand Down
5 changes: 3 additions & 2 deletions src/ZfrForum/Entity/User.php
Expand Up @@ -23,7 +23,8 @@
use ZfcUser\Entity\User as BaseUser;

/**
* @ORM\MappedSuperclass
* @ORM\Entity
* @ORM\Table(name="Users")
*/
class User extends BaseUser implements UserInterface
{
Expand All @@ -32,7 +33,7 @@ class User extends BaseUser implements UserInterface
*
* @ORM\Column(type="string", length=39)
*/
protected $ip;
protected $ip = '';

/**
* @var DateTime
Expand Down
9 changes: 7 additions & 2 deletions src/ZfrForum/Mapper/CategoryMapperInterface.php
Expand Up @@ -29,15 +29,20 @@ interface CategoryMapperInterface extends ObjectRepository
*/
public function create(Category $category);

/**
* @param Category $category
* @return Category
*/
public function update(Category $category);

/**
* @param Category $category
* @return void
*/
public function remove(Category $category);

/**
* @param Category $category
* @return Category
*/
public function update(Category $category);
public function findRoot();
}
7 changes: 0 additions & 7 deletions src/ZfrForum/Mapper/PostMapperInterface.php
Expand Up @@ -20,7 +20,6 @@

use Doctrine\Common\Persistence\ObjectRepository;
use ZfrForum\Entity\Post;
use ZfrForum\Entity\Report;

interface PostMapperInterface extends ObjectRepository
{
Expand All @@ -29,10 +28,4 @@ interface PostMapperInterface extends ObjectRepository
* @return Post
*/
public function update(Post $post);

/**
* @param Report $report
* @return void
*/
public function addReport(Report $report);
}
4 changes: 2 additions & 2 deletions src/ZfrForum/Mapper/RankMapperInterface.php
Expand Up @@ -33,11 +33,11 @@ public function create(Rank $rank);
* @param Rank $rank
* @return void
*/
public function remove(Rank $rank);
public function update(Rank $rank);

/**
* @param Rank $rank
* @return void
*/
public function update(Rank $rank);
public function remove(Rank $rank);
}
39 changes: 39 additions & 0 deletions src/ZfrForum/Mapper/ReportMapperInterface.php
@@ -0,0 +1,39 @@
<?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.
*/

namespace ZfrForum\Mapper;

use Doctrine\Common\Persistence\ObjectRepository;
use Zend\Paginator\Paginator;
use ZfrForum\Entity\Post;
use ZfrForum\Entity\Report;

interface ReportMapperInterface extends ObjectRepository
{
/**
* @param Report $report
* @return Report
*/
public function create(Report $report);

/**
* @param Post $post
* @return Paginator
*/
public function findByPost(Post $post);
}
1 change: 0 additions & 1 deletion src/ZfrForum/Mapper/ThreadMapperInterface.php
Expand Up @@ -21,7 +21,6 @@
use Doctrine\Common\Persistence\ObjectRepository;
use Zend\Paginator\Paginator;
use ZfrForum\Entity\Category;
use ZfrForum\Entity\Post;
use ZfrForum\Entity\Thread;

interface ThreadMapperInterface extends ObjectRepository
Expand Down
56 changes: 38 additions & 18 deletions src/ZfrForum/Repository/CategoryRepository.php
Expand Up @@ -34,24 +34,26 @@ class CategoryRepository extends EntityRepository implements CategoryMapperInter
*/
public function create(Category $category)
{
$em = $this->getEntityManager();
// Does not have a parent ? Add the super root
if (!$category->hasParent()) {
$category->setParent($this->findRoot());
}

if ($category->hasParent()) {
$queryBuilder = $em->createQueryBuilder();
$em = $this->getEntityManager();
$queryBuilder = $em->createQueryBuilder();

// First update right bounds
$queryBuilder->update('ZfrForum\Entity\Category', 'c')
->set('c.rightBound', 'c.rightBound + 2')
->where('c.rightBound >= :rightBound')
->setParameter('rightBound', $category->getParent()->getRightBound())
->getQuery()->execute();
// First update right bounds
$queryBuilder->update('ZfrForum\Entity\Category', 'c')
->set('c.rightBound', 'c.rightBound + 2')
->where('c.rightBound >= :rightBound')
->setParameter('rightBound', $category->getParent()->getRightBound())
->getQuery()->execute();

// Then left bounds
$queryBuilder->resetDQLParts(array('set', 'where'))
->set('c.leftBound', 'c.leftBound + 2')
->where('c.leftBound >= :rightBound')
->getQuery()->execute();
}
// Then left bounds
$queryBuilder->resetDQLParts(array('set', 'where'))
->set('c.leftBound', 'c.leftBound + 2')
->where('c.leftBound >= :rightBound')
->getQuery()->execute();

// Finally, add the category
$em->persist($category);
Expand All @@ -63,6 +65,15 @@ public function create(Category $category)
return $em->merge($category);
}

/**
* @param Category $category
* @return Category
*/
public function update(Category $category)
{
// TODO: Implement update() method.
}

/**
* Note : to efficiently remove a category, we perform an UPDATE at SQL level. To work correctly with
* Doctrine 2, we need to clear the entity manager at the end of the operation. As a consequence, you need
Expand Down Expand Up @@ -101,11 +112,20 @@ public function remove(Category $category)
}

/**
* @param Category $category
* This function returns a special category node
*
* @return Category
*/
public function update(Category $category)
public function findRoot()
{
// TODO: Implement update() method.
$root = $this->findOneBy(array('parent' => null));

if ($root === null) {
$root = new Category();
$this->_em->persist($root);
$this->_em->flush();
}

return $root;
}
}
14 changes: 2 additions & 12 deletions src/ZfrForum/Repository/PostRepository.php
Expand Up @@ -20,12 +20,13 @@

use Doctrine\ORM\EntityRepository;
use ZfrForum\Entity\Post;
use ZfrForum\Entity\Report;
use ZfrForum\Mapper\PostMapperInterface;

class PostRepository extends EntityRepository implements PostMapperInterface
{
/**
* Update the post
*
* @param Post $post
* @return mixed
*/
Expand All @@ -34,15 +35,4 @@ public function update(Post $post)
$this->getEntityManager()->flush($post);
return $post;
}

/**
* @param Report $report
* @return void
*/
public function addReport(Report $report)
{
$em = $this->getEntityManager();
$em->persist($report);
$em->flush();
}
}

0 comments on commit d6749e8

Please sign in to comment.