]> git.agnieray.net Git - galette.git/commitdiff
Handle sequence on PostgreSQL for payment types; refs #1374 refs #1158
authorJohan Cwiklinski <jcwiklinski@teclib.com>
Sun, 15 Dec 2019 14:26:06 +0000 (15:26 +0100)
committerJohan Cwiklinski <jcwiklinski@teclib.com>
Tue, 17 Dec 2019 23:40:44 +0000 (00:40 +0100)
Add unit tests

galette/lib/Galette/Entity/PaymentType.php
galette/lib/Galette/Repository/PaymentTypes.php
tests/Galette/Entity/tests/units/PaymentType.php [new file with mode: 0644]
tests/Galette/Repository/tests/units/PaymentTypes.php [new file with mode: 0644]

index 836f7954da6f9b226b5485aa63bb3585f38ab9c7..9335ead88f10daebb83d8ea0d66a450fb13aeca2 100644 (file)
@@ -58,6 +58,7 @@ use Zend\Db\Sql\Expression;
 class PaymentType
 {
     use TranslatableTrait;
+    use I18nTrait;
 
     const TABLE = 'paymenttypes';
     const PK = 'type_id';
@@ -142,23 +143,8 @@ class PaymentType
         try {
             if ($this->id !== null && $this->id > 0) {
                 if ($this->old_name !== null) {
-                    $deleted = \deleteDynamicTranslation($this->old_name);
-                    if ($deleted === false) {
-                        $this->warnings[] = str_replace(
-                            '%type',
-                            $this->old_name,
-                            _T('Unable to remove old translation for %type :(')
-                        );
-                    }
-
-                    $added = \addDynamicTranslation($this->name);
-                    if ($added === false) {
-                        $this->warnings[] = str_replace(
-                            '%type',
-                            $this->name,
-                            _T('Unable to add translation for %type :(')
-                        );
-                    }
+                    $this->deleteTranslation($this->old_name);
+                    $this->addTranslation($this->name);
                 }
 
                 $update = $this->zdb->update(self::TABLE);
@@ -174,14 +160,12 @@ class PaymentType
                     Analog::log('Not stored!', Analog::ERROR);
                     return false;
                 }
-                $added = \addDynamicTranslation($this->name);
-                if ($added === false) {
-                    $this->warnings[] = str_replace(
-                        '%type',
-                        $this->name,
-                        _T('Unable to add translation for %type :(')
-                    );
-                }
+
+                $this->id = (int)$this->zdb->driver->getLastGeneratedValue(
+                    PREFIX_DB . self::TABLE . '_id_seq'
+                );
+
+                $this->addTranslation($this->name);
             }
             return true;
         } catch (\Exception $e) {
@@ -212,15 +196,13 @@ class PaymentType
                 self::PK . ' = ' . $id
             );
             $this->zdb->execute($delete);
-            \deleteDynamicTranslation($this->name);
+            $this->deleteTranslation($this->name);
             Analog::log(
                 'Payment type #' . $id . ' (' . $this->name
                 . ') deleted successfully.',
                 Analog::INFO
             );
             return true;
-        } catch (\RuntimeException $re) {
-            throw $re;
         } catch (\Exception $e) {
             Analog::log(
                 'Unable to delete payment type ' . $id . ' | ' . $e->getMessage(),
index 1f620dac6bdb5a15da2c0220c3d56d7108ec9a9b..f1fe444c009ac47373004047eaab1b4a5b2775b8 100644 (file)
@@ -37,6 +37,7 @@
 namespace Galette\Repository;
 
 use Analog\Analog;
+use Zend\Db\Sql\Expression;
 use Galette\Entity\PaymentType;
 
 /**
@@ -135,6 +136,11 @@ class PaymentTypes extends Repository
                 //first, we drop all values
                 $delete = $this->zdb->delete($ent::TABLE);
                 $this->zdb->execute($delete);
+
+                $this->zdb->handleSequence(
+                    $ent::TABLE,
+                    count($this->defaults)
+                );
                 $this->insert($ent::TABLE, $this->defaults);
 
                 $this->zdb->connection->commit();
diff --git a/tests/Galette/Entity/tests/units/PaymentType.php b/tests/Galette/Entity/tests/units/PaymentType.php
new file mode 100644 (file)
index 0000000..e206b0a
--- /dev/null
@@ -0,0 +1,184 @@
+<?php
+
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
+/**
+ * Payment type tests
+ *
+ * PHP version 5
+ *
+ * Copyright © 2019 The Galette Team
+ *
+ * This file is part of Galette (http://galette.tuxfamily.org).
+ *
+ * Galette is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Galette is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Galette. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category  Repository
+ * @package   GaletteTests
+ *
+ * @author    Johan Cwiklinski <johan@x-tnd.be>
+ * @copyright 2019 The Galette Team
+ * @license   http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
+ * @version   SVN: $Id$
+ * @link      http://galette.tuxfamily.org
+ * @since     2019-12-15
+ */
+
+namespace Galette\Entity\test\units;
+
+use \atoum;
+
+/**
+ * Payment type tests
+ *
+ * @category  Entity
+ * @name      PaymentType
+ * @package   GaletteTests
+ * @author    Johan Cwiklinski <johan@x-tnd.be>
+ * @copyright 2019 The Galette Team
+ * @license   http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
+ * @link      http://galette.tuxfamily.org
+ * @since     2019-12-15
+ */
+class PaymentType extends atoum
+{
+    private $zdb;
+    private $preferences;
+    private $session;
+    private $login;
+    private $remove = [];
+    private $i18n;
+
+    /**
+     * Set up tests
+     *
+     * @param string $testMethod Calling method
+     *
+     * @return void
+     */
+    public function beforeTestMethod($testMethod)
+    {
+        $this->zdb = new \Galette\Core\Db();
+        $this->preferences = new \Galette\Core\Preferences($this->zdb);
+        $this->i18n = new \Galette\Core\I18n(
+            \Galette\Core\I18n::DEFAULT_LANG
+        );
+        $this->session = new \RKA\Session();
+        $this->login = new \Galette\Core\Login($this->zdb, $this->i18n, $this->session);
+
+        $types = new \Galette\Repository\PaymentTypes($this->zdb, $this->preferences, $this->login);
+        $res = $types->installInit(false);
+        $this->boolean($res)->isTrue();
+    }
+
+    /**
+     * Tear down tests
+     *
+     * @param string $testMethod Calling method
+     *
+     * @return void
+     */
+    public function afterTestMethod($testMethod)
+    {
+        $this->deletePaymentType();
+    }
+
+    /**
+     * Delete payment type
+     *
+     * @return void
+     */
+    private function deletePaymentType()
+    {
+        if (is_array($this->remove) && count($this->remove) > 0) {
+            $delete = $this->zdb->delete(\Galette\Entity\PaymentType::TABLE);
+            $delete->where->in(\Galette\Entity\PaymentType::PK, $this->remove);
+            $this->zdb->execute($delete);
+        }
+
+        //Clean logs
+        $this->zdb->db->query(
+            'TRUNCATE TABLE ' . PREFIX_DB . \Galette\Core\History::TABLE,
+            \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
+        );
+    }
+
+    /**
+     * Test payment type
+     *
+     * @return void
+     */
+    public function testPaymentType()
+    {
+        global $i18n; // globals :(
+        $i18n = $this->i18n;
+
+        $type = new \Galette\Entity\PaymentType($this->zdb);
+
+        $type->name = 'Test payment type';
+        $this->boolean($type->store())->isTrue();
+
+        $select = $this->zdb->select(\Galette\Core\L10n::TABLE);
+        $select->where(
+            array(
+                'text_orig'     => 'Test payment type'
+            )
+        );
+        $results = $this->zdb->execute($select);
+        $result = $results->current();
+
+        $this->array((array)$result)
+            ->string['text_orig']->isIdenticalTo('Test payment type');
+
+        $id = $type->id;
+        $this->remove[] = $id;
+
+        $type = new \Galette\Entity\PaymentType($this->zdb, $id);
+        $type->name = 'Changed test payment type';
+        $this->boolean($type->store())->isTrue();
+
+        $type = new \Galette\Entity\PaymentType($this->zdb, $id);
+        $this->string($type->getName())->isIdenticalTo('Changed test payment type');
+
+        $select = $this->zdb->select(\Galette\Core\L10n::TABLE);
+        $select->where(
+            array(
+                'text_orig'     => 'Changed test payment type'
+            )
+        );
+        $results = $this->zdb->execute($select);
+        $this->integer(count($results))->isIdenticalTo(8);
+
+        $type = new \Galette\Entity\PaymentType($this->zdb, \Galette\Entity\PaymentType::CASH);
+        $this->exception(
+            function () use ($type) {
+                $type->remove();
+            }
+        )
+            ->hasMessage('You cannot delete system payment types!')
+            ->isInstanceOf('\RuntimeException');
+
+        $type = new \Galette\Entity\PaymentType($this->zdb, $id);
+        $this->boolean($type->remove())->isTrue();
+
+        $select = $this->zdb->select(\Galette\Core\L10n::TABLE);
+        $select->where(
+            array(
+                'text_orig'     => 'Test payment type'
+            )
+        );
+        $results = $this->zdb->execute($select);
+        $this->integer($results->count())->isIdenticalTo(0);
+    }
+}
diff --git a/tests/Galette/Repository/tests/units/PaymentTypes.php b/tests/Galette/Repository/tests/units/PaymentTypes.php
new file mode 100644 (file)
index 0000000..27eac57
--- /dev/null
@@ -0,0 +1,154 @@
+<?php
+
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
+/**
+ * Payment types repository tests
+ *
+ * PHP version 5
+ *
+ * Copyright © 2019 The Galette Team
+ *
+ * This file is part of Galette (http://galette.tuxfamily.org).
+ *
+ * Galette is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Galette is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Galette. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category  Repository
+ * @package   GaletteTests
+ *
+ * @author    Johan Cwiklinski <johan@x-tnd.be>
+ * @copyright 2019 The Galette Team
+ * @license   http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
+ * @version   SVN: $Id$
+ * @link      http://galette.tuxfamily.org
+ * @since     2019-12-17
+ */
+
+namespace Galette\Repository\test\units;
+
+use \atoum;
+
+/**
+ * Payment types repository tests
+ *
+ * @category  Repository
+ * @name      PaymentTypes
+ * @package   GaletteTests
+ * @author    Johan Cwiklinski <johan@x-tnd.be>
+ * @copyright 2019 The Galette Team
+ * @license   http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
+ * @link      http://galette.tuxfamily.org
+ * @since     2019-12-17
+ */
+class PaymentTypes extends atoum
+{
+    private $zdb;
+    private $preferences;
+    private $session;
+    private $login;
+    private $remove = [];
+    private $i18n;
+
+    /**
+     * Set up tests
+     *
+     * @param string $testMethod Calling method
+     *
+     * @return void
+     */
+    public function beforeTestMethod($testMethod)
+    {
+        $this->zdb = new \Galette\Core\Db();
+        $this->preferences = new \Galette\Core\Preferences($this->zdb);
+        $this->i18n = new \Galette\Core\I18n(
+            \Galette\Core\I18n::DEFAULT_LANG
+        );
+        $this->session = new \RKA\Session();
+        $this->login = new \Galette\Core\Login($this->zdb, $this->i18n, $this->session);
+
+        $types = new \Galette\Repository\PaymentTypes($this->zdb, $this->preferences, $this->login);
+        $res = $types->installInit(false);
+        $this->boolean($res)->isTrue();
+    }
+
+    /**
+     * Tear down tests
+     *
+     * @param string $testMethod Calling method
+     *
+     * @return void
+     */
+    public function afterTestMethod($testMethod)
+    {
+        $this->deletePaymentType();
+    }
+
+    /**
+     * Delete payment type
+     *
+     * @return void
+     */
+    private function deletePaymentType()
+    {
+        if (is_array($this->remove) && count($this->remove) > 0) {
+            $delete = $this->zdb->delete(\Galette\Entity\PaymentType::TABLE);
+            $delete->where->in(\Galette\Repository\PaymentTypes::PK, $this->remove);
+            $this->zdb->execute($delete);
+        }
+
+        //Clean logs
+        $this->zdb->db->query(
+            'TRUNCATE TABLE ' . PREFIX_DB . \Galette\Core\History::TABLE,
+            \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
+        );
+    }
+
+    /**
+     * Test getList
+     *
+     * @return void
+     */
+    public function testGetList()
+    {
+        $types = new \Galette\Repository\PaymentTypes($this->zdb, $this->preferences, $this->login);
+
+        $list = $types->getList();
+        $this->array($list)->hasSize(6);
+
+        if ($this->zdb->isPostgres()) {
+            $select = $this->zdb->select(\Galette\Entity\PaymentType::TABLE . '_id_seq');
+            $select->columns(['last_value']);
+            $results = $this->zdb->execute($select);
+            $result = $results->current();
+            $this->integer($result->last_value)->isGreaterThanOrEqualTo(6, 'Incorrect payments types sequence');
+        }
+
+        //reinstall payment types
+        $types->installInit();
+
+        $list = $types->getList();
+        $this->array($list)->hasSize(6);
+
+        if ($this->zdb->isPostgres()) {
+            $select = $this->zdb->select(\Galette\Entity\PaymentType::TABLE . '_id_seq');
+            $select->columns(['last_value']);
+            $results = $this->zdb->execute($select);
+            $result = $results->current();
+            $this->integer($result->last_value)->isGreaterThanOrEqualTo(
+                6,
+                'Incorrect payment types sequence ' . $result->last_value
+            );
+        }
+    }
+}