Skip to content

Adding Test for urlFor#1880

Closed
danielgsims wants to merge 1 commit intoslimphp:3.xfrom
danielgsims:router-test
Closed

Adding Test for urlFor#1880
danielgsims wants to merge 1 commit intoslimphp:3.xfrom
danielgsims:router-test

Conversation

@danielgsims
Copy link
Copy Markdown

  • Tested that urlFor triggers deprecation error
  • Tested that urlFor aliases pathFor

  * Tested that urlFor triggers deprecation error
  * Tested that urlFor aliases pathFor
@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.2%) to 93.789% when pulling 5649a78 on danielgsims:router-test into 794073a on slimphp:3.x.

@danielgsims
Copy link
Copy Markdown
Author

Testing this message gives us full coverage on the Router. I wasn't sure how others would feel by using a partial mock for testing the map urlFor -> pathFor. I thought it was okay because we don't care about that functionality, because it's tested in pathFor, we just want to make sure the alias works. I'd appreciate any feedback if anyone disagrees.

@danielgsims
Copy link
Copy Markdown
Author

@akrabat Do you know anything about the HVVM Travis build? It looks like the error was converted into an exception, so the test goes on to run the subsequent code which shouldn't run. Any ideas?

@dopesong
Copy link
Copy Markdown
Contributor

For proper test you should expect for error, not to suppress it.

Example how to do so:

<?php
class MyTest extends PHPUnit_Framework_TestCase
{
    private $errors;

    protected function setUp() {
        $this->errors = array();
        set_error_handler(array($this, "errorHandler"));
    }

    public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) {
        $this->errors[] = compact("errno", "errstr", "errfile",
            "errline", "errcontext");
    }

    public function assertError($errstr, $errno) {
        foreach ($this->errors as $error) {
            if ($error["errstr"] === $errstr
                && $error["errno"] === $errno) {
                return;
            }
        }
        $this->fail("Error with level " . $errno .
            " and message '" . $errstr . "' not found in ", 
            var_export($this->errors, TRUE));
    }

    public function testDoStuff() {
        // execute code that triggers a warning
        $this->assertError("Message for the expected error",
            E_USER_WARNING);
    }
}

@danielgsims
Copy link
Copy Markdown
Author

@dopesong - Thanks for the feedback!

To address the '@' - There were two tests, one that tests whether the error is triggered, and another to test the functionality afterwards. I think the suppression operator was necessary because PHPUnit will convert the error to an exception through PHPUnit, right?

Also, with that strategy, how does PHPUnit keep that error handler attached? Just for the life of the one test case, the file, or the whole shebang? Do we need to use restore_error_handler in there?

@dopesong
Copy link
Copy Markdown
Contributor

With that strategy you can check if exact error triggered. Also you are correct that error handler must be restored, because set_error_handler is global.

Better to expect for error and value returned than suppress it.

@danielgsims
Copy link
Copy Markdown
Author

I made some changes based on @dopesong's feedback. Closing, please see #1882

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