|
12 | 12 | namespace Puli\Cli\Tests; |
13 | 13 |
|
14 | 14 | use PHPUnit_Framework_TestCase; |
| 15 | +use Symfony\Component\Filesystem\Filesystem; |
15 | 16 | use Symfony\Component\Process\PhpExecutableFinder; |
16 | 17 | use Symfony\Component\Process\Process; |
| 18 | +use Symfony\Component\Process\ProcessUtils; |
| 19 | +use Webmozart\Glob\Test\TestUtil; |
17 | 20 | use Webmozart\PathUtil\Path; |
18 | 21 |
|
19 | 22 | /** |
|
23 | 26 | */ |
24 | 27 | class PuliBinTest extends PHPUnit_Framework_TestCase |
25 | 28 | { |
26 | | - public function testRunHelp() |
| 29 | + private static $php; |
| 30 | + |
| 31 | + private $rootDir; |
| 32 | + |
| 33 | + private $puli; |
| 34 | + |
| 35 | + public static function setUpBeforeClass() |
27 | 36 | { |
28 | 37 | $phpFinder = new PhpExecutableFinder(); |
29 | 38 |
|
30 | | - if (!($php = $phpFinder->find())) { |
| 39 | + self::$php = $phpFinder->find(); |
| 40 | + } |
| 41 | + |
| 42 | + protected function setUp() |
| 43 | + { |
| 44 | + if (!self::$php) { |
31 | 45 | $this->markTestSkipped('The "php" command could not be found.'); |
32 | 46 | } |
33 | 47 |
|
34 | | - $rootDir = Path::normalize(realpath(__DIR__.'/..')); |
35 | | - $process = new Process($php.' '.$rootDir.'/bin/puli'); |
| 48 | + $this->rootDir = TestUtil::makeTempDir('puli-manager', __CLASS__); |
| 49 | + $this->puli = Path::canonicalize(__DIR__.'/../bin/puli'); |
| 50 | + |
| 51 | + $filesystem = new Filesystem(); |
| 52 | + $filesystem->mirror(__DIR__.'/Fixtures/root', $this->rootDir); |
| 53 | + |
| 54 | + // Load the package to import the "puli/public-resource" type |
| 55 | + $filesystem->mirror(__DIR__.'/../vendor/puli/url-generator', $this->rootDir.'/vendor/puli/url-generator'); |
| 56 | + } |
| 57 | + |
| 58 | + protected function tearDown() |
| 59 | + { |
| 60 | + $filesystem = new Filesystem(); |
| 61 | + $filesystem->remove($this->rootDir); |
| 62 | + } |
| 63 | + |
| 64 | + public function testHelp() |
| 65 | + { |
| 66 | + $output = $this->runPuli(''); |
| 67 | + |
| 68 | + $this->assertTrue(0 === strpos($output, 'Puli version ') || 0 === strpos($output, "Debug Mode\nPuli version ")); |
| 69 | + } |
| 70 | + |
| 71 | + public function testMap() |
| 72 | + { |
| 73 | + $mappingExistsRegExp = '~\s/app\s+res\s~'; |
| 74 | + |
| 75 | + $this->assertEmpty($this->runPuli('map /app res')); |
| 76 | + $this->assertRegExp($mappingExistsRegExp, $this->runPuli('map')); |
| 77 | + $this->assertRegExp('~^app\s~', $this->runPuli('ls')); |
| 78 | + $this->assertRegExp('~^messages.en.yml\s~', $this->runPuli('ls app')); |
| 79 | + $this->assertRegExp('~\s/app/messages.en.yml\s~', $this->runPuli('find --name *.yml')); |
| 80 | + $this->assertEmpty($this->runPuli('map -d /app')); |
| 81 | + $this->assertNotRegExp($mappingExistsRegExp, $this->runPuli('map')); |
| 82 | + } |
| 83 | + |
| 84 | + public function testType() |
| 85 | + { |
| 86 | + $typeExistsRegExp = '~\sthor/catalog\s~'; |
| 87 | + |
| 88 | + $this->assertEmpty($this->runPuli('type --define thor/catalog')); |
| 89 | + $this->assertRegExp($typeExistsRegExp, $this->runPuli('type')); |
| 90 | + $this->assertEmpty($this->runPuli('type -d thor/catalog')); |
| 91 | + $this->assertNotRegExp($typeExistsRegExp, $this->runPuli('type')); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @depends testMap |
| 96 | + * @depends testType |
| 97 | + */ |
| 98 | + public function testBind() |
| 99 | + { |
| 100 | + $bindingExistsRegExp = '~\s/app/\*\.yml\s+thor/catalog\s~'; |
| 101 | + |
| 102 | + $this->runPuli('map /app res'); |
| 103 | + $this->runPuli('type --define thor/catalog'); |
| 104 | + |
| 105 | + $this->assertEmpty($this->runPuli('bind /app/*.yml thor/catalog')); |
| 106 | + |
| 107 | + $output = $this->runPuli('bind'); |
| 108 | + |
| 109 | + $this->assertRegExp($bindingExistsRegExp, $output); |
| 110 | + $this->assertSame(1, preg_match('~\s(\S+)\s+/app/\*\.yml~', $output, $matches)); |
| 111 | + |
| 112 | + $uuid = $matches[1]; |
| 113 | + |
| 114 | + $this->assertEmpty($this->runPuli('bind -d '.$uuid)); |
| 115 | + $this->assertNotRegExp($bindingExistsRegExp, $this->runPuli('bind')); |
| 116 | + } |
| 117 | + |
| 118 | + public function testServer() |
| 119 | + { |
| 120 | + $serverExistsRegExp = '~\slocalhost\s~'; |
| 121 | + |
| 122 | + $this->assertEmpty($this->runPuli('server --add localhost public_html')); |
| 123 | + $this->assertRegExp($serverExistsRegExp, $this->runPuli('server')); |
| 124 | + $this->assertEmpty($this->runPuli('server -d localhost')); |
| 125 | + $this->assertNotRegExp($serverExistsRegExp, $this->runPuli('server')); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @depends testServer |
| 130 | + */ |
| 131 | + public function testPublish() |
| 132 | + { |
| 133 | + $assetExistsRegExp = '~\s/app/public\s+/\s~'; |
| 134 | + |
| 135 | + $this->runPuli('build'); |
| 136 | + $this->runPuli('map /app res'); |
| 137 | + $this->runPuli('server --add localhost public_html'); |
| 138 | + |
| 139 | + $this->assertEmpty($this->runPuli('publish /app/public localhost')); |
| 140 | + |
| 141 | + $output = $this->runPuli('publish'); |
36 | 142 |
|
| 143 | + $this->assertRegExp($assetExistsRegExp, $output); |
| 144 | + $this->assertSame(1, preg_match('~\s(\S+)\s+/app/public~', $output, $matches)); |
| 145 | + |
| 146 | + $uuid = $matches[1]; |
| 147 | + |
| 148 | + $this->assertEmpty($this->runPuli('publish -d '.$uuid)); |
| 149 | + $this->assertNotRegExp($assetExistsRegExp, $this->runPuli('publish')); |
| 150 | + } |
| 151 | + |
| 152 | + private function runPuli($command) |
| 153 | + { |
| 154 | + $php = escapeshellcmd(self::$php); |
| 155 | + $puli = ProcessUtils::escapeArgument($this->puli); |
| 156 | + $process = new Process($php.' '.$puli.' '.$command, $this->rootDir); |
37 | 157 | $status = $process->run(); |
38 | | - $output = $process->getOutput(); |
| 158 | + $output = (string) $process->getOutput(); |
| 159 | + |
| 160 | + if (0 !== $status) { |
| 161 | + var_dump($process->getErrorOutput()); |
| 162 | + } |
39 | 163 |
|
40 | 164 | $this->assertSame(0, $status); |
41 | | - $this->assertTrue(0 === strpos($output, 'Puli version ') || 0 === strpos($output, "Debug Mode\nPuli version ")); |
| 165 | + |
| 166 | + return $output; |
42 | 167 | } |
43 | 168 | } |
0 commit comments