Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
/.phpunit.cache/
/var/db.sqlite
2 changes: 1 addition & 1 deletion src/CsvDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 (?, ?, ?)')
Expand Down
24 changes: 24 additions & 0 deletions src/Repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace solid;

use PDO;

class Repository
{
private PDO $db;

public function __construct(PDO $db)
{
$this->db = $db;
}

public function getCount(): int
{
$data = $this->db->query('SELECT COUNT(*) AS nb FROM imported')->fetch();

return (int) $data['nb'];
}
}
22 changes: 22 additions & 0 deletions tests/CsvDataImporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace tests;

use PHPUnit\Framework\TestCase;
use solid\CsvDataImporter;
use solid\Repository;

class CsvDataImporterTest extends TestCase
{
public function testImport(): void
{
$db = TestsFacility::createDb();
$importer = new CsvDataImporter($db);
$importer->import('var/import/data.csv');

$repository = new Repository($db);
$this->assertSame(3, $repository->getCount());
}
}
35 changes: 35 additions & 0 deletions tests/TestsFacility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace tests;

use PDO;

class TestsFacility
{
public static function createDb(): PDO
{
$exists = is_file('var/db.sqlite');

$db = new PDO('sqlite:var/db.sqlite', null, null, [
PDO::ATTR_DEFAULT_FETCH_MODE => 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
)');
}
}