[BUGFIX] Replace deprecated calls in Repository
authorMarc Bastian Heinrichs <typo3@mbh-software.de>
Fri, 12 Oct 2012 12:51:38 +0000 (14:51 +0200)
committerHelmut Hummel <helmut.hummel@typo3.org>
Sun, 14 Oct 2012 19:31:27 +0000 (21:31 +0200)
Repository class uses deprecated calls of persistenceManager for
getting the backend and session singletons. Replace it with dependency
injection.

Change-Id: I492d0fc3aeafd60dcae738f3f120aa1ef507205f
Fixes: #35869
Releases: 1.4, 4.7, 6.0
Reviewed-on: http://review.typo3.org/10262
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
Reviewed-by: Helmut Hummel
Tested-by: Helmut Hummel
typo3/sysext/extbase/Classes/Persistence/Repository.php
typo3/sysext/extbase/Tests/Unit/Persistence/RepositoryTest.php

index a812e0f..f0c260a 100644 (file)
@@ -47,6 +47,16 @@ class Repository implements \TYPO3\CMS\Extbase\Persistence\RepositoryInterface,
        protected $queryFactory;
 
        /**
+        * @var \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
+        */
+       protected $backend;
+
+       /**
+        * @var \TYPO3\CMS\Extbase\Persistence\Generic\Session
+        */
+       protected $session;
+
+       /**
         * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
         */
        protected $persistenceManager;
@@ -102,6 +112,27 @@ class Repository implements \TYPO3\CMS\Extbase\Persistence\RepositoryInterface,
        }
 
        /**
+        * Injects the Persistence Backend
+        *
+        * @param \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface $backend The persistence backend
+        * @return void
+        */
+       public function injectBackend(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface $backend) {
+               $this->backend = $backend;
+       }
+
+       /**
+        *
+        * Injects the Persistence Session
+        *
+        * @param \TYPO3\CMS\Extbase\Persistence\Generic\Session $session The persistence session
+        * @return void
+        */
+       public function injectSession(\TYPO3\CMS\Extbase\Persistence\Generic\Session $session) {
+                       $this->session = $session;
+       }
+
+       /**
         * @param \TYPO3\CMS\Extbase\Persistence\Generic\QueryFactory $queryFactory
         * @return void
         */
@@ -173,13 +204,11 @@ class Repository implements \TYPO3\CMS\Extbase\Persistence\RepositoryInterface,
                if (!$newObject instanceof $this->objectType) {
                        throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The new object given to replace was not of the type (' . $this->objectType . ') this repository manages.', 1248363439);
                }
-               $backend = $this->persistenceManager->getBackend();
-               $session = $this->persistenceManager->getSession();
-               $uuid = $backend->getIdentifierByObject($existingObject);
+               $uuid = $this->persistenceManager->getIdentifierByObject($existingObject);
                if ($uuid !== NULL) {
-                       $backend->replaceObject($existingObject, $newObject);
-                       $session->unregisterReconstitutedObject($existingObject);
-                       $session->registerReconstitutedObject($newObject);
+                       $this->backend->replaceObject($existingObject, $newObject);
+                       $this->session->unregisterReconstitutedObject($existingObject);
+                       $this->session->registerReconstitutedObject($newObject);
                        if ($this->removedObjects->contains($existingObject)) {
                                $this->removedObjects->detach($existingObject);
                                $this->removedObjects->attach($newObject);
index dea03dd..574ddb0 100644 (file)
@@ -46,6 +46,16 @@ class RepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
        protected $mockQueryFactory;
 
        /**
+        * @var \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
+        */
+       protected $mockBackend;
+
+       /**
+        * @var \TYPO3\CMS\Extbase\Persistence\Generic\Session
+        */
+       protected $mockSession;
+
+       /**
         * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
         */
        protected $mockPersistenceManager;
@@ -72,12 +82,16 @@ class RepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
                $this->mockQuerySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QuerySettingsInterface');
                $this->mockQuery->expects($this->any())->method('getQuerySettings')->will($this->returnValue($this->mockQuerySettings));
                $this->mockQueryFactory->expects($this->any())->method('create')->will($this->returnValue($this->mockQuery));
-               $this->mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\ManagerInterface');
+               $this->mockBackend = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\BackendInterface');
+               $this->mockSession = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Session');
+               $this->mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
                $this->mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
                $this->repository = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Repository', array('dummy'), array($this->mockObjectManager));
                $this->repository->injectIdentityMap($this->mockIdentityMap);
                $this->repository->injectQueryFactory($this->mockQueryFactory);
                $this->repository->injectPersistenceManager($this->mockPersistenceManager);
+               $this->repository->injectBackend($this->mockBackend);
+               $this->repository->injectSession($this->mockSession);
        }
 
        /**
@@ -242,12 +256,8 @@ class RepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
        public function replaceReplacesReconstitutedEntityByNewObject() {
                $existingObject = $this->getMock('TYPO3\\CMS\\Extbase\\DomainObject\\DomainObjectInterface');
                $newObject = $this->getMock('TYPO3\\CMS\\Extbase\\DomainObject\\DomainObjectInterface');
-               $mockBackend = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\BackendInterface');
-               $this->mockPersistenceManager->expects($this->once())->method('getBackend')->will($this->returnValue($mockBackend));
-               $mockBackend->expects($this->once())->method('getIdentifierByObject')->with($existingObject)->will($this->returnValue('123'));
-               $mockBackend->expects($this->once())->method('replaceObject')->with($existingObject, $newObject);
-               $mockSession = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Session');
-               $this->mockPersistenceManager->expects($this->once())->method('getSession')->will($this->returnValue($mockSession));
+               $this->mockPersistenceManager->expects($this->once())->method('getIdentifierByObject')->with($existingObject)->will($this->returnValue('123'));
+               $this->mockBackend->expects($this->once())->method('replaceObject')->with($existingObject, $newObject);
                $this->repository->_set('objectType', get_class($newObject));
                $this->repository->replace($existingObject, $newObject);
        }
@@ -266,12 +276,8 @@ class RepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
                $newObject = $this->getMock('TYPO3\\CMS\\Extbase\\DomainObject\\DomainObjectInterface');
                $removedObjects = new \SplObjectStorage();
                $removedObjects->attach($existingObject);
-               $mockBackend = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\BackendInterface');
-               $this->mockPersistenceManager->expects($this->once())->method('getBackend')->will($this->returnValue($mockBackend));
-               $mockBackend->expects($this->once())->method('getIdentifierByObject')->with($existingObject)->will($this->returnValue('123'));
-               $mockBackend->expects($this->once())->method('replaceObject')->with($existingObject, $newObject);
-               $mockSession = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Session');
-               $this->mockPersistenceManager->expects($this->once())->method('getSession')->will($this->returnValue($mockSession));
+               $this->mockPersistenceManager->expects($this->once())->method('getIdentifierByObject')->with($existingObject)->will($this->returnValue('123'));
+               $this->mockBackend->expects($this->once())->method('replaceObject')->with($existingObject, $newObject);
                $this->repository->_set('objectType', get_class($newObject));
                $this->repository->_set('removedObjects', $removedObjects);
                $this->repository->replace($existingObject, $newObject);
@@ -292,12 +298,8 @@ class RepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
                $newObject = $this->getMock('TYPO3\\CMS\\Extbase\\DomainObject\\DomainObjectInterface');
                $addedObjects = new \SplObjectStorage();
                $addedObjects->attach($existingObject);
-               $mockBackend = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\BackendInterface');
-               $this->mockPersistenceManager->expects($this->once())->method('getBackend')->will($this->returnValue($mockBackend));
-               $mockBackend->expects($this->once())->method('getIdentifierByObject')->with($existingObject)->will($this->returnValue(NULL));
-               $mockBackend->expects($this->never())->method('replaceObject');
-               $mockSession = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Session');
-               $this->mockPersistenceManager->expects($this->once())->method('getSession')->will($this->returnValue($mockSession));
+               $this->mockPersistenceManager->expects($this->once())->method('getIdentifierByObject')->with($existingObject)->will($this->returnValue(NULL));
+               $this->mockBackend->expects($this->never())->method('replaceObject');
                $this->repository->_set('objectType', get_class($newObject));
                $this->repository->_set('addedObjects', $addedObjects);
                $this->repository->replace($existingObject, $newObject);