|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace derhasi\Composer\Tests; |
| 4 | + |
| 5 | +use derhasi\tempdirectory\TempDirectory; |
| 6 | + |
| 7 | +/** |
| 8 | + * Tests for some examples. |
| 9 | + */ |
| 10 | +class FixturesTest extends \PHPUnit_Framework_TestCase |
| 11 | +{ |
| 12 | + |
| 13 | + /** |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + protected $composerBin; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + protected $fixturesRoot; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + protected $projectRoot; |
| 27 | + |
| 28 | + /** |
| 29 | + * Set up test. |
| 30 | + */ |
| 31 | + protected function setUp() |
| 32 | + { |
| 33 | + $this->projectRoot = realpath(__DIR__.'/..'); |
| 34 | + $this->composerBin = realpath(__DIR__.'/../vendor/bin/composer'); |
| 35 | + $this->fixturesRoot = realpath(__DIR__.'/fixtures'); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Test provided fixtures. |
| 40 | + * |
| 41 | + * @param string $folder |
| 42 | + * Name of the folder of the fixture |
| 43 | + * @param array $commands |
| 44 | + * Array of composer commands to process |
| 45 | + * @param array $files |
| 46 | + * Array of files to check for existance |
| 47 | + * |
| 48 | + * @dataProvider fixturesProvider |
| 49 | + */ |
| 50 | + public function testFixtures($folder, $commands = array(), $files = array()) |
| 51 | + { |
| 52 | + $workingDirectory = new TempDirectory(__METHOD__.$folder); |
| 53 | + |
| 54 | + chdir($workingDirectory->getRoot()); |
| 55 | + copy($this->fixturesRoot.'/example/composer.json', $workingDirectory->getRoot().'/composer.json'); |
| 56 | + |
| 57 | + // Add this project as local development repository sow we work with |
| 58 | + // the latest code. |
| 59 | + $this->composer('config', 'repositories.dev', 'path', $this->projectRoot); |
| 60 | + |
| 61 | + $this->composer('install'); |
| 62 | + |
| 63 | + // Run additional composer commands. |
| 64 | + foreach ($commands as $command) { |
| 65 | + call_user_func_array(array($this, 'composer'), $command); |
| 66 | + } |
| 67 | + |
| 68 | + // Check for file existance. |
| 69 | + foreach ($files as $file) { |
| 70 | + $this->assertFileExists($file); |
| 71 | + } |
| 72 | + |
| 73 | + unset($workingDirectory); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Provides fixtures test data. |
| 78 | + * |
| 79 | + * @return array |
| 80 | + */ |
| 81 | + public function fixturesProvider() |
| 82 | + { |
| 83 | + return array( |
| 84 | + array( |
| 85 | + 'example', |
| 86 | + // Update drupal/drupal to the newest release |
| 87 | + array( |
| 88 | + array('update', 'drupal/drupal'), |
| 89 | + ), |
| 90 | + array( 'web/index.php', 'web/sites/all/modules/contrib/views/views.module'), |
| 91 | + ), |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Run composer command. |
| 97 | + * |
| 98 | + * @param string $command |
| 99 | + * @param string $arg,... Optional arguments |
| 100 | + * |
| 101 | + * @return string[] |
| 102 | + * Array of output lines by the composer command. |
| 103 | + */ |
| 104 | + protected function composer($command) |
| 105 | + { |
| 106 | + $exec = $this->composerBin; |
| 107 | + $exec .= ' '.escapeshellcmd($command); |
| 108 | + $args = func_get_args(); |
| 109 | + array_shift($args); |
| 110 | + foreach ($args as $arg) { |
| 111 | + if (strlen($arg) > 0) { |
| 112 | + $exec .= ' '.escapeshellarg($arg); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + $output = array(); |
| 117 | + $returnCode = null; |
| 118 | + exec("$exec 2>&1", $output, $returnCode); |
| 119 | + |
| 120 | + if ($returnCode) { |
| 121 | + throw new \Exception(sprintf('Composer command "%s" failed:\n%s"', $exec, implode("\n", $output))); |
| 122 | + } |
| 123 | + |
| 124 | + return $output; |
| 125 | + } |
| 126 | +} |
0 commit comments