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

upgrade tests to new exception syntax #3309

Closed
wants to merge 1 commit into from
Closed

upgrade tests to new exception syntax #3309

wants to merge 1 commit into from

Conversation

TomasVotruba
Copy link

No description provided.

\ini_set('xdebug.scream', '1');

throw new \Exception('Screaming preg_match');
}

/**
* @expectedException \Exception variadic
Copy link
Author

Choose a reason for hiding this comment

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

Why was there "variadic", a typo?

Copy link
Contributor

Choose a reason for hiding this comment

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

no no no no no

this annotation supports one and two parameters, one of them is exception class, second, optional, is exception message.

this docblock is equivalent to

/**
 * @expectedException \Exception 
 * @expectedExceptionMessage variadic
 * @expectedExceptionMessageRegExp /^A variadic \w+ message/
 */

Copy link
Author

Choose a reason for hiding this comment

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

I see, thanks for sharing. Is there somewhere documented?

So correct replacement would be this?

$this->expectException('Exception');
$this->expectExceptionMessage('variadic');

Copy link
Contributor

Choose a reason for hiding this comment

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

no, you shall still respect expectedExceptionMessageRegExp
expectedExceptionMessage is not causing === "variadic", but /.*variadic.*/ under the hood

probably it is, but i was not using documentation for so long, i won't point it

Copy link
Author

Choose a reason for hiding this comment

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

In the comment above you mix various rules then. So it's like this?

$this->expectException('Exception');
$this->expectExceptionMessageRegExp('#variadic#');

Copy link
Contributor

Choose a reason for hiding this comment

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

@sebastianbergmann , I believe that I can elaborate on my answer, so maybe you won't need to spend time on it. Of course, if you want to step in, do it ;)

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm sad you don't want to believe me @TomasVotruba nor check it on your own. I'm using this great project for so long and that intensive, I got chance to understood it and know few aspects of it that can be considered tricky, like the one you faced here ;)

To sum up:

  1. one can apply that diff, both versions, before and after patch, mean the same
 /**
- * @expectedException \Exception variadic
+ * @expectedException \Exception
+ * @expectedExceptionMessage variadic
  * @expectedExceptionMessageRegExp /^A variadic \w+ message/
  */
  1. both, message and messageRegExp expectations are respected and executed
  2. message foo is in fact .*foo.* regex (but you can't provide multiple regexes annotations!) (disclaimer - yes, I would prefer personally if that would not happen, yet it does)
  3. to replace annotations to code, we would have:
$this->expectException('Exception');
$this->expectExceptionMessage('variadic');
$this->expectExceptionMessageRegExp('/^A variadic \w+ message/');

Copy link
Contributor

Choose a reason for hiding this comment

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

As I said, I don't rely on documentation, yet that shall be better - I took 5 minutes and grab some links for you, @TomasVotruba . I hope those will help you to understood how it works, if my elaboration is not enough.

expectedException annotation regex is defined here:

private const REGEX_EXPECTED_EXCEPTION = '(@expectedException\s+([:.\w\\\\x7f-\xff]+)(?:[\t ]+(\S*))?(?:[\t ]+(\S*))?\s*$)m';

As you can see, it can defined class, message and code, eg message is handled here:
$message = \trim($matches[2]);

Ten, util is used in TestCase to set expectations:

$this->expectExceptionMessage($expectedException['message']);

And asserted here

$this->expectedExceptionMessage
by creating ExceptionMessage constraint.

So finally, we can see how message we provided in annotation is comparad to message from exception that happened:

return \strpos($other->getMessage(), $this->expectedMessage) !== false;

As we see, it's not ===

Copy link
Author

Choose a reason for hiding this comment

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

This feature is WTF to me. I was confused by your answer and wanted to know for sure.

Thanks for the code, that's what I needed to be able to implement it correctly.

Copy link
Contributor

Choose a reason for hiding this comment

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

as I said, there are some quirks and tricky parts, yet for most use cases they do the job and normally, person doesn't need to think about it.

glad to help ;)

@sebastianbergmann
Copy link
Owner

Are you sure that none of these tests in fact test that the annotation for expecting exceptions works?

@TomasVotruba
Copy link
Author

There should be some I guess. It's not clear from class/method naming to me.
Maybe have "ExpectedExceptionAnnotationTest" with explicit comment?

@keradus
Copy link
Contributor

keradus commented Sep 21, 2018

without having explicit tests, I would suggest to not drop those annotations for now

@TomasVotruba
Copy link
Author

@sebastianbergmann What do you think about explicit test case names?

@TomasVotruba
Copy link
Author

Closing as unclear what to do to close it and not such a big deal atm.

@keradus Thanks for examples again.

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

Successfully merging this pull request may close these issues.

3 participants