From cf0036babb29fad4a77ba8976626f72dda68235b Mon Sep 17 00:00:00 2001 From: Florent Date: Mon, 8 Jul 2013 09:50:49 +0200 Subject: [PATCH] using exception instead of null return (preventing potential NPE) --- DataMapper/UserMapper.php | 2 +- Tests/DataMapper/UserMapperTest.php | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/DataMapper/UserMapper.php b/DataMapper/UserMapper.php index 4bd8ce1..22ce20d 100644 --- a/DataMapper/UserMapper.php +++ b/DataMapper/UserMapper.php @@ -70,7 +70,7 @@ class UserMapper { $result = $this->_adapter->find($id); if (0 == count($result)) { - return; + throw new \InvalidArgumentException("User #$id not found"); } $row = $result->current(); diff --git a/Tests/DataMapper/UserMapperTest.php b/Tests/DataMapper/UserMapperTest.php index 9bcfa65..15b0459 100644 --- a/Tests/DataMapper/UserMapperTest.php +++ b/Tests/DataMapper/UserMapperTest.php @@ -87,4 +87,18 @@ class UserMapperTest extends \PHPUnit_Framework_TestCase $this->assertEquals([$existing], $user); } -} \ No newline at end of file + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage User #404 not found + */ + public function testNotFound() + { + $this->dbal->expects($this->once()) + ->method('find') + ->with(404) + ->will($this->returnValue([])); + + $user = $this->mapper->findById(404); + } + +}