Skip to content

Commit

Permalink
Merge pull request #39 from tattersoftware/test
Browse files Browse the repository at this point in the history
Bundle Testing
  • Loading branch information
MGatner authored Dec 14, 2021
2 parents d0099ce + 44bcfdf commit 2da9c43
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 15 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,11 @@ public $routes = [
```

And we have hands-free Bootstrap updates from now on!

## Testing

This library includes some PHPUnit extension classes in **src/Test/** to assist with testing
Assets and Bundles. These are used to test the files from this library but are also available
for your own libraries and projects to use. Simply extend the appropriate test case and add
a data provider method with your class name and criteria to meet. See the test files in
**tests/** for examples.
57 changes: 57 additions & 0 deletions src/Test/BundlesTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Tatter\Assets\Test;

use CodeIgniter\Publisher\Publisher;
use Tatter\Assets\Bundle;

abstract class BundlesTestCase extends TestCase
{
private $didPublish = false;

/**
* Publishes all files once so they are
* available for bundles.
*/
protected function setUp(): void
{
parent::setUp();

// Make sure everything is published
if (! $this->didPublish) {
foreach (Publisher::discover() as $publisher) {
$publisher->publish(); // @codeCoverageIgnore
}

$this->didPublish = true;
}
}

/**
* @dataProvider bundleProvider
*
* @param class-string<Bundle> $class
* @param string[] $expectedHeadFiles
* @param string[] $expectedBodyFiles
*/
public function testBundlesFiles(string $class, array $expectedHeadFiles, array $expectedBodyFiles): void
{
$bundle = new $class();
$head = $bundle->head();
$body = $bundle->body();

foreach ($expectedHeadFiles as $file) {
$this->assertStringContainsString($file, $head);
}

foreach ($expectedBodyFiles as $file) {
$this->assertStringContainsString($file, $body);
}
}

/**
* Returns an array of items to test with each item
* as a triple of [string bundleClassName, string[] headFileNames, string[] bodyFileNames]
*/
abstract public function bundleProvider(): array;
}
52 changes: 52 additions & 0 deletions src/Test/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Tatter\Assets\Test;

use CodeIgniter\Test\CIUnitTestCase;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use Tatter\Assets\Asset;
use Tatter\Assets\Config\Assets as AssetsConfig;

abstract class TestCase extends CIUnitTestCase
{
/**
* Virtual workspace
*
* @var vfsStreamDirectory
*/
protected $root;

/**
* @var AssetsConfig
*/
protected $config;

/**
* Preps the config and VFS.
*/
protected function setUp(): void
{
parent::setUp();

$this->root = vfsStream::setup('root');

// Create the config
$this->config = config('Assets');
$this->config->directory = $this->root->url() . DIRECTORY_SEPARATOR;
$this->config->useTimestamps = false; // These make testing much harder

Asset::useConfig($this->config);

// Add VFS as an allowed Publisher directory
config('Publisher')->restrictions[$this->config->directory] = '*';
}

protected function tearDown(): void
{
parent::tearDown();

$this->root = null; // @phpstan-ignore-line
$this->resetServices();
}
}
36 changes: 36 additions & 0 deletions tests/BundlesTestCaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Tatter\Assets\Test\BundlesTestCase;
use Tests\Support\Bundles\FruitSalad;

/**
* @internal
*/
final class BundlesTestCaseTest extends BundlesTestCase
{
/**
* Mocks publishing the bundle content.
*/
protected function setUp(): void
{
parent::setUp();

copy(SUPPORTPATH . 'Files/apple.css', $this->config->directory . 'apple.css');
copy(SUPPORTPATH . 'Files/banana.js', $this->config->directory . 'banana.js');
}

public function bundleProvider(): array
{
return [
[
FruitSalad::class,
[
'apple.css',
],
[
'banana.js',
],
],
];
}
}
19 changes: 4 additions & 15 deletions tests/_support/AssetsTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,18 @@

namespace Tests\Support;

use CodeIgniter\Test\CIUnitTestCase;
use Tatter\Assets\Asset;
use Tatter\Assets\Config\Assets as AssetsConfig;
use Tatter\Assets\Test\TestCase;

abstract class AssetsTestCase extends CIUnitTestCase
abstract class AssetsTestCase extends TestCase
{
/**
* @var AssetsConfig
*/
protected $config;

/**
* Preps the config for the test directory.
*/
protected function setUp(): void
{
parent::setUp();

$this->config = config(AssetsConfig::class);
$this->config->directory = SUPPORTPATH . 'Files/';
$this->config->vendor = 'external/';
$this->config->useTimestamps = false; // These make testing much harder

Asset::useConfig($this->config);
$this->config->directory = SUPPORTPATH . 'Files/';
$this->config->vendor = 'external/';
}
}

0 comments on commit 2da9c43

Please sign in to comment.