|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Gaambo\DeployerWordpress\Tests\Functional\Tasks; |
| 4 | + |
| 5 | +use Gaambo\DeployerWordpress\Tests\Functional\FunctionalTestCase; |
| 6 | +use RuntimeException; |
| 7 | + |
| 8 | +class PackagesTasksFunctionalTest extends FunctionalTestCase |
| 9 | +{ |
| 10 | + private string $localMuPluginsDir; |
| 11 | + private string $remoteMuPluginsDir; |
| 12 | + |
| 13 | + protected function setUp(): void |
| 14 | + { |
| 15 | + parent::setUp(); |
| 16 | + |
| 17 | + // Set up temporary directories |
| 18 | + $this->localMuPluginsDir = $this->localDocRootDir . '/wp-content/mu-plugins'; |
| 19 | + $this->remoteMuPluginsDir = $this->remoteReleaseDir . '/wp-content/mu-plugins'; |
| 20 | + |
| 21 | + // Create directories |
| 22 | + mkdir($this->localMuPluginsDir, 0755, true); |
| 23 | + mkdir($this->remoteMuPluginsDir, 0755, true); |
| 24 | + } |
| 25 | + |
| 26 | + public function testListAvailableTasks(): void |
| 27 | + { |
| 28 | + $this->dep('list', null); |
| 29 | + $output = $this->tester->getDisplay(); |
| 30 | + |
| 31 | + $this->assertStringContainsString('packages:assets:vendors', $output); |
| 32 | + $this->assertStringContainsString('packages:assets:build', $output); |
| 33 | + $this->assertStringContainsString('packages:vendors', $output); |
| 34 | + $this->assertStringContainsString('packages:assets', $output); |
| 35 | + $this->assertStringContainsString('packages', $output); |
| 36 | + } |
| 37 | + |
| 38 | + public function testPackagesAssetsVendorsOnlyRunsWhenAssetsIsTrue(): void |
| 39 | + { |
| 40 | + // Copy test plugin from fixtures to local |
| 41 | + $fixturePluginDir = $this->getFixturePath('mu-plugins/test-plugin'); |
| 42 | + |
| 43 | + $targetPluginDir = $this->localMuPluginsDir . '/test-plugin-with-assets'; |
| 44 | + mkdir($targetPluginDir, 0755, true); |
| 45 | + $this->copyDirectory($fixturePluginDir, $targetPluginDir); |
| 46 | + |
| 47 | + $targetPluginDir = $this->localMuPluginsDir . '/test-plugin-without-assets'; |
| 48 | + mkdir($targetPluginDir, 0755, true); |
| 49 | + $this->copyDirectory($fixturePluginDir, $targetPluginDir); |
| 50 | + |
| 51 | + $targetPluginDir = $this->localMuPluginsDir . '/test-plugin-without-assets-flag'; |
| 52 | + mkdir($targetPluginDir, 0755, true); |
| 53 | + $this->copyDirectory($fixturePluginDir, $targetPluginDir); |
| 54 | + |
| 55 | + // Create test packages configuration |
| 56 | + $this->deployer->config->set('packages', [ |
| 57 | + [ |
| 58 | + 'path' => '{{mu-plugins/dir}}/test-plugin-with-assets', |
| 59 | + 'assets' => true |
| 60 | + ], |
| 61 | + [ |
| 62 | + 'path' => '{{mu-plugins/dir}}/test-plugin-without-assets', |
| 63 | + 'assets' => false |
| 64 | + ], |
| 65 | + [ |
| 66 | + 'path' => '{{mu-plugins/dir}}/test-plugin-without-assets-flag' |
| 67 | + // No assets flag |
| 68 | + ] |
| 69 | + ]); |
| 70 | + |
| 71 | + // Mock npm install command |
| 72 | + $this->mockCommands([ |
| 73 | + 'npm install' => function ($host, $command) { |
| 74 | + if(str_contains($command, 'test-plugin-without-assets') || str_contains($command, 'test-plugin-without-assets-flag')) { |
| 75 | + throw new RuntimeException('npm install called for plugin without assets/package.json'); |
| 76 | + } |
| 77 | + return 'npm install completed.'; |
| 78 | + }, |
| 79 | + ], 'testremote'); |
| 80 | + |
| 81 | + // Run the task |
| 82 | + $result = $this->dep('packages:assets:vendors'); |
| 83 | + $this->assertEquals(0, $result); |
| 84 | + } |
| 85 | + |
| 86 | + public function testPackagesAssetsBuildOnlyRunsWhenAssetsIsTrue(): void |
| 87 | + { |
| 88 | + // Copy test plugin from fixtures to local |
| 89 | + $fixturePluginDir = $this->getFixturePath('mu-plugins/test-plugin'); |
| 90 | + |
| 91 | + $targetPluginDir = $this->localMuPluginsDir . '/test-plugin-with-assets'; |
| 92 | + mkdir($targetPluginDir, 0755, true); |
| 93 | + $this->copyDirectory($fixturePluginDir, $targetPluginDir); |
| 94 | + |
| 95 | + $targetPluginDir = $this->localMuPluginsDir . '/test-plugin-without-assets'; |
| 96 | + mkdir($targetPluginDir, 0755, true); |
| 97 | + $this->copyDirectory($fixturePluginDir, $targetPluginDir); |
| 98 | + |
| 99 | + $targetPluginDir = $this->localMuPluginsDir . '/test-plugin-without-assets-flag'; |
| 100 | + mkdir($targetPluginDir, 0755, true); |
| 101 | + $this->copyDirectory($fixturePluginDir, $targetPluginDir); |
| 102 | + |
| 103 | + // Create test packages configuration |
| 104 | + $this->deployer->config->set('packages', [ |
| 105 | + [ |
| 106 | + 'path' => '{{mu-plugins/dir}}/test-plugin-with-assets', |
| 107 | + 'assets' => true |
| 108 | + ], |
| 109 | + [ |
| 110 | + 'path' => '{{mu-plugins/dir}}/test-plugin-without-assets', |
| 111 | + 'assets' => false |
| 112 | + ], |
| 113 | + [ |
| 114 | + 'path' => '{{mu-plugins/dir}}/test-plugin-without-assets-flag' |
| 115 | + // No assets flag |
| 116 | + ] |
| 117 | + ]); |
| 118 | + |
| 119 | + // Mock npm build command |
| 120 | + $this->mockCommands([ |
| 121 | + 'npm run-script build' => function ($host, $command) { |
| 122 | + if(str_contains($command, 'test-plugin-without-assets') || str_contains($command, 'test-plugin-without-assets-flag')) { |
| 123 | + throw new RuntimeException('npm build called for plugin without assets/package.json'); |
| 124 | + } |
| 125 | + return 'npm build completed.'; |
| 126 | + }, |
| 127 | + ], 'testremote'); |
| 128 | + |
| 129 | + // Run the task |
| 130 | + $result = $this->dep('packages:assets:build'); |
| 131 | + $this->assertEquals(0, $result); |
| 132 | + } |
| 133 | + |
| 134 | + public function testPackagesVendorsOnlyRunsWhenVendorsIsTrue(): void |
| 135 | + { |
| 136 | + // Copy test plugin from fixtures to local |
| 137 | + $fixturePluginDir = $this->getFixturePath('mu-plugins/test-plugin'); |
| 138 | + |
| 139 | + $targetPluginDir = $this->localMuPluginsDir . '/test-plugin-with-vendors'; |
| 140 | + mkdir($targetPluginDir, 0755, true); |
| 141 | + $this->copyDirectory($fixturePluginDir, $targetPluginDir); |
| 142 | + |
| 143 | + $targetPluginDir = $this->localMuPluginsDir . '/test-plugin-without-vendors'; |
| 144 | + mkdir($targetPluginDir, 0755, true); |
| 145 | + $this->copyDirectory($fixturePluginDir, $targetPluginDir); |
| 146 | + |
| 147 | + $targetPluginDir = $this->localMuPluginsDir . '/test-plugin-without-vendors-flag'; |
| 148 | + mkdir($targetPluginDir, 0755, true); |
| 149 | + $this->copyDirectory($fixturePluginDir, $targetPluginDir); |
| 150 | + |
| 151 | + // Create test packages configuration |
| 152 | + $this->deployer->config->set('packages', [ |
| 153 | + [ |
| 154 | + 'path' => '{{mu-plugins/dir}}/test-plugin-with-vendors', |
| 155 | + 'vendors' => true |
| 156 | + ], |
| 157 | + [ |
| 158 | + 'path' => '{{mu-plugins/dir}}/test-plugin-without-vendors', |
| 159 | + 'vendors' => false |
| 160 | + ], |
| 161 | + [ |
| 162 | + 'path' => '{{mu-plugins/dir}}/test-plugin-without-vendors-flag' |
| 163 | + // No assets flag |
| 164 | + ] |
| 165 | + ]); |
| 166 | + |
| 167 | + // Mock npm install command |
| 168 | + $this->mockCommands([ |
| 169 | + 'composer install' => function ($host, $command) { |
| 170 | + if(str_contains($command, 'test-plugin-without-vendors') || str_contains($command, 'test-plugin-without-vendors-flag')) { |
| 171 | + throw new RuntimeException('composer install called for plugin without vendors'); |
| 172 | + } |
| 173 | + return 'npm install completed.'; |
| 174 | + }, |
| 175 | + ], 'testremote'); |
| 176 | + |
| 177 | + // Run the task |
| 178 | + $result = $this->dep('packages:vendors'); |
| 179 | + $this->assertEquals(0, $result); |
| 180 | + } |
| 181 | +} |
0 commit comments