Skip to content
Snippets Groups Projects
Commit c180e685 authored by Ghislain Loas's avatar Ghislain Loas
Browse files

refacto

parent 4bc07f8c
Branches
Tags
3 merge requests!1659Master,!1639Dev#42624 skin update button,!1638Dev#42624 skin update button
......@@ -41,13 +41,11 @@ class Class_Profil_SkinUpdateReader {
public function askGitPull() {
$json = [];
$data = [];
foreach($this->getUpdatableSkins() as $skin)
$json[$skin->getLabel()] = ['UpdateTime' => $this->_('En attente d\'execution de la commande "%s" depuis le %s', static::$command,
static::getCurrentDateTime())];
$data[$skin->getLabel()] = ['Status' => $this->_('En attente depuis le %s', static::getCurrentDateTime())];
$json = json_encode($json);
return static::getFileWriter()->putContents(static::getLogPath(), $json);
return $this->_writeInLog($data, true);
}
......@@ -57,10 +55,10 @@ class Class_Profil_SkinUpdateReader {
$data = [];
foreach($this->getUpdatableSkins() as $skin) {
$data[$skin->getLabel()] = ['UpdateTime' => $this->_runGitPullIn($skin->getLabel())];
$data[$skin->getLabel()] = ['Status' => $this->_runGitPullIn($skin->getLabel())];
}
return $this->_writeInLog($data);
return $this->_writeInLog($data, false);
}
......@@ -69,14 +67,14 @@ class Class_Profil_SkinUpdateReader {
}
public function getUpdateTime($folder) {
public function getStatus($folder) {
if(!$folder || !($json = $this->getJson()))
return '';
if(!isset($json[$folder]))
return '';
return $json[$folder]['UpdateTime'];
return $json[$folder]['Status'];
}
......@@ -84,36 +82,38 @@ class Class_Profil_SkinUpdateReader {
$skins = [];
foreach((new Class_Profil())->getUpdatableSkins() as $skin_label)
$skins[] = (new Class_Entity())->updateAttributes(['Label' => $skin_label,
'UpdateTime' => $this->getUpdateTime($skin_label)]);
'Status' => $this->getStatus($skin_label)]);
return $skins;
}
protected function _runGitPullIn($folder) {
$commands = ['cd ' . static::getSkinsPath($folder),
static::$command,
'cd ' . ROOT_PATH];
foreach($commands as $command) {
$result = '';
$last_line = $this->getCommand()->system($command, $result);
if(!$last_line)
return $this->_('La commande a échoué : %s %s %s', $command, $last_line, $result);
}
$commands = ['cd ' . static::getSkinsPath($folder) . ' 2>&1',
static::$command . ' 2>&1',
'cd ' . ROOT_PATH . ' 2>&1'];
$command = implode(' && ', $commands);
$runner = $this->getCommand();
$runner->exec($command);
$output = '<br>' . implode('<br>', $runner->getOutput());
if(0 !== $runner->getReturnVar())
return $this->_('La commande %s a échoué : %s', $command, $output);
return static::getCurrentDateTime();
return static::getCurrentDateTime() . $output;
}
protected function _writeInLog($data) {
protected function _writeInLog($data, $should_run) {
$data['should_run'] = $should_run;
return static::getFileWriter()->putContents(static::getLogPath(), json_encode($data));
}
protected function _shouldRun() {
foreach($this->getJson() as $skin_folder => $update_time)
return false !== strpos($this->getUpdateTime($skin_folder), static::$command);
return false;
return $this->getJson()['should_run'];
}
}
?>
\ No newline at end of file
......@@ -21,9 +21,27 @@
class Class_Testing_Command extends Class_Testing_Abstract {
protected $_known_functions = ['exec',
'passthru',
'shell_exec',
'system'];
protected $_output,
$_return_var;
public function exec($command) {
$output = [];
$return_var = '';
$result = exec($command, $output, $return_var);
$this->_output = $output;
$this->_return_var = $return_var;
return $result;
}
public function getOutput() {
return $this->_output;
}
public function getReturnVar() {
return $this->_return_var;
}
}
?>
\ No newline at end of file
......@@ -30,7 +30,7 @@ class ZendAfi_View_Helper_Admin_UpdateSkins extends ZendAfi_View_Helper_BaseHelp
$html = $this->view->bouton('id=789789',
'picto=add',
'texte=' . $this->_('Envoyer une demande de mise à jour'),
'texte=' . $this->_('Demander la mise à jour'),
'url=' . $this->view->url(['git' => 'pull']),
'largeur=initial');
......@@ -44,13 +44,13 @@ class ZendAfi_View_Helper_Admin_UpdateSkins extends ZendAfi_View_Helper_BaseHelp
protected function _renderSkin($skin) {
$header = $this->_tag('h2', $this->_('Dossier : %s', $skin->getLabel()));
$header = $this->_tag('h2', $this->_('Thème %s', $skin->getLabel()));
$update_time = ($update_time = $skin->getUpdateTime())
$update_time = ($update_time = $skin->getStatus())
? $update_time
: $this->_('inconnue');
: $this->_('inconnu');
$definitions = $this->_tag('dt', $this->_('Date et heure de la dernière mise à jour : '))
$definitions = $this->_tag('dt', $this->_('Statut : '))
. $this->_tag('dd', $update_time);
$content = $this->_tag('dl', $definitions);
......
......@@ -38,11 +38,11 @@ abstract class IndexControllerUpdateSkinTestCase extends Admin_AbstractControlle
$file_writer
->whenCalled('getContents')
->with(Class_Profil_SkinUpdateReader::getLogPath())
->answers(json_encode(['Valence' => ['UpdateTime' => '25/04/2016 15:01:37']]))
->answers(json_encode(['Valence' => ['Status' => '25/04/2016 15:01:37']]))
->whenCalled('putContents')
->with(Class_Profil_SkinUpdateReader::getLogPath(),
'{"Valence":{"UpdateTime":"En attente d\'execution de la commande \"git pull --rebase\" depuis le 2016-05-02 12:30:00"}}')
'{"Valence":{"Status":"En attente depuis le 2016-05-02 12:30:00"},"should_run":true}')
->answers(true)
->beStrict();
......@@ -67,7 +67,7 @@ class IndexControllerUpdateSkinDispatchTest extends IndexControllerUpdateSkinTes
/** @test */
public function listShouldContainsSkinValence() {
$this->assertXPathContentContains('//ul/li/h2', 'Dossier : Valence');
$this->assertXPathContentContains('//ul/li/h2', 'Thème Valence');
}
......
......@@ -41,28 +41,27 @@ class Class_SkinUpdateReaderTest extends Storm_Test_ModelTestCase {
Class_Profil_SkinUpdateReader::setCommand($command);
$command
->whenCalled('system')
->with('git pull --rebase', '', '')
->answers(true)
->whenCalled('exec')
->with('cd ' . Class_Profil_SkinUpdateReader::getSkinsPath('Valence') . ' 2>&1 && git pull --rebase 2>&1 && cd ' . ROOT_PATH . ' 2>&1')
->answers('')
->whenCalled('system')
->with('cd ' . Class_Profil_SkinUpdateReader::getSkinsPath('Valence'), '', '')
->answers(true)
->whenCalled('getOutput')
->answers([])
->whenCalled('system')
->with('cd ' . ROOT_PATH, '', '')
->answers(true)
->whenCalled('getReturnVar')
->answers(0)
->beStrict();
$file_writer
->whenCalled('getContents')
->with(Class_Profil_SkinUpdateReader::getLogPath())
->answers(json_encode(['Valence' => ['UpdateTime' => '2016-05-02 12:30:00']]))
->answers(json_encode(['Valence' => ['Status' => '2016-05-02 12:30:00'],
'should_run' => true]))
->whenCalled('putContents')
->with(Class_Profil_SkinUpdateReader::getLogPath(),
'{"Valence":{"UpdateTime":"2016-05-02 12:30:00"}}')
'{"Valence":{"Status":"2016-05-02 12:30:00<br>"},"should_run":false}')
->answers(true)
->beStrict();
......@@ -74,6 +73,6 @@ class Class_SkinUpdateReaderTest extends Storm_Test_ModelTestCase {
/** @test */
public function valenceShouldHaveBeenUpdated() {
$this->assertEquals('2016-05-02 12:30:00', $this->_reader->getUpdateTime('Valence'));
$this->assertEquals('2016-05-02 12:30:00', $this->_reader->getStatus('Valence'));
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment