|
4 | 4 |
|
5 | 5 | use StellarWP\Assets\Tests\AssetTestCase; |
6 | 6 | use PHPUnit\Framework\Assert; |
| 7 | +use stdClass; |
| 8 | +use Closure; |
| 9 | +use Generator; |
| 10 | +use InvalidArgumentException; |
7 | 11 |
|
8 | 12 | class AssetsTest extends AssetTestCase { |
9 | 13 | /** |
@@ -43,6 +47,56 @@ public function unset_uopz_redefines() { |
43 | 47 | self::$uopz_redefines = []; |
44 | 48 | } |
45 | 49 |
|
| 50 | + public function it_should_accept_instance_of_asset_or_array_of_assets_in_register_in_wp() { |
| 51 | + $asset_1 = Asset::add( 'fake1-script', 'fake1.js' ); |
| 52 | + $asset_2 = Asset::add( 'fake1-style', 'fake1.css' ); |
| 53 | + $asset_3 = Asset::add( 'fake2-script', 'fake2.js' ); |
| 54 | + $asset_4 = Asset::add( 'fake2-style', 'fake2.css' ); |
| 55 | + $asset_5 = Asset::add( 'fake3-script', 'fake3.js' ); |
| 56 | + |
| 57 | + $assets = Assets::init(); |
| 58 | + |
| 59 | + $assets->register_in_wp( null ); // No problemo... nothing happens though. |
| 60 | + $assets->register_in_wp( [] ); // No problemo... nothing happens though. |
| 61 | + |
| 62 | + $this->assertFalse( $asset_1->is_registered() ); |
| 63 | + $assets->register_in_wp( $asset_1 ); |
| 64 | + $this->assertTrue( $asset_1->is_registered() ); |
| 65 | + |
| 66 | + $this->assertFalse( $asset_2->is_registered() ); |
| 67 | + $this->assertFalse( $asset_3->is_registered() ); |
| 68 | + $this->assertFalse( $asset_4->is_registered() ); |
| 69 | + $this->assertFalse( $asset_5->is_registered() ); |
| 70 | + $assets->register_in_wp( [ $asset_2, $asset_3, $asset_4, $asset_5 ] ); |
| 71 | + $this->assertTrue( $asset_2->is_registered() ); |
| 72 | + $this->assertTrue( $asset_3->is_registered() ); |
| 73 | + $this->assertTrue( $asset_4->is_registered() ); |
| 74 | + $this->assertTrue( $asset_5->is_registered() ); |
| 75 | + } |
| 76 | + |
| 77 | + public function invalid_params_for_register_in_wp_provider(): Generator { |
| 78 | + yield 'string' => [ fn() => 'string' ]; |
| 79 | + yield 'int' => [ fn() => 1 ]; |
| 80 | + yield 'float' => [ fn() => 1.1 ]; |
| 81 | + yield 'bool - true' => [ fn() => true ]; |
| 82 | + yield 'bool - false' => [ fn() => false ]; |
| 83 | + yield 'object' => [ fn() => new stdClass() ]; |
| 84 | + yield 'array - mixed' => [ fn () => [ Asset::add( 'fake1-script', 'fake1.js' ), 'string' ] ]; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @test |
| 89 | + * @dataProvider invalid_params_for_register_in_wp_provider |
| 90 | + */ |
| 91 | + public function it_should_throw_exception_when_invalid_params_are_passed_to_register_in_wp( Closure $fixture ) { |
| 92 | + $assets = Assets::init(); |
| 93 | + |
| 94 | + $this->expectException( InvalidArgumentException::class ); |
| 95 | + $this->expectExceptionMessage( 'Assets in register_in_wp() must be of type Asset' ); |
| 96 | + |
| 97 | + $assets->register_in_wp( $fixture() ); |
| 98 | + } |
| 99 | + |
46 | 100 | /** |
47 | 101 | * @test |
48 | 102 | */ |
|
0 commit comments