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

assertSessionHas assertions don't work correctly when using PHPUnit 9 #32541

Closed
SjorsO opened this issue Apr 25, 2020 · 2 comments
Closed

assertSessionHas assertions don't work correctly when using PHPUnit 9 #32541

SjorsO opened this issue Apr 25, 2020 · 2 comments
Labels

Comments

@SjorsO
Copy link
Contributor

SjorsO commented Apr 25, 2020

  • Laravel Version: 7.8.1
  • PHP Version: 7.4.4
  • Database Driver & Version: Mysql 5.7
  • Session driver: array

Description:

After a updating PHPUnit from 8 to 9 some of my tests started failing, for example:

$this->userLogin()
    ->postBuy('something invalid')
    ->assertStatus(302)
    ->assertSessionHasErrors(['invalidAmount' => true]);

After some debugging, this seems to be the culprit:

public function assertSessionHasErrors($keys = [], $format = null, $errorBag = 'default')
{
    $this->assertSessionHas('errors');

    $keys = (array) $keys;

    $errors = $this->session()->get('errors')->getBag($errorBag);
    dump($errors);
    foreach ($keys as $key => $value) {
        // $value === "1"
        dump($errors->get($key, $format));
        
        // This assertion fails when using PHPUnit 9
        // PHPUnit::assertContains(true, [0 => "1"]);
        PHPUnit::assertContains($value, $errors->get($key, $format));
    }

The result of the first dump is:

// dump($errors);

Illuminate\Support\MessageBag^ {#4970
  #messages: array:2 [
    "oops" => array:1 [
      0 => true
    ]
    "invalidAmount" => array:1 [
      0 => true
    ]
  ]
  #format: ":message"
}

The result of the second dump is:

// $key === 'invalidAmount'
// dump($errors->get($key, $format));

array:1 [
  0 => "1"
]

Maybe the real issue here is that [0 => true] gets turned into [0 => "1"] when getting it from the error bag.

@SjorsO SjorsO changed the title Breaking change in $this->session()->get('errors')->all() Breaking change in getting errors from the ErrorBag Apr 25, 2020
@SjorsO SjorsO changed the title Breaking change in getting errors from the ErrorBag Breaking change in getting errors from the ErrorBag (or maybe due to PHPUnit 9?) Apr 25, 2020
@SjorsO SjorsO changed the title Breaking change in getting errors from the ErrorBag (or maybe due to PHPUnit 9?) Breaking change when using SessionHas* assertions when using PHPUnit 9 Apr 25, 2020
@SjorsO SjorsO changed the title Breaking change when using SessionHas* assertions when using PHPUnit 9 assertSessionHas assertions don't work correctly when using PHPUnit 9 Apr 25, 2020
@driesvints
Copy link
Member

Maybe the real issue here is that [0 => true] gets turned into [0 => "1"] when getting it from the error bag.

This is actually correct because message placeholders are populated with the values from the error bag. Because they're strings all booleans are converted to "1"s and zeros respectively.

This bug is apparently caused by sebastianbergmann/phpunit#3426. I'll try to send in a PR to workaround this.

@driesvints
Copy link
Member

Sent in a PR: #32555

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants