]> git.agnieray.net Git - galette.git/blobdiff - tests/Galette/IO/tests/units/CsvIn.php
Properly use injection
[galette.git] / tests / Galette / IO / tests / units / CsvIn.php
index 254157542a2ef4d4ea5f2f0287e19f042343f26d..5b27481713c057f368af3821dfbfe5f1ba77514b 100644 (file)
@@ -7,7 +7,7 @@
  *
  * PHP version 5
  *
- * Copyright © 2020 The Galette Team
+ * Copyright © 2020-2023 The Galette Team
  *
  * This file is part of Galette (http://galette.tuxfamily.org).
  *
  * @package   GaletteTests
  *
  * @author    Johan Cwiklinski <johan@x-tnd.be>
- * @copyright 2020 The Galette Team
+ * @copyright 2020-2023 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     2020-05-11
  */
 
 namespace Galette\IO\test\units;
 
-use atoum;
+use PHPUnit\Framework\TestCase;
 use Galette\Entity\Adherent;
 use Galette\DynamicFields\DynamicField;
 use Galette\GaletteTestCase;
@@ -49,38 +48,34 @@ use Galette\GaletteTestCase;
  * @name      CsvIn
  * @package   GaletteTests
  * @author    Johan Cwiklinski <johan@x-tnd.be>
- * @copyright 2020 The Galette Team
+ * @copyright 2020-2023 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     2020-05-11
  */
 class CsvIn extends GaletteTestCase
 {
-    private $contents_table = null;
+    private ?string $contents_table = null;
 
     /**
      * Set up tests
      *
-     * @param string $method Calling method
-     *
      * @return void
      */
-    public function beforeTestMethod($method)
+    public function setUp(): void
     {
-        parent::beforeTestMethod($method);
+        parent::setUp();
         $this->contents_table = null;
     }
 
     /**
      * Tear down tests
      *
-     * @param string $method Calling method
-     *
      * @return void
      */
-    public function afterTestMethod($method)
+    public function tearDown(): void
     {
-        parent::afterTestMethod($method);
+        parent::tearDown();
 
         $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
         $this->zdb->execute($delete);
@@ -135,8 +130,9 @@ class CsvIn extends GaletteTestCase
 
         $members = new \Galette\Repository\Members();
         $list = $members->getList();
-        $this->integer($list->count())->isIdenticalTo(
+        $this->assertSame(
             $count_before,
+            $list->count(),
             print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1), true)
         );
 
@@ -144,17 +140,29 @@ class CsvIn extends GaletteTestCase
 
         //get csv model file to add data in
         $controller = new \Galette\Controllers\CsvController($this->container);
-        $response = $controller->getImportModel($this->request, $this->response);
+        $this->container->injectOn($controller);
+
+        $rfactory = new \Slim\Psr7\Factory\RequestFactory();
+        $request = $rfactory->createRequest('GET', 'http://localhost/models/csv');
+        $response = new \Slim\Psr7\Response();
+
+        $response = $controller->getImportModel($request, $response);
         $csvin = new \Galette\IO\CsvIn($this->container->get('zdb'));
 
-        $this->integer($response->getStatusCode())->isIdenticalTo(200);
-        $this->array($response->getHeaders())
-            ->array['Content-Type']->isIdenticalTo(['text/csv'])
-            ->array['Content-Disposition']->isIdenticalTo(['attachment;filename="galette_import_model.csv"']);
+        $this->assertSame(200, $response->getStatusCode());
+        $headers = $response->getHeaders();
+        $this->assertIsArray($headers);
+        $this->assertSame(['text/csv'], $headers['Content-Type']);
+        $this->assertSame(
+            ['attachment;filename="galette_import_model.csv"'],
+            $headers['Content-Disposition']
+        );
 
         $csvfile_model = $response->getBody()->__toString();
-        $this->string($csvfile_model)
-             ->isIdenticalTo("\"" . implode("\";\"", $fields) . "\"\r\n");
+        $this->assertSame(
+            "\"" . implode("\";\"", $fields) . "\"\r\n",
+            $csvfile_model
+        );
 
         $contents = $csvfile_model;
         foreach ($members_list as $member) {
@@ -166,38 +174,38 @@ class CsvIn extends GaletteTestCase
         }
 
         $path = GALETTE_CACHE_DIR . $file_name;
-        $this->integer(file_put_contents($path, $contents));
+        $this->assertIsInt(file_put_contents($path, $contents));
         $_FILES['new_file'] = [
             'error' => UPLOAD_ERR_OK,
             'name'      => $file_name,
             'tmp_name'  => $path,
             'size'      => filesize($path)
         ];
-        $this->boolean($csvin->store($_FILES['new_file'], true))->isTrue();
-        $this->boolean(file_exists($csvin->getDestDir() . $csvin->getFileName()))->isTrue();
+        $this->assertTrue($csvin->store($_FILES['new_file'], true));
+        $this->assertTrue(file_exists($csvin->getDestDir() . $csvin->getFileName()));
 
         $post = [
             'import_file'   => $file_name
         ];
 
-        $request = clone $this->request;
+        $request = clone $request;
         $request = $request->withParsedBody($post);
 
-        $response = $controller->doImports($request, $this->response);
-        $this->integer($response->getStatusCode())->isIdenticalTo(301);
-        $this->array($this->flash_data['slimFlash'])->isIdenticalTo($flash_messages);
+        $response = $controller->doImports($request, $response);
+        $this->assertSame(301, $response->getStatusCode());
+        $this->assertSame($flash_messages, $this->flash_data['slimFlash']);
         $this->flash->clearMessages();
 
         $members = new \Galette\Repository\Members();
         $list = $members->getList();
-        $this->integer($list->count())->isIdenticalTo($count_after);
+        $this->assertSame($count_after, $list->count());
 
         if ($count_before != $count_after) {
             foreach ($list as $member) {
                 $created = $members_list[$member->fingerprint];
                 foreach ($fields as $field) {
                     if (property_exists($member, $field)) {
-                        $this->variable($member->$field)->isEqualTo($created[$field]);
+                        $this->assertEquals($created[$field], $member->$field);
                     } else {
                         //manage dynamic fields
                         $matches = [];
@@ -218,7 +226,10 @@ class CsvIn extends GaletteTestCase
                                 $expected[0]['text_val'] = $values[$created[$field]];
                             }
 
-                            $this->array($adh->getDynamicFields()->getValues($matches[1]))->isEqualTo($expected);
+                            $this->assertEquals(
+                                $expected,
+                                $adh->getDynamicFields()->getValues($matches[1])
+                            );
                         } else {
                             throw new \RuntimeException("Unknown field $field");
                         }
@@ -272,11 +283,11 @@ class CsvIn extends GaletteTestCase
     protected function getModel($fields): \Galette\Entity\ImportModel
     {
         $model = new \Galette\Entity\ImportModel();
-        $this->boolean($model->remove($this->zdb))->isTrue();
+        $this->assertTrue($model->remove($this->zdb));
 
-        $this->object($model->setFields($fields))->isInstanceOf('Galette\Entity\ImportModel');
-        $this->boolean($model->store($this->zdb))->isTrue();
-        $this->boolean($model->load())->isTrue();
+        $this->assertInstanceOf(\Galette\Entity\ImportModel::class, $model->setFields($fields));
+        $this->assertTrue($model->store($this->zdb));
+        $this->assertTrue($model->load());
         return $model;
     }
 
@@ -290,7 +301,7 @@ class CsvIn extends GaletteTestCase
      */
     protected function checkDynamicTranslation($text_orig, $lang = 'fr_FR.utf8')
     {
-        $langs = array_keys($this->i18n->langs);
+        $langs = array_keys($this->i18n->getArrayList());
         $select = $this->zdb->select(\Galette\Core\L10n::TABLE);
         $select->columns([
             'text_locale',
@@ -299,13 +310,14 @@ class CsvIn extends GaletteTestCase
         ]);
         $select->where(['text_orig' => $text_orig]);
         $results = $this->zdb->execute($select);
-        $this->integer($results->count())->isIdenticalTo(count($langs));
+        $this->assertSame(count($langs), $results->count());
 
         foreach ($results as $result) {
-            $this->boolean(in_array(str_replace('.utf8', '', $result['text_locale']), $langs))->isTrue();
-            $this->integer((int)$result['text_nref'])->isIdenticalTo(1);
-            $this->string($result['text_trans'])->isIdenticalTo(
-                ($result['text_locale'] == 'en_US' ? $text_orig : '')
+            $this->assertTrue(in_array(str_replace('.utf8', '', $result['text_locale']), $langs));
+            $this->assertSame(1, (int)$result['text_nref']);
+            $this->assertSame(
+                ($result['text_locale'] == 'en_US' ? $text_orig : ''),
+                $result['text_trans']
             );
         }
     }
@@ -332,21 +344,22 @@ class CsvIn extends GaletteTestCase
         $stored = $df->store($field_data);
         $error_detected = $df->getErrors();
         $warning_detected = $df->getWarnings();
-        $this->boolean($stored)->isTrue(
+        $this->assertTrue(
+            $stored,
             implode(
                 ' ',
                 $df->getErrors() + $df->getWarnings()
             )
         );
-        $this->array($error_detected)->isEmpty(implode(' ', $df->getErrors()));
-        $this->array($warning_detected)->isEmpty(implode(' ', $df->getWarnings()));
+        $this->assertEmpty($error_detected, implode(' ', $df->getErrors()));
+        $this->assertEmpty($warning_detected, implode(' ', $df->getWarnings()));
         //check if dynamic translation has been added
         $this->checkDynamicTranslation($field_data['field_name']);
 
         $select = $this->zdb->select(DynamicField::TABLE);
         $select->columns(array('num' => new \Laminas\Db\Sql\Expression('COUNT(1)')));
         $result = $this->zdb->execute($select)->current();
-        $this->integer((int)$result->num)->isIdenticalTo(1);
+        $this->assertSame(1, (int)$result->num);
 
         $fields = ['nom_adh', 'ville_adh', 'dynfield_' . $df->getId(), 'fingerprint'];
         $file_name = 'test-import-atoum-dyn.csv';
@@ -431,23 +444,24 @@ class CsvIn extends GaletteTestCase
         $stored = $cdf->store($cfield_data);
         $error_detected = $cdf->getErrors();
         $warning_detected = $cdf->getWarnings();
-        $this->boolean($stored)->isTrue(
+        $this->assertTrue(
+            $stored,
             implode(
                 ' ',
                 $cdf->getErrors() + $cdf->getWarnings()
             )
         );
-        $this->array($error_detected)->isEmpty(implode(' ', $cdf->getErrors()));
-        $this->array($warning_detected)->isEmpty(implode(' ', $cdf->getWarnings()));
+        $this->assertEmpty($error_detected, implode(' ', $cdf->getErrors()));
+        $this->assertEmpty($warning_detected, implode(' ', $cdf->getWarnings()));
         //check if dynamic translation has been added
         $this->checkDynamicTranslation($cfield_data['field_name']);
 
         $select = $this->zdb->select(DynamicField::TABLE);
         $select->columns(array('num' => new \Laminas\Db\Sql\Expression('COUNT(1)')));
         $result = $this->zdb->execute($select)->current();
-        $this->integer((int)$result->num)->isIdenticalTo(2);
+        $this->assertSame(2, (int)$result->num);
 
-        $this->array($cdf->getValues())->isIdenticalTo($values);
+        $this->assertSame($values, $cdf->getValues());
 
         $fields = ['nom_adh', 'ville_adh', 'dynfield_' . $cdf->getId(), 'fingerprint'];
         $file_name = 'test-import-atoum-dyn-cdyn.csv';
@@ -496,21 +510,22 @@ class CsvIn extends GaletteTestCase
         $stored = $cdf->store($cfield_data);
         $error_detected = $cdf->getErrors();
         $warning_detected = $cdf->getWarnings();
-        $this->boolean($stored)->isTrue(
+        $this->assertTrue(
+            $stored,
             implode(
                 ' ',
                 $cdf->getErrors() + $cdf->getWarnings()
             )
         );
-        $this->array($error_detected)->isEmpty(implode(' ', $cdf->getErrors()));
-        $this->array($warning_detected)->isEmpty(implode(' ', $cdf->getWarnings()));
+        $this->assertEmpty($error_detected, implode(' ', $cdf->getErrors()));
+        $this->assertEmpty($warning_detected, implode(' ', $cdf->getWarnings()));
         //check if dynamic translation has been added
         $this->checkDynamicTranslation($cfield_data['field_name']);
 
         $select = $this->zdb->select(DynamicField::TABLE);
         $select->columns(array('num' => new \Laminas\Db\Sql\Expression('COUNT(1)')));
         $result = $this->zdb->execute($select)->current();
-        $this->integer((int)$result->num)->isIdenticalTo(3);
+        $this->assertSame(3, (int)$result->num);
 
 
         $fields = ['nom_adh', 'ville_adh', 'dynfield_' . $cdf->getId(), 'fingerprint'];
@@ -581,7 +596,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'tgonzalez',
                 'pays_adh' => null,
                 'tel_adh' => '+33 8 93 53 99 52',
-                'url_adh' => null,
                 'activite_adh' => true,
                 'id_statut' => 9,
                 'date_crea_adh' => '2020-03-09',
@@ -609,7 +623,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'agnes.evrard',
                 'pays_adh' => null,
                 'tel_adh' => '0288284193',
-                'url_adh' => 'https://leroux.fr/omnis-autem-suscipit-consequuntur-possimus-sint-iste-beatae.html',
                 'activite_adh' => true,
                 'id_statut' => 9,
                 'date_crea_adh' => '2019-11-29',
@@ -637,7 +650,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'fgay',
                 'pays_adh' => null,
                 'tel_adh' => '+33 7 45 45 19 81',
-                'url_adh' => null,
                 'activite_adh' => true,
                 'id_statut' => 8,
                 'date_crea_adh' => '2019-02-03',
@@ -665,7 +677,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'chauvin.guillaume',
                 'pays_adh' => 'Hong Kong',
                 'tel_adh' => '+33 5 48 57 32 28',
-                'url_adh' => null,
                 'activite_adh' => true,
                 'id_statut' => 1,
                 'date_crea_adh' => '2017-11-20',
@@ -695,7 +706,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'sauvage.dorothee',
                 'pays_adh' => 'Bangladesh',
                 'tel_adh' => '+33 4 75 14 66 56',
-                'url_adh' => null,
                 'activite_adh' => false,
                 'id_statut' => 9,
                 'date_crea_adh' => '2018-08-16',
@@ -723,7 +733,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'andre.guillou',
                 'pays_adh' => null,
                 'tel_adh' => '09 26 70 06 55',
-                'url_adh' => 'http://www.hoarau.fr/quis-neque-ducimus-quidem-et',
                 'activite_adh' => true,
                 'id_statut' => 9,
                 'date_crea_adh' => '2018-09-28',
@@ -751,7 +760,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'ferreira.rene',
                 'pays_adh' => 'Tuvalu',
                 'tel_adh' => '+33 (0)7 47 56 89 70',
-                'url_adh' => null,
                 'activite_adh' => true,
                 'id_statut' => 9,
                 'date_crea_adh' => '2018-01-13',
@@ -779,7 +787,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'roger27',
                 'pays_adh' => 'Antilles néerlandaises',
                 'tel_adh' => '0922523762',
-                'url_adh' => null,
                 'activite_adh' => true,
                 'id_statut' => 9,
                 'date_crea_adh' => '2020-02-13',
@@ -809,7 +816,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'julien.isabelle',
                 'pays_adh' => 'Mexique',
                 'tel_adh' => '0809527977',
-                'url_adh' => null,
                 'activite_adh' => true,
                 'id_statut' => 9,
                 'date_crea_adh' => '2019-06-26',
@@ -837,7 +843,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'louis.pruvost',
                 'pays_adh' => null,
                 'tel_adh' => '+33 (0)6 80 24 46 58',
-                'url_adh' => null,
                 'activite_adh' => true,
                 'id_statut' => 9,
                 'date_crea_adh' => '2020-08-09',
@@ -877,7 +882,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'olivier.roux',
                 'pays_adh' => 'République Dominicaine',
                 'tel_adh' => '08 95 04 73 14',
-                'url_adh' => null,
                 'activite_adh' => true,
                 'id_statut' => 9,
                 'date_crea_adh' => '2020-07-31',
@@ -905,7 +909,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'hchevalier',
                 'pays_adh' => 'Kiribati',
                 'tel_adh' => '04 55 49 80 92',
-                'url_adh' => 'http://www.leblanc.com/nemo-non-rerum-commodi-sequi-ut',
                 'activite_adh' => true,
                 'id_statut' => 1,
                 'date_crea_adh' => '2020-06-02',
@@ -933,7 +936,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'abarre',
                 'pays_adh' => 'Kirghizistan',
                 'tel_adh' => '07 47 63 11 31',
-                'url_adh' => 'https://www.jacques.com/fuga-voluptatem-tenetur-rem-possimus',
                 'activite_adh' => true,
                 'id_statut' => 9,
                 'date_crea_adh' => '2020-10-28',
@@ -961,7 +963,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'frederique.bernier',
                 'pays_adh' => null,
                 'tel_adh' => '+33 2 50 03 01 12',
-                'url_adh' => null,
                 'activite_adh' => true,
                 'id_statut' => 9,
                 'date_crea_adh' => '2020-08-14',
@@ -989,7 +990,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'stoussaint',
                 'pays_adh' => 'Îles Mineures Éloignées des États-Unis',
                 'tel_adh' => '+33 (0)1 30 50 01 54',
-                'url_adh' => 'http://www.lemaitre.org/dolorum-recusandae-non-eum-non',
                 'activite_adh' => true,
                 'id_statut' => 3,
                 'date_crea_adh' => '2018-12-05',
@@ -1019,7 +1019,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'virginie.jacquet',
                 'pays_adh' => null,
                 'tel_adh' => '0393209420',
-                'url_adh' => null,
                 'activite_adh' => true,
                 'id_statut' => 9,
                 'date_crea_adh' => '2018-02-17',
@@ -1047,7 +1046,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'diallo.sebastien',
                 'pays_adh' => null,
                 'tel_adh' => '+33 5 72 28 24 81',
-                'url_adh' => null,
                 'activite_adh' => true,
                 'id_statut' => 9,
                 'date_crea_adh' => '2020-03-16',
@@ -1075,7 +1073,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'helene59',
                 'pays_adh' => null,
                 'tel_adh' => '0383453389',
-                'url_adh' => 'http://www.lesage.com/et-aperiam-labore-est-libero-voluptatem.html',
                 'activite_adh' => true,
                 'id_statut' => 9,
                 'date_crea_adh' => '2020-02-03',
@@ -1103,7 +1100,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'wpierre',
                 'pays_adh' => null,
                 'tel_adh' => '01 32 14 47 74',
-                'url_adh' => null,
                 'activite_adh' => true,
                 'id_statut' => 9,
                 'date_crea_adh' => '2020-03-28',
@@ -1131,7 +1127,6 @@ class CsvIn extends GaletteTestCase
                 'pseudo_adh' => 'gerard66',
                 'pays_adh' => null,
                 'tel_adh' => '+33 1 46 04 81 87',
-                'url_adh' => 'http://www.thierry.com/',
                 'activite_adh' => true,
                 'id_statut' => 5,
                 'date_crea_adh' => '2019-05-16',