Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Exception/NotCallableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static function fromInvalidCallable($value, bool $containerEntry = false)
{
if (is_object($value)) {
$message = sprintf('Instance of %s is not a callable', get_class($value));
} elseif (is_array($value) && isset($value[0], $value[1])) {
} elseif (is_array($value) && array_key_exists(0, $value) && array_key_exists(1, $value)) {
$class = is_object($value[0]) ? get_class($value[0]) : $value[0];

$extra = method_exists($class, '__call') || method_exists($class, '__callStatic')
Expand All @@ -23,9 +23,9 @@ public static function fromInvalidCallable($value, bool $containerEntry = false)

$message = sprintf('%s::%s() is not a callable.%s', $class, $value[1], $extra);
} elseif ($containerEntry) {
$message = var_export($value, true) . ' is neither a callable nor a valid container entry';
$message = print_r($value, true) . ' is neither a callable nor a valid container entry';
} else {
$message = var_export($value, true) . ' is not a callable';
$message = print_r($value, true) . ' is not a callable';
}

return new self($message);
Expand Down
65 changes: 65 additions & 0 deletions tests/Exception/NotCallableExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php declare(strict_types=1);

namespace Invoker\Test\Exception;

use Invoker\Exception\NotCallableException;
use PHPUnit\Framework\TestCase;
use stdClass;

class NotCallableExceptionTest extends TestCase
{
/**
* @test
*/
#[\PHPUnit\Framework\Attributes\Test]
public function handles_object_with_circular_reference()
{
$obj = new stdClass();
$obj->self = $obj;

$exception = NotCallableException::fromInvalidCallable($obj);

$this->assertSame('Instance of stdClass is not a callable', $exception->getMessage());
}

/**
* @test
*/
#[\PHPUnit\Framework\Attributes\Test]
public function handles_array_with_object_and_null_method()
{
$obj = new stdClass();

$exception = NotCallableException::fromInvalidCallable([$obj, null]);

$this->assertSame('stdClass::() is not a callable.', $exception->getMessage());
}

/**
* @test
*/
#[\PHPUnit\Framework\Attributes\Test]
public function handles_circular_array_reference()
{
$a = [];
$a[] = &$a;

$exception = NotCallableException::fromInvalidCallable($a);

$this->assertStringContainsString('is not a callable', $exception->getMessage());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not assert on the entire message? (that we we know what the exception message will be, here I'm not sure what it will be)

}

/**
* @test
*/
#[\PHPUnit\Framework\Attributes\Test]
public function handles_circular_array_reference_as_container_entry()
{
$a = [];
$a[] = &$a;

$exception = NotCallableException::fromInvalidCallable($a, true);

$this->assertStringContainsString('is neither a callable nor a valid container entry', $exception->getMessage());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here

}
}
Loading