<?php
App::uses('BemmelhorComposer', 'Extensions.Lib');

/**
 * Bemmelhor Composer Test
 *
 * PHP version 5
 *
 * @category Test
 * @package  Bemmelhor
 * @version  1.4
 * @author   Fabricio S Costa <fabriciojf@gmail.com>
 * 
 * @link     http://www.bemmelhor.org
 */
class BemmelhorComposerTest extends CakeTestCase {

/**
 * setUp
 *
 * @return void
 */
	public function setUp() {
		parent::setUp();
		App::build(array(
			'Plugin' => array(CakePlugin::path('Extensions') . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
			'View' => array(CakePlugin::path('Extensions') . 'Test' . DS . 'test_app' . DS . 'View' . DS),
		), App::PREPEND);
		$this->testPlugin = CakePlugin::path('Extensions') . 'Test' . DS . 'test_files' . DS . 'example_plugin.zip';
		$this->testTheme = CakePlugin::path('Extensions') . 'Test' . DS . 'test_files' . DS . 'example_theme.zip';
		$this->BemmelhorComposer = new BemmelhorComposer();
		$this->BemmelhorComposer->appPath = CakePlugin::path('Extensions') . 'Test' . DS . 'test_app' . DS;
	}

/**
 * tearDown
 *
 * @return void
 */
	public function tearDown() {
		parent::tearDown();
		$path = CakePlugin::path('Extensions') . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'Example';
		$Folder = new Folder($path);
		$Folder->delete();
		$path = CakePlugin::path('Extensions') . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'Minimal';
		$Folder = new Folder($path);
		$Folder->delete();
		$File = new File(CakePlugin::path('Extensions') . 'Test' . DS . 'test_app' . DS . 'composer.json');
		$File->delete();
	}

/**
 * testGetComposer
 *
 * @return void
 */
	public function testGetComposer() {
		$BemmelhorComposer = $this->getMock('BemmelhorComposer', array('_shellExec'));
		$BemmelhorComposer->appPath = $this->BemmelhorComposer->appPath;
		$BemmelhorComposer->expects($this->any())
			->method('_shellExec')
			->with(
				$this->equalTo('curl -s http://getcomposer.org/installer | php -- --install-dir=' . $BemmelhorComposer->appPath)
			)
			->will($this->returnValue(true));
		$BemmelhorComposer->getComposer();
	}

/**
 * testRunComposer
 *
 * @return void
 */
	public function testRunComposer() {
		$BemmelhorComposer = $this->getMock('BemmelhorComposer', array('_shellExec'));
		$BemmelhorComposer->appPath = $this->BemmelhorComposer->appPath;
		$BemmelhorComposer->getComposer();
		$BemmelhorComposer->expects($this->once())
			->method('_shellExec')
			->with(
				$this->equalTo('php ' . $BemmelhorComposer->composerPath . ' install')
			)
			->will($this->returnValue(true));
		$BemmelhorComposer->runComposer();
	}

/**
 * testSetConfig
 *
 * @return void
 */
	public function testSetConfig() {
		$result = $this->BemmelhorComposer->setConfig(array(
			'shama/ftp' => '*',
		));
		$this->assertTrue($result);
		$File = new File($this->BemmelhorComposer->appPath . 'composer.json');
		$result = $File->read();
		$File->close();
		$expected = <<<END
{
\s+"minimum-stability": "dev",
\s+"config": {
\s+"vendor-dir": "Vendor",
\s+"bin-dir": "Vendor/bin"
\s+},
\s+"require": {
\s+"composer/installers": "\*",
\s+"shama/ftp": "\*"
\s+}
}
END;
		$this->assertRegExp($expected, trim($result));
	}
}