[TASK] Add Domain Model and Repository to Category
authorFabien Udriot <fabien.udriot@ecodev.ch>
Sat, 14 Jul 2012 15:03:03 +0000 (17:03 +0200)
committerIngo Pfennigstorf <i.pfennigstorf@gmail.com>
Thu, 2 Aug 2012 07:37:07 +0000 (09:37 +0200)
For extension developer's sake, it is convenient to provide a domain
model along with a repository. Both are relying on Extbase.

Change-Id: I7260a57dcc742f217f0ddfe19470673bc91532c9
Resolves: #38719
Releases: 6.0
Reviewed-on: http://review.typo3.org/12793
Reviewed-by: Oliver Klee
Tested-by: Oliver Klee
Reviewed-by: Fabien Udriot
Tested-by: Fabien Udriot
Reviewed-by: Sebastian Michaelsen
Reviewed-by: Jochen Rau
Reviewed-by: Georg Ringer
Tested-by: Georg Ringer
Reviewed-by: Mattias Nilsson
Tested-by: Mattias Nilsson
Reviewed-by: Markus Günther
Tested-by: Markus Günther
Reviewed-by: Ingo Pfennigstorf
Tested-by: Ingo Pfennigstorf
typo3/sysext/extbase/Classes/Domain/Model/Category.php [new file with mode: 0755]
typo3/sysext/extbase/Classes/Domain/Repository/CategoryRepository.php [new file with mode: 0755]
typo3/sysext/extbase/Tests/Unit/Domain/Model/CategoryTest.php [new file with mode: 0644]
typo3/sysext/extbase/Tests/Unit/Domain/Repository/CategoryRepositoryTest.php [new file with mode: 0644]

diff --git a/typo3/sysext/extbase/Classes/Domain/Model/Category.php b/typo3/sysext/extbase/Classes/Domain/Model/Category.php
new file mode 100755 (executable)
index 0000000..98ffdfa
--- /dev/null
@@ -0,0 +1,187 @@
+<?php
+/***************************************************************
+ *  Copyright notice
+*
+*  (c) 2012 Fabien Udriot <fabien.udriot@typo3.org>
+*  All rights reserved
+*
+*  This script is part of the TYPO3 project. The TYPO3 project is
+*  free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  The GNU General Public License can be found at
+*  http://www.gnu.org/copyleft/gpl.html.
+*
+*  This script is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+
+/**
+ * This model represents a category (for anything).
+ *
+ * @author Fabien Udriot <fabien.udriot@typo3.org>
+ * @package Extbase
+ * @subpackage Domain\Model
+ * @scope prototype
+ * @entity
+ * @api
+ */
+class Tx_Extbase_Domain_Model_Category extends Tx_Extbase_DomainObject_AbstractEntity {
+       /**
+        * @var string
+        * @validate notEmpty
+        */
+       protected $title = '';
+
+       /**
+        * @var string
+        */
+       protected $description = '';
+
+       /**
+        * @var Tx_Extbase_Domain_Model_Category|NULL
+        * @lazy
+        */
+       protected $parent = NULL;
+
+       /**
+        * @var Tx_Extbase_Persistence_ObjectStorage<Tx_Extbase_DomainObject_AbstractEntity>
+        * @lazy
+        */
+       protected $items = NULL;
+
+       /**
+        * The constructor.
+        */
+       public function __construct() {
+               $this->initStorageObjects();
+       }
+
+       /**
+        * Initializes all Tx_Extbase_Persistence_ObjectStorage properties.
+        *
+        * @return void
+        * @api
+        */
+       protected function initStorageObjects() {
+               $this->items = new Tx_Extbase_Persistence_ObjectStorage();
+       }
+
+       /**
+        * Gets the title.
+        *
+        * @return string the title, might be empty
+        * @api
+        */
+       public function getTitle() {
+               return $this->title;
+       }
+
+       /**
+        * Sets the title.
+        *
+        * @param string $title the title to set, may be empty
+        * @return void
+        * @api
+        */
+
+       public function setTitle($title) {
+               $this->title = $title;
+       }
+
+       /**
+        * Gets the description.
+        *
+        * @return string the description, might be empty
+        * @api
+        */
+       public function getDescription() {
+               return $this->description;
+       }
+
+       /**
+        * Sets the description.
+        *
+        * @param string $description the description to set, may be empty
+        * @return void
+        * @api
+        */
+       public function setDescription($description) {
+               $this->description = $description;
+       }
+
+       /**
+        * Gets the parent category.
+        *
+        * @return Tx_Extbase_Domain_Model_Category|NULL the parent category
+        * @api
+        */
+       public function getParent() {
+               if ($this->parent instanceof Tx_Extbase_Persistence_LazyLoadingProxy) {
+                       $this->parent->_loadRealInstance();
+               }
+
+               return $this->parent;
+       }
+
+       /**
+        * Sets the parent category.
+        *
+        * @param Tx_Extbase_Domain_Model_Category $parent the parent category
+        * @return void
+        * @api
+        */
+       public function setParent(Tx_Extbase_Domain_Model_Category $parent) {
+               $this->parent = $parent;
+       }
+
+       /**
+        * Returns the items within this category.
+        *
+        * @return Tx_Extbase_Persistence_ObjectStorage<Tx_Extbase_DomainObject_AbstractEntity> the items, might be empty
+        * @api
+        */
+       public function getItems() {
+               return $this->items;
+       }
+
+       /**
+        * Sets the items within this category.
+        *
+        * @param Tx_Extbase_Persistence_ObjectStorage<Tx_Extbase_DomainObject_AbstractEntity> $items the items, may be empty
+        * @return void
+        * @api
+        */
+       public function setItems(Tx_Extbase_Persistence_ObjectStorage $items) {
+               $this->items = $items;
+       }
+
+       /**
+        * Adds a item to the items property
+        *
+        * @param Tx_Extbase_DomainObject_AbstractEntity $item
+        * @return void
+        * @api
+        */
+       public function addItem(Tx_Extbase_DomainObject_AbstractEntity $item) {
+               $this->items->attach($item);
+       }
+
+       /**
+        * Removes a item of the items property
+        *
+        * @param Tx_Extbase_DomainObject_AbstractEntity $item
+        * @return void
+        * @api
+        */
+       public function removeItem(Tx_Extbase_DomainObject_AbstractEntity $item) {
+               $this->items->detach($item);
+       }
+}
+?>
\ No newline at end of file
diff --git a/typo3/sysext/extbase/Classes/Domain/Repository/CategoryRepository.php b/typo3/sysext/extbase/Classes/Domain/Repository/CategoryRepository.php
new file mode 100755 (executable)
index 0000000..fd189e9
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2012 Fabien Udriot <fabien.udriot@typo3.org>
+*  All rights reserved
+*
+*  This script is part of the TYPO3 project. The TYPO3 project is
+*  free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  The GNU General Public License can be found at
+*  http://www.gnu.org/copyleft/gpl.html.
+*
+*  This script is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+
+/**
+ * Repository for Category models.
+ *
+ * @author Fabien Udriot <fabien.udriot@typo3.org>
+ * @author Oliver Klee <typo3-coding@oliverklee.de>
+ *
+ * @package Extbase
+ * @subpackage Domain\Repository
+ *
+ * @api
+ */
+class Tx_Extbase_Domain_Repository_CategoryRepository extends Tx_Extbase_Persistence_Repository {
+       /**
+        * Initializes the repository.
+        *
+        * @return void
+        */
+       public function initializeObject() {
+               /** @var $querySettings Tx_Extbase_Persistence_Typo3QuerySettings */
+               $querySettings = $this->objectManager->create('Tx_Extbase_Persistence_Typo3QuerySettings');
+               $querySettings->setRespectStoragePage(FALSE);
+               $this->setDefaultQuerySettings($querySettings);
+       }
+}
+?>
\ No newline at end of file
diff --git a/typo3/sysext/extbase/Tests/Unit/Domain/Model/CategoryTest.php b/typo3/sysext/extbase/Tests/Unit/Domain/Model/CategoryTest.php
new file mode 100644 (file)
index 0000000..1750274
--- /dev/null
@@ -0,0 +1,164 @@
+<?php
+/***************************************************************
+* Copyright notice
+*
+* (c) 2012 Oliver Klee <typo3-coding@oliverklee.de>
+* All rights reserved
+*
+* This script is part of the TYPO3 project. The TYPO3 project is
+* free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation; either version 2 of the License, or
+* (at your option) any later version.
+*
+* The GNU General Public License can be found at
+* http://www.gnu.org/copyleft/gpl.html.
+*
+* This script is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+
+/**
+ * Testcase for the Tx_Extbase_Domain_Model_Category class.
+ *
+ * @package Extbase
+ * @subpackage Domain\Model
+ *
+ * @author Oliver Klee <typo3-coding@oliverklee.de>
+ */
+class Tx_Extbase_Tests_Unit_Domain_Model_CategoryTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
+       /**
+        * @var Tx_Extbase_Domain_Model_Category
+        */
+       private $fixture = NULL;
+
+       public function setUp() {
+               $this->fixture = new Tx_Extbase_Domain_Model_Category();
+       }
+
+       public function tearDown() {
+               unset($this->fixture);
+       }
+
+       /**
+        * @test
+        */
+       public function getTitleInitiallyReturnsEmptyString() {
+               $this->assertSame(
+                       '',
+                       $this->fixture->getTitle()
+               );
+       }
+
+       /**
+        * @test
+        */
+       public function setTitleSetsTitle() {
+               $this->fixture->setTitle('foo bar');
+
+               $this->assertSame(
+                       'foo bar',
+                       $this->fixture->getTitle()
+               );
+       }
+
+       /**
+        * @test
+        */
+       public function getDescriptionInitiallyReturnsEmptyString() {
+               $this->assertSame(
+                       '',
+                       $this->fixture->getDescription()
+               );
+       }
+
+       /**
+        * @test
+        */
+       public function setDescriptionSetsDescription() {
+               $this->fixture->setDescription('foo bar');
+
+               $this->assertSame(
+                       'foo bar',
+                       $this->fixture->getDescription()
+               );
+       }
+
+       /**
+        * @test
+        */
+       public function getParentInitiallyReturnsNull() {
+               $this->assertNull(
+                       $this->fixture->getParent()
+               );
+       }
+
+       /**
+        * @test
+        */
+       public function setParentSetsParent() {
+               $parent = new Tx_Extbase_Domain_Model_Category();
+               $this->fixture->setParent($parent);
+
+               $this->assertSame(
+                       $parent,
+                       $this->fixture->getParent()
+               );
+       }
+
+       /**
+        * @test
+        */
+       public function getItemsInitiallyReturnsEmptyStorage() {
+               $this->assertEquals(
+                       new Tx_Extbase_Persistence_ObjectStorage(),
+                       $this->fixture->getItems()
+               );
+       }
+
+       /**
+        * @test
+        */
+       public function setItemsSetsItems() {
+               $items = new Tx_Extbase_Persistence_ObjectStorage();
+               $items->attach($this->getMockForAbstractClass('Tx_Extbase_DomainObject_AbstractEntity'));
+               $this->fixture->setItems($items);
+
+               $this->assertEquals(
+                       $items,
+                       $this->fixture->getItems()
+               );
+       }
+
+       /**
+        * @test
+        */
+       public function addItemAttachesItem() {
+               /** @var Tx_Extbase_DomainObject_AbstractEntity $item */
+               $item = $this->getMockForAbstractClass('Tx_Extbase_DomainObject_AbstractEntity');
+               $this->fixture->addItem($item);
+
+               $this->assertTrue(
+                       $this->fixture->getItems()->contains($item)
+               );
+       }
+
+       /**
+        * @test
+        */
+       public function removeItemDetachesItem() {
+               /** @var Tx_Extbase_DomainObject_AbstractEntity $item */
+               $item = $this->getMockForAbstractClass('Tx_Extbase_DomainObject_AbstractEntity');
+               $this->fixture->addItem($item);
+               $this->fixture->removeItem($item);
+
+               $this->assertFalse(
+                       $this->fixture->getItems()->contains($item)
+               );
+       }
+}
+?>
\ No newline at end of file
diff --git a/typo3/sysext/extbase/Tests/Unit/Domain/Repository/CategoryRepositoryTest.php b/typo3/sysext/extbase/Tests/Unit/Domain/Repository/CategoryRepositoryTest.php
new file mode 100644 (file)
index 0000000..5e55716
--- /dev/null
@@ -0,0 +1,65 @@
+<?php
+/***************************************************************
+ * Copyright notice
+ *
+ * (c) 2012 Oliver Klee <typo3-coding@oliverklee.de>
+ * All rights reserved
+ *
+ * This script is part of the TYPO3 project. The TYPO3 project is
+ * free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * The GNU General Public License can be found at
+ * http://www.gnu.org/copyleft/gpl.html.
+ *
+ * This script is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+/**
+ * Testcase for the Tx_Extbase_Domain_Repository_CategoryRepository class.
+ *
+ * @package Extbase
+ * @subpackage Domain\Repository
+ *
+ * @author Oliver Klee <typo3-coding@oliverklee.de>
+ */
+class Tx_Extbase_Tests_Unit_Domain_Repository_CategoryRepositoryTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
+       /**
+        * @var Tx_Extbase_Domain_Repository_CategoryRepository
+        */
+       private $fixture = NULL;
+
+       protected function setUp() {
+               $this->fixture = new Tx_Extbase_Domain_Repository_CategoryRepository(
+                       $this->getMock('Tx_Extbase_Object_ObjectManagerInterface')
+               );
+       }
+
+       protected function tearDown() {
+               unset($this->fixture);
+       }
+
+       /**
+        * @test
+        */
+       public function initializeObjectSetsRespectStoragePidToFalse() {
+               /** @var $objectManager Tx_Extbase_Object_ObjectManagerInterface */
+               $objectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
+               $fixture = new Tx_Extbase_Domain_Repository_CategoryRepository($objectManager);
+
+               $querySettings = $this->getMock('Tx_Extbase_Persistence_Typo3QuerySettings');
+               $querySettings->expects($this->once())->method('setRespectStoragePage')->with(FALSE);
+               $objectManager->expects($this->once())->method('create')
+                       ->with('Tx_Extbase_Persistence_Typo3QuerySettings')->will($this->returnValue($querySettings));
+
+               $fixture->initializeObject();
+       }
+}
+?>
\ No newline at end of file