diff --git a/library/Class/Migration/UpdateDatabaseAfterSelectDb.php b/library/Class/Migration/UpdateDatabaseAfterSelectDb.php index 8c91da1def8a7abec7d84df6ef4f826db5e8894f..545810e5e890fedb7e23b4ef57c8ae81b8a40fcf 100644 --- a/library/Class/Migration/UpdateDatabaseAfterSelectDb.php +++ b/library/Class/Migration/UpdateDatabaseAfterSelectDb.php @@ -24,7 +24,14 @@ class Class_Migration_UpdateDatabaseAfterSelectDb { use Trait_EchoError; - protected $_adapter; + protected static $_default_adapter; + + + /** @category testing */ + public static function setAdapter($adapter) { + static::$_default_adapter = $adapter; + } + public function run() { //reload config files @@ -32,10 +39,8 @@ class Class_Migration_UpdateDatabaseAfterSelectDb { ->warmUpFiles() ->warmUpDB(); - $this->_adapter = Zend_Db_Table::getDefaultAdapter(); - $this->echoStartTitle(sprintf('Début de l\'étape de mise à jour des variables dans la base de donnée : %s', - array_at('dbname', $this->_adapter->getConfig()))); + array_at('dbname', $this->_adapter()->getConfig()))); $this->echoError('Mise à jour des variables de Cosmogramme' . "\n"); @@ -59,7 +64,7 @@ class Class_Migration_UpdateDatabaseAfterSelectDb { try { $this->echoError('Mise à jour du mysql.proc.' . "\n"); - $this->_adapter->query("update mysql.proc set definer='root@localhost'"); + $this->_adapter()->query("update mysql.proc set definer='root@localhost'"); } catch (Exception $e) { $this->echoError('Échec de la mise à jour du mysql.proc' . "\n" . $e->getMessage() @@ -70,7 +75,7 @@ class Class_Migration_UpdateDatabaseAfterSelectDb { try { $this->echoError('Suppression du trigger datemaj_notices_update' . "\n"); - $this->_adapter->query("drop trigger datemaj_notices_update;"); + $this->_adapter()->query("drop trigger datemaj_notices_update;"); } catch (Exception $e) { $this->echoError($e->getMessage() . "\n"); } @@ -84,7 +89,12 @@ class Class_Migration_UpdateDatabaseAfterSelectDb { protected function adminVar(string $key, string $value) : self { - $this->_adapter->query('replace into bib_admin_var (clef, valeur) values ("' . $key . '", "' . $value . '")'); + $this->_adapter()->query('replace into bib_admin_var (clef, valeur) values ("' . $key . '", "' . $value . '")'); return $this; } + + + protected function _adapter() { + return static::$_default_adapter ?? Zend_Db_Table::getDefaultAdapter(); + } } diff --git a/tests/library/Class/Migration/UpdateDatabaseAfterSelectDbTest.php b/tests/library/Class/Migration/UpdateDatabaseAfterSelectDbTest.php index d66bd4332611da55dd3640ed743f6a4293af5d7f..1475d3de89ee6a3dabfc4ec990009f588f66c1d2 100644 --- a/tests/library/Class/Migration/UpdateDatabaseAfterSelectDbTest.php +++ b/tests/library/Class/Migration/UpdateDatabaseAfterSelectDbTest.php @@ -21,11 +21,11 @@ class UpdateDatabaseAfterSelectDbTest extends ModelTestCase { - protected $_storm_default_to_volatile = true; - + protected array $_queries = []; public function setUp() { parent::setUp(); + Class_CosmoVar::setValueOf('cache_path', './prod/cache/path/'); Class_CosmoVar::setValueOf('log_path', './prod/log/path/'); Class_CosmoVar::setValueOf('ftp_path', './prod/ftp/path/'); @@ -37,10 +37,25 @@ class UpdateDatabaseAfterSelectDbTest extends ModelTestCase { Class_AdminVar::set('STATUS_REPORT_PUSH_URL', 'https://git-bokeh.org'); Class_AdminVar::set('FORCE_HTTPS', '1'); + $adapter = $this->mock() + ->whenCalled('getConfig') + ->answers(['dbname' => 'testing']) + + ->whenCalled('query') + ->willDo(fn($query) => $this->_queries[] = $query); + + Class_Migration_UpdateDatabaseAfterSelectDb::setAdapter($adapter); + (new Class_Migration_UpdateDatabaseAfterSelectDb)->runWithoutEcho(); } + public function tearDown() { + Class_Migration_UpdateDatabaseAfterSelectDb::setAdapter(null); + parent::tearDown(); + } + + public function expectedCosmoVarValuesForKey() { return [['./cosmogramme/fichiers/cache/', 'cache_path'], ['./cosmogramme/fichiers/log/', 'log_path'], @@ -54,27 +69,21 @@ class UpdateDatabaseAfterSelectDbTest extends ModelTestCase { /** - * @test - * @dataProvider expectedCosmoVarValuesForKey - */ + * @test + * @dataProvider expectedCosmoVarValuesForKey + */ public function cosmoVarValuesShouldHaveChange($value, $key) { $this->assertEquals($value, Class_CosmoVar::getValueOf($key)); } - public function expectedAdminVarValuesForKey() { - return [['0', 'ENABLE_COLLABORATIVE_BROWSING'], - ['no', 'STATUS_REPORT_PUSH_URL'], - ['0', 'FORCE_HTTPS'] - ]; - } - - - /** - * @test - * @dataProvider expectedAdminVarValuesForKey - */ - public function adminVarValuesShouldHaveChange($value, $key) { - $this->assertEquals($value, Class_AdminVar::get($key)); + /** @test */ + public function adapterShouldHaveRunQueries() { + $this->assertEquals(['replace into bib_admin_var (clef, valeur) values ("ENABLE_COLLABORATIVE_BROWSING", "0")', + 'replace into bib_admin_var (clef, valeur) values ("STATUS_REPORT_PUSH_URL", "no")', + 'replace into bib_admin_var (clef, valeur) values ("FORCE_HTTPS", "0")', + "update mysql.proc set definer='root@localhost'", + 'drop trigger datemaj_notices_update;'], + $this->_queries); } }