|
| 1 | +<?php |
| 2 | + |
| 3 | +declare( strict_types=1 ); |
| 4 | + |
| 5 | +namespace wpunit; |
| 6 | + |
| 7 | +use InvalidArgumentException; |
| 8 | +use LogicException; |
| 9 | +use StellarWP\Assets\Config; |
| 10 | +use StellarWP\Assets\Tests\AssetTestCase; |
| 11 | +use StellarWP\Assets\VendorAsset; |
| 12 | + |
| 13 | +class VendorAssetTest extends AssetTestCase { |
| 14 | + |
| 15 | + /** |
| 16 | + * @before |
| 17 | + */ |
| 18 | + public function before_tests(): void { |
| 19 | + Config::reset(); |
| 20 | + Config::set_hook_prefix( 'jpry' ); |
| 21 | + } |
| 22 | + |
| 23 | + public function test_can_create_vendor_asset(): void { |
| 24 | + $asset = new VendorAsset( 'test-script', 'https://example.com/fake.js' ); |
| 25 | + |
| 26 | + $this->assertEquals( 'test-script', $asset->get_slug() ); |
| 27 | + $this->assertEquals( 'https://example.com/fake.js', $asset->get_url() ); |
| 28 | + $this->assertEquals( 'js', $asset->get_type() ); |
| 29 | + } |
| 30 | + |
| 31 | + public function test_invalid_url_throws_exception(): void { |
| 32 | + $this->expectException( InvalidArgumentException::class ); |
| 33 | + |
| 34 | + new VendorAsset( 'test-script', 'invalid-url' ); |
| 35 | + } |
| 36 | + |
| 37 | + public function test_can_set_version(): void { |
| 38 | + $asset = new VendorAsset( 'test-script', 'https://example.com/fake.js' ); |
| 39 | + $asset->set_version( '1.0.0' ); |
| 40 | + |
| 41 | + $this->assertEquals( '1.0.0', $asset->get_version() ); |
| 42 | + $this->assertEquals( 'https://example.com/fake.js?ver=1.0.0', $asset->get_url() ); |
| 43 | + } |
| 44 | + |
| 45 | + public function test_can_set_version_with_url_placeholder(): void { |
| 46 | + $asset = new VendorAsset( 'test-script', 'https://example.com/path/to/version/%s/fake.js' ); |
| 47 | + $asset->set_version( '1.2.3' ); |
| 48 | + $this->assertEquals( '1.2.3', $asset->get_version() ); |
| 49 | + $this->assertEquals( 'https://example.com/path/to/version/1.2.3/fake.js', $asset->get_url() ); |
| 50 | + } |
| 51 | + |
| 52 | + public function test_placeholder_without_version_throws_error(): void { |
| 53 | + $this->expectException( LogicException::class ); |
| 54 | + $this->expectExceptionMessage( 'A URL with a placeholder must have a version provided.' ); |
| 55 | + |
| 56 | + $asset = new VendorAsset( 'test-script', 'https://example.com/path/to/version/%s/fake.js' ); |
| 57 | + $asset->get_url(); |
| 58 | + } |
| 59 | + |
| 60 | + public function test_version_with_query_string_has_ver_appended_correctly(): void { |
| 61 | + $asset = new VendorAsset( 'test-script', 'https://example.com/path/to/version?query=string' ); |
| 62 | + $asset->set_version( '1.2.3' ); |
| 63 | + |
| 64 | + $this->assertEquals( '1.2.3', $asset->get_version() ); |
| 65 | + $this->assertEquals( 'https://example.com/path/to/version?query=string&ver=1.2.3', $asset->get_url() ); |
| 66 | + } |
| 67 | +} |
0 commit comments