} elseif (substr($methodName, 0, 9) === 'findOneBy' && strlen($methodName) > 10) {
$propertyName = lcfirst(substr($methodName, 9));
$query = $this->createQuery();
- $object = $query->matching($query->equals($propertyName, $arguments[0]))->setLimit(1)->execute()->getFirst();
- return $object;
+
+ $result = $query->matching($query->equals($propertyName, $arguments[0]))->setLimit(1)->execute();
+ if ($result instanceof \TYPO3\CMS\Extbase\Persistence\QueryResultInterface) {
+ return $result->getFirst();
+ } elseif (is_array($result)) {
+ return isset($result[0]) ? $result[0] : NULL;
+ }
+
} elseif (substr($methodName, 0, 7) === 'countBy' && strlen($methodName) > 8) {
$propertyName = lcfirst(substr($methodName, 7));
$query = $this->createQuery();
$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
*/