-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from tattersoftware/test
Bundle Testing
- Loading branch information
Showing
5 changed files
with
157 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters