[BUGFIX] Added handling for array in findOneByXXX result
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / RepositoryTest.php
index 1e4b95e..be0626d 100644 (file)
@@ -376,6 +376,34 @@ class RepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
                $this->assertSame($object, $this->repository->findOneByFoo('bar'));
        }
 
+       /**
+        * @test
+        */
+       public function magicCallMethodReturnsFirstArrayKeyInFindOneBySomethingIfQueryReturnsRawResult() {
+               $queryResultArray = array(
+                       0 => array(
+                               'foo' => 'bar',
+                       ),
+               );
+               $this->mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
+               $this->mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($this->mockQuery));
+               $this->mockQuery->expects($this->once())->method('setLimit')->with(1)->will($this->returnValue($this->mockQuery));
+               $this->mockQuery->expects($this->once())->method('execute')->will($this->returnValue($queryResultArray));
+               $this->assertSame(array('foo' => 'bar'), $this->repository->findOneByFoo('bar'));
+       }
+
+       /**
+        * @test
+        */
+       public function magicCallMethodReturnsNullInFindOneBySomethingIfQueryReturnsEmptyRawResult() {
+               $queryResultArray = array();
+               $this->mockQuery->expects($this->once())->method('equals')->with('foo', 'bar')->will($this->returnValue('matchCriteria'));
+               $this->mockQuery->expects($this->once())->method('matching')->with('matchCriteria')->will($this->returnValue($this->mockQuery));
+               $this->mockQuery->expects($this->once())->method('setLimit')->with(1)->will($this->returnValue($this->mockQuery));
+               $this->mockQuery->expects($this->once())->method('execute')->will($this->returnValue($queryResultArray));
+               $this->assertNull($this->repository->findOneByFoo('bar'));
+       }
+
        /**
         * @test
         */