diff --git a/.gitignore b/.gitignore index 27eff37..0b6e2bc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor/ /.phpunit.cache/ +/var/db.sqlite diff --git a/src/CsvDataImporter.php b/src/CsvDataImporter.php index 5c259e8..88c6fa1 100644 --- a/src/CsvDataImporter.php +++ b/src/CsvDataImporter.php @@ -38,7 +38,7 @@ private function importData(array $records): void try { $this->db->beginTransaction(); - $this->db->exec('TRUNCATE imported'); + $this->db->exec('DELETE FROM imported'); foreach ($records as $record) { $this->db->prepare('INSERT INTO imported VALUES (?, ?, ?)') diff --git a/src/Repository.php b/src/Repository.php new file mode 100644 index 0000000..9049cd0 --- /dev/null +++ b/src/Repository.php @@ -0,0 +1,24 @@ +db = $db; + } + + public function getCount(): int + { + $data = $this->db->query('SELECT COUNT(*) AS nb FROM imported')->fetch(); + + return (int) $data['nb']; + } +} diff --git a/tests/CsvDataImporterTest.php b/tests/CsvDataImporterTest.php new file mode 100644 index 0000000..30bda43 --- /dev/null +++ b/tests/CsvDataImporterTest.php @@ -0,0 +1,22 @@ +import('var/import/data.csv'); + + $repository = new Repository($db); + $this->assertSame(3, $repository->getCount()); + } +} diff --git a/tests/TestsFacility.php b/tests/TestsFacility.php new file mode 100644 index 0000000..7f46584 --- /dev/null +++ b/tests/TestsFacility.php @@ -0,0 +1,35 @@ + PDO::FETCH_ASSOC, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION + ]); + + if (! $exists) { + self::loadFixtures($db); + } + + return $db; + } + + private static function loadFixtures(PDO $db): void + { + $db->exec('CREATE TABLE imported ( + champ1 TINYTEXT NOT NULL, + champ2 TINYTEXT NOT NULL, + champ3 TINYTEXT NOT NULL + )'); + } +}