-
-
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 #40 from tattersoftware/test-trait
Reorganize Test
- Loading branch information
Showing
6 changed files
with
169 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace Tatter\Assets\Test; | ||
|
||
use CodeIgniter\Publisher\Publisher; | ||
use org\bovigo\vfs\vfsStream; | ||
use org\bovigo\vfs\vfsStreamDirectory; | ||
use Tatter\Assets\Asset; | ||
use Tatter\Assets\Config\Assets as AssetsConfig; | ||
|
||
/** | ||
* Asset Test Trait | ||
* | ||
* Trait to set up a VFS instance for testing | ||
* Assets, Bundles, and Publishers. | ||
*/ | ||
trait AssetsTestTrait | ||
{ | ||
/** | ||
* Virtual workspace | ||
* | ||
* @var vfsStreamDirectory|null | ||
*/ | ||
protected $root; | ||
|
||
/** | ||
* @var AssetsConfig | ||
*/ | ||
protected $config; | ||
|
||
/** | ||
* Whether the publishers have been run. | ||
* | ||
* @var bool | ||
*/ | ||
private $published = false; | ||
|
||
/** | ||
* Creates the VFS (if necessary) and updates the Assets config. | ||
*/ | ||
protected function setUpAssetsTestTrait(): void | ||
{ | ||
if ($this->root === null) { | ||
$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] = '*'; | ||
} | ||
|
||
/** | ||
* Resets the VFS if $refreshVfs is truthy. | ||
*/ | ||
protected function tearDownAssetsTestTrait(): void | ||
{ | ||
if (! empty($this->refreshVfs)) { | ||
$this->root = null; | ||
$this->published = false; | ||
} | ||
} | ||
|
||
/** | ||
* Publishes all files once so they are | ||
* available for bundles. | ||
*/ | ||
protected function publishAll(): void | ||
{ | ||
if ($this->published) { | ||
return; | ||
} | ||
|
||
foreach (Publisher::discover() as $publisher) { | ||
$publisher->publish(); | ||
} | ||
|
||
$this->published = true; | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,38 @@ | ||
<?php | ||
|
||
use CodeIgniter\Test\CIUnitTestCase; | ||
use Tatter\Assets\Test\AssetsTestTrait; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class AssetsTestTraitTest extends CIUnitTestCase | ||
{ | ||
use AssetsTestTrait; | ||
|
||
protected $refreshVfs = false; | ||
|
||
public function testPublishesOnce() | ||
{ | ||
$file = $this->config->directory . $this->config->vendor . 'fruit/third_party.js'; | ||
|
||
$this->publishAll(); | ||
$this->assertFileExists($file); | ||
|
||
unlink($file); | ||
|
||
$this->publishAll(); | ||
$this->assertFileDoesNotExist($file); | ||
} | ||
|
||
public function testTearDownRefreshes() | ||
{ | ||
$this->assertNotNull($this->root); | ||
|
||
$this->refreshVfs = true; | ||
$this->tearDownAssetsTestTrait(); | ||
$this->assertNull($this->root); | ||
|
||
$this->refreshVfs = false; | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace Tests\Support\Publishers; | ||
|
||
use CodeIgniter\Publisher\Publisher; | ||
use Tatter\Assets\Asset; | ||
|
||
class FruitPublisher extends Publisher | ||
{ | ||
protected $source = SUPPORTPATH . 'Files/external'; | ||
|
||
/** | ||
* Set the real destination. | ||
*/ | ||
public function __construct(?string $source = null, ?string $destination = null) | ||
{ | ||
$config = Asset::config(); | ||
|
||
$this->destination = $config->directory . $config->vendor . 'fruit'; | ||
|
||
if (! is_dir($this->destination)) { | ||
mkdir($this->destination, 0775, true); | ||
} | ||
|
||
parent::__construct($source, $destination); | ||
} | ||
} |