Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to customize the __toString representation of a CallbackToken #573

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions spec/Prophecy/Argument/Token/CallbackTokenSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ function its_string_representation_should_tell_that_its_callback()
{
$this->__toString()->shouldReturn('callback()');
}

function its_string_representation_can_be_customized()
{
$this->beConstructedWith('get_class', 'MyCustomTestCase');

$this->__toString()->shouldReturn('MyCustomTestCase');
}
}
7 changes: 7 additions & 0 deletions spec/Prophecy/ArgumentSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ function it_has_a_shortcut_for_callback_token()
$token->shouldBeAnInstanceOf('Prophecy\Argument\Token\CallbackToken');
}

function it_supports_customizing_tostring_representation_for_callback_token()
{
$token = $this->that('get_class', 'MyCustomTestCase');
$token->shouldBeAnInstanceOf('Prophecy\Argument\Token\CallbackToken');
$token->__toString()->shouldReturn('MyCustomTestCase');
}

function it_has_a_shortcut_for_object_state_token()
{
$token = $this->which('getName', 'everzet');
Expand Down
5 changes: 3 additions & 2 deletions src/Prophecy/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ public static function which($methodName, $value)
* Checks that argument matches provided callback.
*
* @param callable $callback
* @param string|null $customStringRepresentation Customize the __toString() representation of this token
*
* @return Token\CallbackToken
*/
public static function that($callback)
public static function that($callback, ?string $customStringRepresentation = null)
{
return new Token\CallbackToken($callback);
return new Token\CallbackToken($callback, $customStringRepresentation);
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/Prophecy/Argument/Token/CallbackToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ class CallbackToken implements TokenInterface
{
private $callback;

/**
* @var string|null
*/
private $customStringRepresentation;

/**
* Initializes token.
*
* @param callable $callback
* @param string|null $customStringRepresentation Customize the __toString() representation of this token
*
* @throws \Prophecy\Exception\InvalidArgumentException
*/
public function __construct($callback)
public function __construct($callback, ?string $customStringRepresentation = null)
{
if (!is_callable($callback)) {
throw new InvalidArgumentException(sprintf(
Expand All @@ -39,6 +45,7 @@ public function __construct($callback)
}

$this->callback = $callback;
$this->customStringRepresentation = $customStringRepresentation;
}

/**
Expand Down Expand Up @@ -70,6 +77,10 @@ public function isLast()
*/
public function __toString()
{
if ($this->customStringRepresentation !== null) {
return $this->customStringRepresentation;
}

return 'callback()';
}
}