I use pest to figure things out rather than tinker or tinkerwell normally so I add a bunch of unit tests to make sure my small classes are working as I build value objects and such. While running first test everything was fine, when I added a failing condition, it threw this error:
Issue: Could not bind closure.
PHP version: 8.3.2
Operating system: Darwin
Caused by
Exception: Could not bind closure.
Working:
test('it can make a word', closure: static function(): void {
expect(Word::fromText('hello'))
->text->toBe('hello')
->sum->toBe(532)
->ordered->toBe('ehllo')
->length->toBe(5);
});
Failing, I was just messing with it to show a failure by changing the letters and looking for an error on the different value as a sanity check
test('it can make a word', closure: static function(): void {
expect(Word::fromText('hello'))
->text->toBe('hello')
->sum->toBe(532)
->ordered->toBe('ehllo')
->length->toBe(5)
->and(Word::fromText('hallo'))
->text->toBe('hallo')
->sum->toBe(532)
->ordered->toBe('ahllo')
->length->toBe(5);
});
This is in a giant repository of code Kata's to play around with.
It shouldn't make a difference but here is the Word class so you can drop it in any new laravel project, there is no front end, it's just classes and tests to help work on cleaning my problem solving. Here is the word class at the moment of this writing:
<?php
namespace App\Katas\Anagram;
class Word
{
public readonly int $length;
public readonly int $sum;
public readonly string $ordered;
private function __construct(
public readonly string $text,
) {
$this->length = strlen(trim($text));
$this->sum = $this->sum($text);
$this->ordered =$this->sort($text);
}
public static function fromText(string $text): self
{
return new self(strtolower(trim($text)));
}
protected function sum(string $text): int
{
$sum = 0;
$lettes = str_split($text);
foreach($lettes as $lette){
$sum += ord($lette);
}
return $sum;
}
protected function sort(string $text)
{
$ordered =str_split($text);
sort($ordered);
return implode($ordered);
}
public function compare(Word $word): int
{
return $this->length <=> $word->length && $this->sum <=> $word->sum && $this->ordered <=> $word->ordered;
}
// Just a visual aid for me to look at on console.
public function __toString(): string {
return
'text: ' .$this->text .
', length:' .$this->length .
', sum:' .$this->sum .
', ordered:' .$this->ordered;
}
}
What Happened
I use pest to figure things out rather than tinker or tinkerwell normally so I add a bunch of unit tests to make sure my small classes are working as I build value objects and such. While running first test everything was fine, when I added a failing condition, it threw this error:
Pest\Exceptions\ShouldNotHappen: This should not happen - please create an new issue here: https://github.com/pestphp/pest.
Issue: Could not bind closure. PHP version: 8.3.2 Operating system: Darwin
Caused by Exception: Could not bind closure.
Working:
Failing, I was just messing with it to show a failure by changing the letters and looking for an error on the different value as a sanity check
This is in a giant repository of code Kata's to play around with.
It shouldn't make a difference but here is the Word class so you can drop it in any new laravel project, there is no front end, it's just classes and tests to help work on cleaning my problem solving. Here is the word class at the moment of this writing:
How to Reproduce
Sample Repository
No response
Pest Version
2.34.0
PHP Version
8.3.2
Operation System
macOS
Notes
No response