Skip to content

Commit

Permalink
Merge pull request #687 from davedevelopment/issue-686
Browse files Browse the repository at this point in the history
Forces the return value to null
  • Loading branch information
davedevelopment committed Jan 30, 2017
2 parents 3c90be8 + 02d5a81 commit 2d61ad4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library/Mockery/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ public function andReturnUndefined()
*/
public function andReturnNull()
{
return $this;
return $this->andReturn(null);
}

public function andReturnFalse()
Expand Down
10 changes: 10 additions & 0 deletions tests/Mockery/Fixtures/MethodWithNullableReturnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,14 @@ public function nullableClass() : ?MethodWithNullableReturnType
{
return null;
}

public function nullableInt() : ?int
{
return null;
}

public function nullableString() : ?string
{
return null;
}
}
30 changes: 30 additions & 0 deletions tests/Mockery/MockingNullableMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,34 @@ public function itShouldAllowNullableClassToBeNull()
$mock->shouldReceive('nullableClass')->andReturn(null);
$mock->nullableClass();
}

/** @test */
public function it_allows_returning_null_for_nullable_object_return_types()
{
$double= \Mockery::mock(MethodWithNullableReturnType::class);

$double->shouldReceive("nullableClass")->andReturnNull();

$this->assertEquals(null, $double->nullableClass());
}

/** @test */
public function it_allows_returning_null_for_nullable_string_return_types()
{
$double= \Mockery::mock(MethodWithNullableReturnType::class);

$double->shouldReceive("nullableString")->andReturnNull();

$this->assertEquals(null, $double->nullableString());
}

/** @test */
public function it_allows_returning_null_for_nullable_int_return_types()
{
$double= \Mockery::mock(MethodWithNullableReturnType::class);

$double->shouldReceive("nullableInt")->andReturnNull();

$this->assertEquals(null, $double->nullableInt());
}
}

0 comments on commit 2d61ad4

Please sign in to comment.