Skip to content

mEsUsah/craft-phpunit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 

Repository files navigation

PHPUnit setup for Craft CMS

A simple PHPUnit setup for Craft CMS.

Setup:

Run this one-liner in your project root to add the basic files and directories:

  • NB! If you use DDev, you must run the script inside the container.
wget -O - https://raw.githubusercontent.com/mEsUsah/craft-phpunit/refs/heads/master/setup.sh | bash

For a more in-depth desription on the setup and how to get started with testing Craft CMS with PHPUnit, read this article on my blog.

Example tests:

This repo contains some example tests, that can be used for inspiration.

Modify Example

If you run Craft Solo, you must change line 10 in tests/Unit/ExampleUnitTest.php to:

$this->assertEquals(CmsEdition::Solo, Craft::$app->edition);

Run tests

Run the tests with this command:

php ./vendor/bin/phpunit

Utils

If you need to use the utility classes in this repo, you must add the "Tests" root namespace to your composer.json

{
    ...
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    ...
}

Once added, you must run composer update, to make it work.

composer update

ClearCache utility

You can then use the the ClearChache utility class.

<?php
namespace Tests\Feature;

use PHPUnit\Framework\TestCase;
use Tests\utils\ClearCache;

final class ExampleTest extends TestCase
{    
    public function test_can_clear_cache(): void
    {
        ClearCache::run();
        $this->assertTrue(true);
    }
}

About

Basic setup of PHPUnit for Craft CMS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published