Skip to content

Commit

Permalink
[master]: Merge remote-tracking branch 'origin/PLATFORM-10982_fix_is_…
Browse files Browse the repository at this point in the history
…callable'

# Conflicts:
#	CHANGELOG.md
  • Loading branch information
Sannis committed Apr 16, 2019
2 parents 5cfdbdb + 04b18a8 commit cbdfdb2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

There are next changes:

- fix `is_callable` method which fails on malformed arrays

## v2.0.4

There are next changes:

- make more unique rewritten files paths depends on php internals


## v2.0.3

There are next changes:
Expand Down
4 changes: 4 additions & 0 deletions src/Badoo/SoftMocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,10 @@ public static function isCallable($callable)
}

if (is_array($callable) && sizeof($callable) === 2) {
if (!isset($callable[0]) || !isset($callable[1])) {
return is_callable($callable);
}

if (is_object($callable[0])) {
$class = get_class($callable[0]);
} else {
Expand Down
25 changes: 22 additions & 3 deletions tests/unit/Badoo/SoftMocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2777,16 +2777,35 @@ public function testGetDeclaringTrait()
$getDeclaringTrait->setAccessible(true);

// No traits.
$this->assertNull($getDeclaringTrait->invoke(null, ClassWithoutTraits::class, 'do_things'));
$this->assertNull($getDeclaringTrait->invoke(null, ClassWithoutTraits::class, 'doThings'));

// Direct trait usage.
/** @var \ReflectionClass $result */
$result = $getDeclaringTrait->invoke(null, ClassWithTraitB::class, 'do_things');
$result = $getDeclaringTrait->invoke(null, ClassWithTraitB::class, 'doThings');
$this->assertSame(TraitB::class, $result->getName());

// Trait in trait usage.
/** @var \ReflectionClass $result */
$result = $getDeclaringTrait->invoke(null, ClassWithTraitA::class, 'do_things');
$result = $getDeclaringTrait->invoke(null, ClassWithTraitA::class, 'doThings');
$this->assertSame(TraitB::class, $result->getName());
}

public function providerClassWithIsCallable()
{
return [
'numeric keys' => [[ClassWithIsCallable::class, 'doThings'], true],
'assoc keys' => [['x' => ClassWithIsCallable::class, 'y' => 'doThings'], false],
];
}

/**
* @dataProvider providerClassWithIsCallable
*
* @param callable $callable
* @param bool $expected_result
*/
public function testClassWithIsCallable($callable, $expected_result)
{
$this->assertSame($expected_result, ClassWithIsCallable::check($callable));
}
}
21 changes: 14 additions & 7 deletions tests/unit/Badoo/SoftMocksTestClasses.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,7 @@ trait TraitA

trait TraitB
{
public function do_things()
{
return;
}
public function doThings() {}
}

class ClassWithTraitA
Expand All @@ -291,8 +288,18 @@ class ClassWithTraitB

class ClassWithoutTraits
{
public function do_things()
public function doThings() {}
}

class ClassWithIsCallable
{
public static function doThings() {}

public static function check($callable)
{
return;
if (is_callable($callable)) {
return true;
}
return false;
}
}
}

0 comments on commit cbdfdb2

Please sign in to comment.