Mitchell McKenna - tagged with phpunit http://mitchmckenna.com/feed en-us http://blogs.law.harvard.edu/tech/rss LifePress mitchellmckenna@gmail.com "Avoiding Mockageddon" Sebastian Bergmann http://mitchmckenna.com/post/18964/avoiding-mockageddon-sebastian-bergmann

Conference talk from Sebastian Bergmann, the creator of PHPUnit, on the dangers of "overmocking" as well as how to avoid them.

]]>
Tue, 14 Jul 2020 11:00:00 -0400 http://mitchmckenna.com/post/18964/avoiding-mockageddon-sebastian-bergmann/avoiding-mockageddon-sebastian-bergmann
Writing Your Own Test Doubles http://mitchmckenna.com/post/17038/writing-your-own-test-doubles

Adam Wathan screencast on creating your own test doubles.

Once in a while I run into a situation where trying to use a mocking library hurts the readability of my test. In this screencast, I walk through writing a custom fake to clean up the test and simplify my assertions.

]]>
Wed, 21 Sep 2016 15:13:00 -0400 http://mitchmckenna.com/post/17038/writing-your-own-test-doubles/writing-your-own-test-doubles
Git Pre-commit Hook to Auto Run PHPUnit http://mitchmckenna.com/post/15960/auto-run-phpunit-git-pre-commit-hook

Never push up broken tests again. Using a pre-commit hook for git, when you type in git commit git will automatically run your unit tests to make sure nothing is broken with your recent changes.

Create a file called pre-commit in your project root at ./.git/hooks/ and put the following code inside it:

#!/usr/bin/php
<?php
printf("%sGit pre-commit hook %1\$s", PHP_EOL);
$projectName = basename(getcwd());
exec('vendor/bin/phpunit', $output, $returnCode);
if ($returnCode !== 0) {
    $minimalTestSummary = array_pop($output);
    printf("Test suite for %s failed: ", $projectName);
    printf("( %s ) %s%2\$s", $minimalTestSummary, PHP_EOL);
    return false;
}
printf("All tests for %s passed.%s%2\$s", $projectName, PHP_EOL);
return true;


The above is based on this gist but updated to use the copy of phpunit from your project's composer.

]]>
Fri, 20 Nov 2015 10:53:52 -0500 http://mitchmckenna.com/post/15960/auto-run-phpunit-git-pre-commit-hook/auto-run-phpunit-git-pre-commit-hook
PHPStorm: Auto Run PHPUnit On File Save http://mitchmckenna.com/post/15957/phpstorm-auto-run-phpunit-on-file-save

You can setup PHPStorm to run PHPUnit every time you save a unit test. By doing this you get immediate feedback if your unit tests are passing as you write them. And by running PHPUnit only against the current file you’re editing it can run really fast.

To set this up we need to create a File Watcher:

Preferences > File Watchers

Click (+) sign and select custom to open the new file watcher dialog. Add the following settings:

  • Name: PHPUnit
  • File type: PHP
  • Scope: click the to create a new one called tests and use a pattern like file:tests/*.* if tests is the folder all your unit tests are located in and call it tests or something.
  • Program: $PhpExecutable$
  • Arguments: vendor/bin/phpunit --configuration phpunit.xml.dist $FileDirRelativeToProjectRoot$/$FileName$
  • Working directory: $ProjectFileDir$

]]>
Thu, 19 Nov 2015 15:11:00 -0500 http://mitchmckenna.com/post/15957/phpstorm-auto-run-phpunit-on-file-save/phpstorm-auto-run-phpunit-on-file-save
Setting Up PHPUnit in PHPStorm http://mitchmckenna.com/post/15237/setting-up-phpunit-in-phpstorm

This is more so just notes for how I got PHPUnit running in PHPStorm for my project. There's probably a more optimal way to set this up but this works for now.

Tell PHPStorm to Use Composer's Copy of PHPUnit And Set Default Config

Preferences > PHPUnit

Docs: Loading PHPUnit with Composer.

Edit PHPUnit Configuration to use your custom config file

Run > Edit Configurations

Click the plus (+) sign and select PHPUnit.

Docs: Run/Debug Configuration: PHPUnit.

You can now right click (or use the keyboard shortcut) on files or folders and run unit tests.

Note: It may prompt you to setup the PHP Interpreter, open up terminal and type which php to find out where the php version is that your using then paste that in as the interpreter location (related docs).

Results

PHPStorm now tells how much unit test coverage you have beside each file/folder in the navigation tree and a Coverage panel should slide in from the right.

Setup PHPUnit to Run in Vagrant

You might need to run your phpunit inside Vagrant for some reason, follow this guide to do so.

]]>
Wed, 04 Mar 2015 18:57:06 -0500 http://mitchmckenna.com/post/15237/setting-up-phpunit-in-phpstorm/setting-up-phpunit-in-phpstorm
Running phpunit Tests from inside Vim ~ Even just ":!phpunit %" is a good http://mitchmckenna.com/post/13974/mitchellmckenna-rt-baileylo-running-phpunit-tests-from-inside-vim-httptcoy8z5sfhrtx-even-just-phpunit-is-a-good-tip

RT @baileylo: Running #phpunit Tests from inside Vim http://t.co/y8z5SFHrTX ~ Even just ":!phpunit %" is a good tip

]]>
Mon, 27 Jan 2014 00:36:00 -0500 http://mitchmckenna.com/post/13974/mitchellmckenna-rt-baileylo-running-phpunit-tests-from-inside-vim-httptcoy8z5sfhrtx-even-just-phpunit-is-a-good-tip/mitchellmckenna-rt-baileylo-running-phpunit-tests-from-inside-vim-httptcoy8z5sfhrtx-even-just-phpunit-is-a-good-tip