Skip to content

Commit 8c197b8

Browse files
committed
Update command added
1 parent 15daeb3 commit 8c197b8

File tree

5 files changed

+252
-0
lines changed

5 files changed

+252
-0
lines changed

Bower/Bower.php

+19
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ public function install(ConfigurationInterface $config, $callback = null)
9292

9393
return $result->getProcess()->getExitCode();
9494
}
95+
96+
/**
97+
* Updates bower dependencies from the given config directory.
98+
*
99+
* @param ConfigurationInterface $config
100+
* @param null $callback
101+
*
102+
* @return int
103+
*/
104+
public function update(ConfigurationInterface $config, $callback = null)
105+
{
106+
$this->eventDispatcher->dispatch(BowerEvents::PRE_UPDATE, new BowerEvent($config));
107+
108+
$result = $this->execCommand($config, array('update'), $callback);
109+
110+
$this->eventDispatcher->dispatch(BowerEvents::POST_UPDATE, new BowerEvent($config));
111+
112+
return $result->getProcess()->getExitCode();
113+
}
95114

96115
/**
97116
* Creates the cache for the dependency mapping.

Bower/Event/BowerEvents.php

+4
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ final class BowerEvents
2323
const PRE_INSTALL = 'bower.pre_install';
2424

2525
const POST_INSTALL = 'bower.post_install';
26+
27+
const PRE_UPDATE = 'bower.pre_update';
28+
29+
const POST_UPDATE = 'bower.post_update';
2630
}

Command/UpdateCommand.php

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the SpBowerBundle package.
5+
*
6+
* (c) Martin Parsiegla <martin.parsiegla@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Sp\BowerBundle\Command;
13+
14+
use Sp\BowerBundle\Bower\Exception\CommandException;
15+
use Sp\BowerBundle\Bower\Exception\RuntimeException;
16+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
use Symfony\Component\Console\Input\InputInterface;
19+
use Symfony\Component\Process\Process;
20+
21+
/**
22+
* @author Jorge Agustín Marisi <agustinmarisi@gmail.com>
23+
*/
24+
class UpdateCommand extends ContainerAwareCommand
25+
{
26+
protected function configure()
27+
{
28+
$this
29+
->setName('sp:bower:update')
30+
->setDescription('Update all bower dependencies.')
31+
->setHelp(<<<EOT
32+
The <info>sp:bower:update</info> command updates bower dependencies for every bundle:
33+
34+
<info>php app/console sp:bower:update</info>
35+
EOT
36+
);
37+
}
38+
39+
protected function execute(InputInterface $input, OutputInterface $output)
40+
{
41+
$bowerManager = $this->getBowerManager();
42+
$bower = $this->getBower();
43+
$callback = function($type, $data) use ($output) {
44+
if (Process::ERR != $type) {
45+
$output->write($data);
46+
}
47+
};
48+
49+
foreach ($bowerManager->getBundles() as $bundle => $configuration) {
50+
$output->writeln(sprintf('Updating bower dependencies for <comment>"%s"</comment> into <comment>"%s"</comment>', $bundle, $configuration->getAssetDirectory()));
51+
try {
52+
$bower->update($configuration, $callback);
53+
} catch (CommandException $ex) {
54+
$output->writeln($ex->getCommandError());
55+
throw new RuntimeException("An error occured while updating dependencies");
56+
}
57+
}
58+
}
59+
60+
/**
61+
* @return \Sp\BowerBundle\Bower\BowerManager
62+
*/
63+
protected function getBowerManager()
64+
{
65+
return $this->getContainer()->get('sp_bower.bower_manager');
66+
}
67+
68+
/**
69+
* @return \Sp\BowerBundle\Bower\Bower
70+
*/
71+
protected function getBower()
72+
{
73+
return $this->getContainer()->get('sp_bower.bower');
74+
}
75+
76+
}

Tests/Bower/BowerTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,31 @@ public function testInstall($configDir)
128128

129129
$this->bower->install($config);
130130
}
131+
132+
/**
133+
* @covers Sp\BowerBundle\Bower\Bower::update
134+
* @dataProvider componentsProvider
135+
*/
136+
public function testUpdate($configDir)
137+
{
138+
$config = new Configuration($configDir);
139+
$this->processBuilder->expects($this->once())->method('setWorkingDirectory')->with($this->equalTo($configDir));
140+
$this->processBuilder->expects($this->once())->method('setTimeout')->with($this->equalTo(600));
141+
$this->processBuilder->expects($this->at(2))->method('add')->with($this->equalTo($this->bin));
142+
$this->processBuilder->expects($this->at(3))->method('add')->with($this->equalTo('update'));
143+
$this->processBuilder->expects($this->once())->method('getProcess')->will($this->returnValue($this->process));
144+
$this->process->expects($this->once())->method('isSuccessful')->will($this->returnValue(true));
145+
$this->eventDispatcher->expects($this->at(0))->method('dispatch')->with($this->equalTo(BowerEvents::PRE_UPDATE));
146+
$this->eventDispatcher->expects($this->at(1))->method('dispatch')->with($this->equalTo(BowerEvents::PRE_EXEC));
147+
$this->eventDispatcher->expects($this->at(2))->method('dispatch')->with($this->equalTo(BowerEvents::POST_EXEC));
148+
$this->eventDispatcher->expects($this->at(3))->method('dispatch')->with($this->equalTo(BowerEvents::POST_UPDATE));
149+
150+
$this->bower->expects($this->once())->method('dumpBowerConfig');
151+
152+
$this->process->expects($this->once())->method('run')->with($this->equalTo(null));
153+
154+
$this->bower->update($config);
155+
}
131156

132157
/**
133158
* @covers Sp\BowerBundle\Bower\Bower::createDependencyMappingCache

Tests/Command/UpdateCommandTest.php

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the SpBowerBundle package.
5+
*
6+
* (c) Martin Parsiegla <martin.parsiegla@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Sp\BowerBundle\Tests\Command;
13+
14+
use Sp\BowerBundle\Command\UpdateCommand;
15+
use Sp\BowerBundle\Bower\Configuration;
16+
use Symfony\Component\Console\Input\InputOption;
17+
use Symfony\Component\Console\Output\NullOutput;
18+
use Symfony\Component\Console\Input\ArrayInput;
19+
20+
/**
21+
* @author Martin Parsiegla <martin.parsiegla@gmail.com>
22+
*/
23+
class UpdateCommandTest extends \PHPUnit_Framework_TestCase
24+
{
25+
private $application;
26+
private $definition;
27+
private $kernel;
28+
private $container;
29+
private $command;
30+
private $bower;
31+
private $bm;
32+
private $helperSet;
33+
34+
/**
35+
* Sets up the fixture, for example, opens a network connection.
36+
* This method is called before a test is executed.
37+
*/
38+
protected function setUp()
39+
{
40+
$this->application = $this->getMockBuilder('Symfony\\Bundle\\FrameworkBundle\\Console\\Application')
41+
->disableOriginalConstructor()
42+
->getMock();
43+
$this->definition = $this->getMockBuilder('Symfony\\Component\\Console\\Input\\InputDefinition')
44+
->disableOriginalConstructor()
45+
->getMock();
46+
$this->kernel = $this->getMock('Symfony\\Component\\HttpKernel\\KernelInterface');
47+
$this->helperSet = $this->getMock('Symfony\\Component\\Console\\Helper\\HelperSet');
48+
$this->container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
49+
$this->bm = $this->getMockBuilder('Sp\\BowerBundle\\Bower\\BowerManager')
50+
->disableOriginalConstructor()
51+
->getMock();
52+
$this->bower = $this->getMockBuilder('Sp\\BowerBundle\\Bower\\Bower')
53+
->disableOriginalConstructor()
54+
->getMock();
55+
56+
$this->application->expects($this->any())
57+
->method('getDefinition')
58+
->will($this->returnValue($this->definition));
59+
$this->definition->expects($this->any())
60+
->method('getArguments')
61+
->will($this->returnValue(array()));
62+
$this->definition->expects($this->any())
63+
->method('getOptions')
64+
->will($this->returnValue(array(
65+
new InputOption('--verbose', '-v', InputOption::VALUE_NONE, 'Increase verbosity of messages.'),
66+
new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', 'dev'),
67+
new InputOption('--no-debug', null, InputOption::VALUE_NONE, 'Switches off debug mode.'),
68+
)));
69+
70+
$this->application->expects($this->any())
71+
->method('getKernel')
72+
->will($this->returnValue($this->kernel));
73+
74+
$this->application->expects($this->once())
75+
->method('getHelperSet')
76+
->will($this->returnValue($this->helperSet));
77+
78+
$this->kernel->expects($this->any())
79+
->method('getContainer')
80+
->will($this->returnValue($this->container));
81+
82+
$this->container->expects($this->at(0))
83+
->method('get')
84+
->with('sp_bower.bower_manager')
85+
->will($this->returnValue($this->bm));
86+
87+
$this->container->expects($this->at(1))
88+
->method('get')
89+
->with('sp_bower.bower')
90+
->will($this->returnValue($this->bower));
91+
92+
$this->command = new UpdateCommand();
93+
$this->command->setApplication($this->application);
94+
}
95+
96+
public function testEmptyBowerManager()
97+
{
98+
$this->bm->expects($this->once())
99+
->method('getBundles')
100+
->will($this->returnValue(array()));
101+
102+
$this->command->run(new ArrayInput(array()), new NullOutput());
103+
}
104+
105+
public function testUpdate()
106+
{
107+
$configuration = new Configuration('/foo');
108+
$configuration->setAssetDirectory('/test');
109+
$configuration->setJsonFile('foo.json');
110+
111+
$barConfig = new Configuration('/bar');
112+
$bundles = array(
113+
'DemoBundle' => $configuration,
114+
'AcmeBundle' => $barConfig
115+
);
116+
117+
$this->bower->expects($this->at(0))->method('update')->with($this->equalTo($configuration));
118+
119+
$this->bower->expects($this->at(1))->method('update')->with($this->equalTo($barConfig));
120+
121+
122+
$this->bm->expects($this->once())
123+
->method('getBundles')
124+
->will($this->returnValue($bundles));
125+
126+
$this->command->run(new ArrayInput(array()), new NullOutput());
127+
}
128+
}

0 commit comments

Comments
 (0)