Replacing private services with mocks during tests

Use a custom services file for your tests, to allow changing dependencies as needed.

Testing code that generates warnings

Our code base has a lot of code that looks like this: <?php try { $this->doScaryThing(); } catch(Exception $e) { trigger_error("Downgraded: " . get_class($e) . ":" . $e->getMessage(), E_USER_WARNING); } Or sometimes trigger_error is used as a way to log other thigns. This makes it rather difficult to test. Thankfully PHPUnit 8.4 has the expectWarning method, that allows us to check this: <?php public function testItTriggersWarning(): void { $object = new Danger(); $this->expectWarning(); $object->doWarningThing(); } This does mean that the execution stops after the warning is triggered, so we can’t assert anything after that.

Infection 0.9 is out!

I wrote about infection a while ago, and not too long after that, 0.9.0 was released. Lets look at the new features, and how we can use them in our projects!

Automated code reviews

Sometimes a code base has specific code style rules, that aren’t easy to detect with either with a style fixer or a static analyzer. So instead of having to tell every new contributor your rules during a PR, or lay them all out in your CONTRIBUTING.md, why not automatically check them with PHPUnit. We will look at a couple of different ‘rules’ your code base could have, and how to set up the automated tests.

Test your tests are Testing

Are your tests testing? Are you assertions asserting? Let’s find out how we can test that our tests are testing, with the mutation testing framework: infection.