diff --git a/src/PhpFpm.php b/src/PhpFpm.php index c14a855..fd8b2a0 100644 --- a/src/PhpFpm.php +++ b/src/PhpFpm.php @@ -7,40 +7,38 @@ class PhpFpm { + var $brew, $cli; + + var $taps = [ + 'homebrew/dupes', 'homebrew/versions', 'homebrew/homebrew-php' + ]; + + /** + * Create a new PHP FPM class instance. + * + * @param Brew $brew + * @param CommandLine $cli + * @return void + */ + function __construct(Brew $brew, CommandLine $cli) + { + $this->cli = $cli; + $this->brew = $brew; + } + /** * Install and configure DnsMasq. * * @param OutputInterface $output * @return void */ - public static function install($output) + function install($output) { - if (! Brew::hasInstalledPhp()) { - static::download($output); - } + $this->brew->ensureInstalled('php70', $this->taps); - static::updateConfiguration(); + $this->updateConfiguration(); - static::restart(); - } - - /** - * Download a fresh copy of PHP 7.0 from Brew. - * - * @param OutputInterface $output - * @return void - */ - public static function download($output) - { - $output->writeln('PHP 7.0 is not installed, installing it now via Brew... 🍻'); - - Brew::tap('homebrew/dupes', 'homebrew/versions', 'homebrew/homebrew-php'); - - run('brew install php70', function ($exitCode, $processOutput) use ($output) { - $output->write($processOutput); - - throw new Exception('We were unable to install PHP.'); - }); + $this->restart(); } /** @@ -48,11 +46,11 @@ public static function download($output) * * @return void */ - public static function updateConfiguration() + function updateConfiguration() { - quietly('sed -i "" -E "s/^user = .+$/user = '.$_SERVER['SUDO_USER'].'/" '.static::fpmConfigPath()); + $this->cli->quietly('sed -i "" -E "s/^user = .+$/user = '.user().'/" '.$this->fpmConfigPath()); - quietly('sed -i "" -E "s/^group = .+$/group = staff/" '.static::fpmConfigPath()); + $this->cli->quietly('sed -i "" -E "s/^group = .+$/group = staff/" '.$this->fpmConfigPath()); } /** @@ -60,11 +58,11 @@ public static function updateConfiguration() * * @return void */ - public static function restart() + function restart() { - static::stop(); + $this->stop(); - Brew::restartLinkedPhp(); + $this->brew->restartLinkedPhp(); } /** @@ -72,9 +70,9 @@ public static function restart() * * @return void */ - public static function stop() + function stop() { - Brew::stopService('php56', 'php70'); + $this->brew->stopService('php56', 'php70'); } /** @@ -82,9 +80,9 @@ public static function stop() * * @return string */ - protected static function fpmConfigPath() + function fpmConfigPath() { - if (Brew::linkedPhp() === 'php70') { + if ($this->brew->linkedPhp() === 'php70') { return '/usr/local/etc/php/7.0/php-fpm.d/www.conf'; } else { return '/usr/local/etc/php/5.6/php-fpm.conf'; diff --git a/tests/BrewTest.php b/tests/BrewTest.php index dbfcde6..ed26e99 100644 --- a/tests/BrewTest.php +++ b/tests/BrewTest.php @@ -30,12 +30,12 @@ public function test_brew_can_be_resolved_from_container() public function test_installed_returns_true_when_given_formula_is_installed() { $cli = Mockery::mock(CommandLine::class); - $cli->shouldReceive('run')->with('brew list | grep php70')->andReturn('php70'); + $cli->shouldReceive('run')->once()->with('brew list | grep php70')->andReturn('php70'); swap(CommandLine::class, $cli); $this->assertTrue(resolve(Brew::class)->installed('php70')); $cli = Mockery::mock(CommandLine::class); - $cli->shouldReceive('run')->with('brew list | grep php70')->andReturn('php70-mcrypt + $cli->shouldReceive('run')->once()->with('brew list | grep php70')->andReturn('php70-mcrypt php70'); swap(CommandLine::class, $cli); $this->assertTrue(resolve(Brew::class)->installed('php70')); @@ -45,17 +45,17 @@ public function test_installed_returns_true_when_given_formula_is_installed() public function test_installed_returns_false_when_given_formula_is_not_installed() { $cli = Mockery::mock(CommandLine::class); - $cli->shouldReceive('run')->with('brew list | grep php70')->andReturn(''); + $cli->shouldReceive('run')->once()->with('brew list | grep php70')->andReturn(''); swap(CommandLine::class, $cli); $this->assertFalse(resolve(Brew::class)->installed('php70')); $cli = Mockery::mock(CommandLine::class); - $cli->shouldReceive('run')->with('brew list | grep php70')->andReturn('php70-mcrypt'); + $cli->shouldReceive('run')->once()->with('brew list | grep php70')->andReturn('php70-mcrypt'); swap(CommandLine::class, $cli); $this->assertFalse(resolve(Brew::class)->installed('php70')); $cli = Mockery::mock(CommandLine::class); - $cli->shouldReceive('run')->with('brew list | grep php70')->andReturn('php70-mcrypt + $cli->shouldReceive('run')->once()->with('brew list | grep php70')->andReturn('php70-mcrypt php70-something-else php7'); swap(CommandLine::class, $cli); @@ -66,18 +66,18 @@ public function test_installed_returns_false_when_given_formula_is_not_installed public function test_has_installed_php_indicates_if_php_is_installed_via_brew() { $brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]); - $brew->shouldReceive('installed')->with('php70')->andReturn(true); + $brew->shouldReceive('installed')->once()->with('php70')->andReturn(true); $brew->shouldReceive('installed')->with('php56')->andReturn(true); $this->assertTrue($brew->hasInstalledPhp()); $brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]); - $brew->shouldReceive('installed')->with('php70')->andReturn(true); + $brew->shouldReceive('installed')->once()->with('php70')->andReturn(true); $brew->shouldReceive('installed')->with('php56')->andReturn(false); $this->assertTrue($brew->hasInstalledPhp()); $brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]); - $brew->shouldReceive('installed')->with('php70')->andReturn(false); - $brew->shouldReceive('installed')->with('php56')->andReturn(false); + $brew->shouldReceive('installed')->once()->with('php70')->andReturn(false); + $brew->shouldReceive('installed')->once()->with('php56')->andReturn(false); $this->assertFalse($brew->hasInstalledPhp()); } @@ -85,8 +85,8 @@ public function test_has_installed_php_indicates_if_php_is_installed_via_brew() public function test_tap_taps_the_given_homebrew_repository() { $cli = Mockery::mock(CommandLine::class); - $cli->shouldReceive('passthru')->with('sudo -u Taylor brew tap php70'); - $cli->shouldReceive('passthru')->with('sudo -u Taylor brew tap php56'); + $cli->shouldReceive('passthru')->once()->with('sudo -u Taylor brew tap php70'); + $cli->shouldReceive('passthru')->once()->with('sudo -u Taylor brew tap php56'); swap(CommandLine::class, $cli); resolve(Brew::class)->tap('php70', 'php56'); } @@ -95,7 +95,7 @@ public function test_tap_taps_the_given_homebrew_repository() public function test_restart_restarts_the_service_using_homebrew_services() { $cli = Mockery::mock(CommandLine::class); - $cli->shouldReceive('quietly')->with('sudo brew services restart dnsmasq'); + $cli->shouldReceive('quietly')->once()->with('sudo brew services restart dnsmasq'); swap(CommandLine::class, $cli); resolve(Brew::class)->restartService('dnsmasq'); } @@ -104,7 +104,7 @@ public function test_restart_restarts_the_service_using_homebrew_services() public function test_stop_stops_the_service_using_homebrew_services() { $cli = Mockery::mock(CommandLine::class); - $cli->shouldReceive('quietly')->with('sudo brew services stop dnsmasq'); + $cli->shouldReceive('quietly')->once()->with('sudo brew services stop dnsmasq'); swap(CommandLine::class, $cli); resolve(Brew::class)->stopService('dnsmasq'); } @@ -113,14 +113,14 @@ public function test_stop_stops_the_service_using_homebrew_services() public function test_linked_php_returns_linked_php_formula_name() { $files = Mockery::mock(Filesystem::class); - $files->shouldReceive('isLink')->with('/usr/local/bin/php')->andReturn(true); - $files->shouldReceive('readLink')->with('/usr/local/bin/php')->andReturn('/test/path/php70/test'); + $files->shouldReceive('isLink')->once()->with('/usr/local/bin/php')->andReturn(true); + $files->shouldReceive('readLink')->once()->with('/usr/local/bin/php')->andReturn('/test/path/php70/test'); swap(Filesystem::class, $files); $this->assertEquals('php70', resolve(Brew::class)->linkedPhp()); $files = Mockery::mock(Filesystem::class); - $files->shouldReceive('isLink')->with('/usr/local/bin/php')->andReturn(true); - $files->shouldReceive('readLink')->with('/usr/local/bin/php')->andReturn('/test/path/php56/test'); + $files->shouldReceive('isLink')->once()->with('/usr/local/bin/php')->andReturn(true); + $files->shouldReceive('readLink')->once()->with('/usr/local/bin/php')->andReturn('/test/path/php56/test'); swap(Filesystem::class, $files); $this->assertEquals('php56', resolve(Brew::class)->linkedPhp()); } @@ -132,7 +132,7 @@ public function test_linked_php_returns_linked_php_formula_name() public function test_linked_php_throws_exception_if_no_php_link() { $files = Mockery::mock(Filesystem::class); - $files->shouldReceive('isLink')->with('/usr/local/bin/php')->andReturn(false); + $files->shouldReceive('isLink')->once()->with('/usr/local/bin/php')->andReturn(false); swap(Filesystem::class, $files); resolve(Brew::class)->linkedPhp(); } @@ -144,8 +144,8 @@ public function test_linked_php_throws_exception_if_no_php_link() public function test_linked_php_throws_exception_if_unsupported_php_version_is_linked() { $files = Mockery::mock(Filesystem::class); - $files->shouldReceive('isLink')->with('/usr/local/bin/php')->andReturn(true); - $files->shouldReceive('readLink')->with('/usr/local/bin/php')->andReturn('/test/path/php42/test'); + $files->shouldReceive('isLink')->once()->with('/usr/local/bin/php')->andReturn(true); + $files->shouldReceive('readLink')->once()->with('/usr/local/bin/php')->andReturn('/test/path/php42/test'); swap(Filesystem::class, $files); resolve(Brew::class)->linkedPhp(); } @@ -154,7 +154,7 @@ public function test_linked_php_throws_exception_if_unsupported_php_version_is_l public function test_install_or_fail_will_install_brew_formulas() { $cli = Mockery::mock(CommandLine::class); - $cli->shouldReceive('run')->with('brew install dnsmasq', Mockery::type('Closure')); + $cli->shouldReceive('run')->once()->with('brew install dnsmasq', Mockery::type('Closure')); swap(CommandLine::class, $cli); resolve(Brew::class)->installOrFail('dnsmasq'); } @@ -163,10 +163,10 @@ public function test_install_or_fail_will_install_brew_formulas() public function test_install_or_fail_can_install_taps() { $cli = Mockery::mock(CommandLine::class); - $cli->shouldReceive('run')->with('brew install dnsmasq', Mockery::type('Closure')); + $cli->shouldReceive('run')->once()->with('brew install dnsmasq', Mockery::type('Closure')); swap(CommandLine::class, $cli); $brew = Mockery::mock(Brew::class.'[tap]', [$cli, new Filesystem]); - $brew->shouldReceive('tap')->with(['test/tap']); + $brew->shouldReceive('tap')->once()->with(['test/tap']); $brew->installOrFail('dnsmasq', ['test/tap']); } diff --git a/tests/DnsMasqTest.php b/tests/DnsMasqTest.php index bd3c7b5..fe31b3d 100644 --- a/tests/DnsMasqTest.php +++ b/tests/DnsMasqTest.php @@ -29,8 +29,8 @@ public function tearDown() public function test_install_installs_and_places_configuration_files_in_proper_locations() { $brew = Mockery::mock(Brew::class); - $brew->shouldReceive('ensureInstalled')->with('dnsmasq'); - $brew->shouldReceive('restartService')->with('dnsmasq'); + $brew->shouldReceive('ensureInstalled')->once()->with('dnsmasq'); + $brew->shouldReceive('restartService')->once()->with('dnsmasq'); swap(Brew::class, $brew); $dnsMasq = resolve(StubForCreatingCustomDnsMasqConfigFiles::class); diff --git a/tests/PhpFpmTest.php b/tests/PhpFpmTest.php new file mode 100644 index 0000000..bb8d35f --- /dev/null +++ b/tests/PhpFpmTest.php @@ -0,0 +1,45 @@ +updateConfiguration(); + $contents = file_get_contents(__DIR__.'/output/fpm.conf'); + $this->assertTrue(strpos($contents, 'user = '.user()) !== false); + $this->assertTrue(strpos($contents, 'group = staff') !== false); + } +} + + +class StubForUpdatingFpmConfigFiles extends PhpFpm +{ + function fpmConfigPath() + { + return __DIR__.'/output/fpm.conf'; + } +} diff --git a/tests/files/fpm.conf b/tests/files/fpm.conf new file mode 100644 index 0000000..23c2f0f --- /dev/null +++ b/tests/files/fpm.conf @@ -0,0 +1,6 @@ +Test contents + +user = something +group = something-else + +more contents