Testing with PHPUnit
phpunit
aka vendor/bin/phpunit
phpunit --version
phpunit --testsuite testsuitename
phpunit --filter myTestMethod
phpunit --group groupname
phpunit --coverage-html tests/coverage
External lib. include path (OS X & Mamp): /Applications/MAMP/Library/bin
/usr/local/src/composer/composer.json
sudo composer update
bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
public
and a method name starting with test
or have the @test
notationNULL
)private
method not starting with test
$this->markTestSkipped( 'Must be revisited.' )
@covers
notation$this->withoutExceptionHandling()
$this->actingAs(factory('App\User')->create());
Given App\Project
class:
class Project {
public function subscribeTo($name, $user) {
//
}
}
// Create user
$user = factory(User::class)->create();
// Create mock
$this->mock(Project::class, function ($mock) use ($user) {
$mock->shouldReceive()->subscribeTo('members', $user)->once();
});
// Perform action
$this->actingAs($user)->get('/endpoint');
// Create user
$user = factory(User::class)->create();
// Create spy
$spy = $this->spy(Project::class);
// Perform action
$this->actingAs($user)->get('/endpoint');
// Confirm action
$spy->shouldHaveReceived()->subscribeTo('members', $user);
# PHPUnit completion
source ~/phpunit.bash
# Bash-Completion script for PHPUnit
#
# Created by Henrique Moody <henriquemoody@gmail.com>
# https://gist.github.com/henriquemoody/5014805
#
_phpunit()
{
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local opts="--coverage-clover --coverage-crap4j --coverage-html --coverage-php --coverage-text --coverage-xml --log-junit --log-tap --log-json --testdox-html --testdox-text --filter --testsuite --group --exclude-group --list-groups --test-suffix --report-useless-tests --strict-coverage --disallow-test-output --enforce-time-limit --disallow-todo-tests --process-isolation --no-globals-backup --static-backup --colors --columns --columns --stderr --stop-on-error --stop-on-failure --stop-on-risky --stop-on-skipped --stop-on-incomplete -v --verbose --debug --loader --repeat --tap --testdox --printer --bootstrap -c --configuration --no-configuration --include-path -d -h --help --version"
local diropts="--coverage-html|--coverage-xml|--include-path"
local nocompleteopts="--filter|--testsuite|--group|--exclude-group|--test-suffix|--loader|--repeat|--printer|-d|-h|--help|--version"
if [[ ${prev} =~ ${diropts} ]]; then
COMPREPLY=( $(compgen -d -- ${cur}) )
return 0
elif [[ ${prev} =~ ${nocompleteopts} ]]; then
return 0
elif [[ ${prev} = --columns ]]; then
COMPREPLY=( $(compgen -W "max" -- ${cur}) )
return 0
elif [[ ${prev} = --colors ]]; then
COMPREPLY=( $(compgen -W "auto never always" -- ${cur}) )
return 0
elif [[ "${cur}" == -* ]]; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
else
COMPREPLY=( $(compgen -f -- "${cur}") )
return 0
fi
}
complete -F _phpunit phpunit
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="./tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<php>
<const name="PluginNamespace\Tests\DB_NAME" value="PLACEHOLDER"/>
<const name="PluginNamespace\Tests\DB_USER" value="PLACEHOLDER"/>
<const name="PluginNamespace\Tests\DB_PASSWORD" value="PLACEHOLDER"/>
<const name="PluginNamespace\Tests\DB_HOST" value="localhost"/>
<const name="PluginNamespace\Tests\DB_CHARSET" value="utf8"/>
<const name="PluginNamespace\Tests\DB_COLLATE" value=""/>
<const name="PluginNamespace\Tests\DB_TABLE_PREFIX" value="wp_"/>
</php>
<testsuites>
<testsuite name="Integration">
<directory suffix="Test.php" phpVersion="5.6.0" phpVersionOperator=">=">./tests/Integration</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<filter>
<whitelist>
<directory>./src</directory>
<exclude>
<directory>./assets</directory>
<directory>./languages</directory>
<directory>./resources</directory>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="NunoMaduro\Collision\Adapters\Phpunit\Listener" />
</listeners>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="MAIL_DRIVER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>