Skip to content

Commit b4bc529

Browse files
authored
Merge pull request #40 from tattersoftware/test-trait
Reorganize Test
2 parents 2da9c43 + a300658 commit b4bc529

File tree

6 files changed

+169
-67
lines changed

6 files changed

+169
-67
lines changed

src/Test/AssetsTestTrait.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Tatter\Assets\Test;
4+
5+
use CodeIgniter\Publisher\Publisher;
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+
/**
12+
* Asset Test Trait
13+
*
14+
* Trait to set up a VFS instance for testing
15+
* Assets, Bundles, and Publishers.
16+
*/
17+
trait AssetsTestTrait
18+
{
19+
/**
20+
* Virtual workspace
21+
*
22+
* @var vfsStreamDirectory|null
23+
*/
24+
protected $root;
25+
26+
/**
27+
* @var AssetsConfig
28+
*/
29+
protected $config;
30+
31+
/**
32+
* Whether the publishers have been run.
33+
*
34+
* @var bool
35+
*/
36+
private $published = false;
37+
38+
/**
39+
* Creates the VFS (if necessary) and updates the Assets config.
40+
*/
41+
protected function setUpAssetsTestTrait(): void
42+
{
43+
if ($this->root === null) {
44+
$this->root = vfsStream::setup('root');
45+
}
46+
47+
// Create the config
48+
$this->config = config('Assets');
49+
$this->config->directory = $this->root->url() . DIRECTORY_SEPARATOR;
50+
$this->config->useTimestamps = false; // These make testing much harder
51+
52+
Asset::useConfig($this->config);
53+
54+
// Add VFS as an allowed Publisher directory
55+
config('Publisher')->restrictions[$this->config->directory] = '*';
56+
}
57+
58+
/**
59+
* Resets the VFS if $refreshVfs is truthy.
60+
*/
61+
protected function tearDownAssetsTestTrait(): void
62+
{
63+
if (! empty($this->refreshVfs)) {
64+
$this->root = null;
65+
$this->published = false;
66+
}
67+
}
68+
69+
/**
70+
* Publishes all files once so they are
71+
* available for bundles.
72+
*/
73+
protected function publishAll(): void
74+
{
75+
if ($this->published) {
76+
return;
77+
}
78+
79+
foreach (Publisher::discover() as $publisher) {
80+
$publisher->publish();
81+
}
82+
83+
$this->published = true;
84+
}
85+
}

src/Test/BundlesTestCase.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Tatter\Assets\Test;
44

5-
use CodeIgniter\Publisher\Publisher;
5+
use CodeIgniter\Test\CIUnitTestCase;
66
use Tatter\Assets\Bundle;
77

8-
abstract class BundlesTestCase extends TestCase
8+
abstract class BundlesTestCase extends CIUnitTestCase
99
{
10-
private $didPublish = false;
10+
use AssetsTestTrait;
1111

1212
/**
1313
* Publishes all files once so they are
@@ -17,14 +17,7 @@ protected function setUp(): void
1717
{
1818
parent::setUp();
1919

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-
}
20+
$this->publishAll();
2821
}
2922

3023
/**

src/Test/TestCase.php

Lines changed: 0 additions & 52 deletions
This file was deleted.

tests/AssetsTestTraitTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use CodeIgniter\Test\CIUnitTestCase;
4+
use Tatter\Assets\Test\AssetsTestTrait;
5+
6+
/**
7+
* @internal
8+
*/
9+
final class AssetsTestTraitTest extends CIUnitTestCase
10+
{
11+
use AssetsTestTrait;
12+
13+
protected $refreshVfs = false;
14+
15+
public function testPublishesOnce()
16+
{
17+
$file = $this->config->directory . $this->config->vendor . 'fruit/third_party.js';
18+
19+
$this->publishAll();
20+
$this->assertFileExists($file);
21+
22+
unlink($file);
23+
24+
$this->publishAll();
25+
$this->assertFileDoesNotExist($file);
26+
}
27+
28+
public function testTearDownRefreshes()
29+
{
30+
$this->assertNotNull($this->root);
31+
32+
$this->refreshVfs = true;
33+
$this->tearDownAssetsTestTrait();
34+
$this->assertNull($this->root);
35+
36+
$this->refreshVfs = false;
37+
}
38+
}

tests/_support/AssetsTestCase.php

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

33
namespace Tests\Support;
44

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

7-
abstract class AssetsTestCase extends TestCase
9+
abstract class AssetsTestCase extends CIUnitTestCase
810
{
11+
/**
12+
* @var AssetsConfig
13+
*/
14+
protected $config;
15+
916
/**
1017
* Preps the config for the test directory.
1118
*/
1219
protected function setUp(): void
1320
{
1421
parent::setUp();
1522

16-
$this->config->directory = SUPPORTPATH . 'Files/';
17-
$this->config->vendor = 'external/';
23+
$this->config = config('Assets');
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);
1829
}
1930
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Tests\Support\Publishers;
4+
5+
use CodeIgniter\Publisher\Publisher;
6+
use Tatter\Assets\Asset;
7+
8+
class FruitPublisher extends Publisher
9+
{
10+
protected $source = SUPPORTPATH . 'Files/external';
11+
12+
/**
13+
* Set the real destination.
14+
*/
15+
public function __construct(?string $source = null, ?string $destination = null)
16+
{
17+
$config = Asset::config();
18+
19+
$this->destination = $config->directory . $config->vendor . 'fruit';
20+
21+
if (! is_dir($this->destination)) {
22+
mkdir($this->destination, 0775, true);
23+
}
24+
25+
parent::__construct($source, $destination);
26+
}
27+
}

0 commit comments

Comments
 (0)