pestphp / pest

Pest is an elegant PHP testing Framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.
https://pestphp.com
MIT License
9.07k stars 315 forks source link

[Bug]: IDE asked me to create an issue from the following test #1098

Closed roni-estein closed 4 months ago

roni-estein commented 4 months ago

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:


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;
    }

}

How to Reproduce

  1. Install a fresh laravel app, copy in the class.
  2. Adjust any namespaces you need
  3. Copy the tests to a pest test
  4. Run

Sample Repository

No response

Pest Version

2.34.0

PHP Version

8.3.2

Operation System

macOS

Notes

No response

roni-estein commented 4 months ago

sorry how embarrassing, EA plugin somehow added static to the test closure which caused the whole issue