Skip to content

Commit 2da9c43

Browse files
authored
Merge pull request #39 from tattersoftware/test
Bundle Testing
2 parents d0099ce + 44bcfdf commit 2da9c43

File tree

5 files changed

+157
-15
lines changed

5 files changed

+157
-15
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,11 @@ public $routes = [
347347
```
348348

349349
And we have hands-free Bootstrap updates from now on!
350+
351+
## Testing
352+
353+
This library includes some PHPUnit extension classes in **src/Test/** to assist with testing
354+
Assets and Bundles. These are used to test the files from this library but are also available
355+
for your own libraries and projects to use. Simply extend the appropriate test case and add
356+
a data provider method with your class name and criteria to meet. See the test files in
357+
**tests/** for examples.

src/Test/BundlesTestCase.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Tatter\Assets\Test;
4+
5+
use CodeIgniter\Publisher\Publisher;
6+
use Tatter\Assets\Bundle;
7+
8+
abstract class BundlesTestCase extends TestCase
9+
{
10+
private $didPublish = false;
11+
12+
/**
13+
* Publishes all files once so they are
14+
* available for bundles.
15+
*/
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
// Make sure everything is published
21+
if (! $this->didPublish) {
22+
foreach (Publisher::discover() as $publisher) {
23+
$publisher->publish(); // @codeCoverageIgnore
24+
}
25+
26+
$this->didPublish = true;
27+
}
28+
}
29+
30+
/**
31+
* @dataProvider bundleProvider
32+
*
33+
* @param class-string<Bundle> $class
34+
* @param string[] $expectedHeadFiles
35+
* @param string[] $expectedBodyFiles
36+
*/
37+
public function testBundlesFiles(string $class, array $expectedHeadFiles, array $expectedBodyFiles): void
38+
{
39+
$bundle = new $class();
40+
$head = $bundle->head();
41+
$body = $bundle->body();
42+
43+
foreach ($expectedHeadFiles as $file) {
44+
$this->assertStringContainsString($file, $head);
45+
}
46+
47+
foreach ($expectedBodyFiles as $file) {
48+
$this->assertStringContainsString($file, $body);
49+
}
50+
}
51+
52+
/**
53+
* Returns an array of items to test with each item
54+
* as a triple of [string bundleClassName, string[] headFileNames, string[] bodyFileNames]
55+
*/
56+
abstract public function bundleProvider(): array;
57+
}

src/Test/TestCase.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Tatter\Assets\Test;
4+
5+
use CodeIgniter\Test\CIUnitTestCase;
6+
use org\bovigo\vfs\vfsStream;
7+
use org\bovigo\vfs\vfsStreamDirectory;
8+
use Tatter\Assets\Asset;
9+
use Tatter\Assets\Config\Assets as AssetsConfig;
10+
11+
abstract class TestCase extends CIUnitTestCase
12+
{
13+
/**
14+
* Virtual workspace
15+
*
16+
* @var vfsStreamDirectory
17+
*/
18+
protected $root;
19+
20+
/**
21+
* @var AssetsConfig
22+
*/
23+
protected $config;
24+
25+
/**
26+
* Preps the config and VFS.
27+
*/
28+
protected function setUp(): void
29+
{
30+
parent::setUp();
31+
32+
$this->root = vfsStream::setup('root');
33+
34+
// Create the config
35+
$this->config = config('Assets');
36+
$this->config->directory = $this->root->url() . DIRECTORY_SEPARATOR;
37+
$this->config->useTimestamps = false; // These make testing much harder
38+
39+
Asset::useConfig($this->config);
40+
41+
// Add VFS as an allowed Publisher directory
42+
config('Publisher')->restrictions[$this->config->directory] = '*';
43+
}
44+
45+
protected function tearDown(): void
46+
{
47+
parent::tearDown();
48+
49+
$this->root = null; // @phpstan-ignore-line
50+
$this->resetServices();
51+
}
52+
}

tests/BundlesTestCaseTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Tatter\Assets\Test\BundlesTestCase;
4+
use Tests\Support\Bundles\FruitSalad;
5+
6+
/**
7+
* @internal
8+
*/
9+
final class BundlesTestCaseTest extends BundlesTestCase
10+
{
11+
/**
12+
* Mocks publishing the bundle content.
13+
*/
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
copy(SUPPORTPATH . 'Files/apple.css', $this->config->directory . 'apple.css');
19+
copy(SUPPORTPATH . 'Files/banana.js', $this->config->directory . 'banana.js');
20+
}
21+
22+
public function bundleProvider(): array
23+
{
24+
return [
25+
[
26+
FruitSalad::class,
27+
[
28+
'apple.css',
29+
],
30+
[
31+
'banana.js',
32+
],
33+
],
34+
];
35+
}
36+
}

tests/_support/AssetsTestCase.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,18 @@
22

33
namespace Tests\Support;
44

5-
use CodeIgniter\Test\CIUnitTestCase;
6-
use Tatter\Assets\Asset;
7-
use Tatter\Assets\Config\Assets as AssetsConfig;
5+
use Tatter\Assets\Test\TestCase;
86

9-
abstract class AssetsTestCase extends CIUnitTestCase
7+
abstract class AssetsTestCase extends TestCase
108
{
11-
/**
12-
* @var AssetsConfig
13-
*/
14-
protected $config;
15-
169
/**
1710
* Preps the config for the test directory.
1811
*/
1912
protected function setUp(): void
2013
{
2114
parent::setUp();
2215

23-
$this->config = config(AssetsConfig::class);
24-
$this->config->directory = SUPPORTPATH . 'Files/';
25-
$this->config->vendor = 'external/';
26-
$this->config->useTimestamps = false; // These make testing much harder
27-
28-
Asset::useConfig($this->config);
16+
$this->config->directory = SUPPORTPATH . 'Files/';
17+
$this->config->vendor = 'external/';
2918
}
3019
}

0 commit comments

Comments
 (0)