Skip to content

[PhpUnitBridge] add a triggered errors assertion helper #18880

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

Merged
merged 1 commit into from
Jun 15, 2016

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented May 25, 2016

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

In the past, we updated the test code several times that made sure that deprecation messages are triggered. I think we should move this code to a reusable class in the PhpUnitBridge to make it available everywhere and to be able to change a single method in case we need to update the logic.

@xabbuh
Copy link
Member Author

xabbuh commented May 26, 2016

@nicolas-grekas Any idea why the deps=low build is failing?

@GuilhemN
Copy link
Contributor

@xabbuh the phpunit-bridge is not required in several composer files such the one of the framework bundle (https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/composer.json).

@GuilhemN
Copy link
Contributor

Anyway the tests probably won't pass as long as this isn't merged.

*/
final class ErrorAssert
{
public static function assertDeprecationsAreTriggered($expectedMessages, callable $testCode)
Copy link
Contributor

Choose a reason for hiding this comment

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

should we use singular here? assertDeprecationIsTriggered or anything like setExpectedException ?

setExpectedDeprecation ?

Copy link
Member

Choose a reason for hiding this comment

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

Given that the method takes an array of expected messages, plural is fine.

setExpectedDeprecation is not fine for the current API. It would work if the method was register the expectation to check it later after running the test, but this is not what happens in the current API.

Copy link
Contributor

@OskarStark OskarStark May 31, 2016

Choose a reason for hiding this comment

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

Given that the method takes an array of expected messages, plural is fine.

Ok fine, but do we have many cases where we assert many deprecations?
https://github.com/symfony/symfony/pull/18880/files/b8fcced6cd5b6f324e85c232fbb853f514fe8ea3#diff-ecb644500d31b54f3fa4574298dc6156R537

setExpectedDeprecation is not fine for the current API. It would work if the method was register the expectation to check it later after running the test, but this is not what happens in the current API.

👍

@stof
Copy link
Member

stof commented May 31, 2016

@Ener-Getick we have a special phpunit launcher performing a local composer-based installation of PHPUnit (to remove unnecessary dependency which would hurt us, for instance symfony/yaml as we don't want to run tests on the version shipped in PHPUnit and we don't use the PHPUnit feature needing it). This special wrapper includes the bridge in the phpunit installation.

@stof
Copy link
Member

stof commented May 31, 2016

@xabbuh looks like the PHPUnit setup installed the 3.1.0-beta1 version of the bridge, and so does not use the updated version of the bridge: https://travis-ci.org/symfony/symfony/jobs/133035973#L578

@GuilhemN
Copy link
Contributor

GuilhemN commented Jun 1, 2016

@stof thanks for the explanation, I didn't know that.

$triggeredMessages[] = $message;
});

$testCode();
Copy link
Contributor

Choose a reason for hiding this comment

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

What about putting the entire function in a try/finally structure in case an exception is thrown ?

try {
    // ...
} finally {
   restore_error_handler();
}

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@fabpot
Copy link
Member

fabpot commented Jun 14, 2016

ping @nicolas-grekas

$triggeredMessages = array();

set_error_handler(function ($type, $message) use ($expectedType, &$triggeredMessages) {
if ($expectedType !== $type) {
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this be a bitfield?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think that that would be a good idea. I think if you really need to test for different kind of errors, you should probably rethink your code or write your own assertion code.

@xabbuh xabbuh force-pushed the error-assert branch 5 times, most recently from 6458c99 to ec0464c Compare June 14, 2016 12:09
@nicolas-grekas
Copy link
Member

👍

$triggeredMessages[] = $message;
});

$testCode();
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't it safer to use call_user_func here as well ?

Copy link
Member

Choose a reason for hiding this comment

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

Nope: no code has been written that can't be run. Having it here since the beginning will enforce this forever.

Copy link
Contributor

@GuilhemN GuilhemN Jun 14, 2016

Choose a reason for hiding this comment

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

so we won't accept other callables than closure ?

Edit: if yes, then we should change the typehint to closure

Copy link
Member Author

Choose a reason for hiding this comment

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

@Ener-Getick But that would mean to drop support for other callables that are not anonymous functions.

Copy link
Contributor

@GuilhemN GuilhemN Jun 14, 2016

Choose a reason for hiding this comment

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

@xabbuh indeed sorry, let's forget it, I thought the syntax $bar() only worked with closures...

Copy link
Member

Choose a reason for hiding this comment

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

I suggest doing $testCode($this) to ease writing tests in 5.3

Copy link
Contributor

@GuilhemN GuilhemN Jun 14, 2016

Choose a reason for hiding this comment

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

@nicolas-grekas the class is static.

Edit: or did you mean adding it to the method signature ?

Copy link
Member

Choose a reason for hiding this comment

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

I forgot it is static :)

*/
public static function assertDeprecationsAreTriggered($expectedMessages, $testCode)
{
if (!is_callable($testCode)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

useless here as will be checked in the second function anyway

Copy link
Member Author

Choose a reason for hiding this comment

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

That's true but I'd like to keep it here too for two reasons: Do not confuse users by letting them deal with exceptions that have been thrown by methods they did not call if we cannot avoid that. Secondly, mitigate the risk of breaking anything if we refactor the method that we call here.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok fine for me :)

@nicolas-grekas
Copy link
Member

Status: reviewed

@fabpot
Copy link
Member

fabpot commented Jun 15, 2016

Thank you @xabbuh.

@fabpot fabpot merged commit b5c2095 into symfony:master Jun 15, 2016
fabpot added a commit that referenced this pull request Jun 15, 2016
…r (xabbuh)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[PhpUnitBridge] add a triggered errors assertion helper

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

In the past, we updated the test code several times that made sure that deprecation messages are triggered. I think we should move this code to a reusable class in the PhpUnitBridge to make it available everywhere and to be able to change a single method in case we need to update the logic.

Commits
-------

b5c2095 add a triggered errors assertion helper
@xabbuh xabbuh deleted the error-assert branch June 15, 2016 19:52
fabpot added a commit that referenced this pull request Oct 21, 2016
…cation` (nicolas-grekas)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[PhpUnitBridge] Replace ErrorAssert by `@expectedDeprecation`

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18880
| License       | MIT
| Doc PR        | symfony/symfony-docs#7074

For 3.2, that's what the feat. freeze is for in this case. ping @xabbuh

See https://github.com/symfony/symfony/pull/20255/files?w=1

Commits
-------

c344203 [PhpUnitBridge] Drop ErrorAssert in favor of @expectedDeprecation
@fabpot fabpot mentioned this pull request Oct 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants