Skip to content
Merged
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 .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ repos:
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
exclude: (docs|^acceptance-test-results\.md$)
- id: check-json
- id: check-xml
- id: check-yaml
Expand Down
12 changes: 10 additions & 2 deletions acceptance-test-results.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# Accpetance Test Results
# Acceptance Test Results

## Memory Usage (Phpolar\CsvFileStorage\MemoryUsage)
- [x] Memory usage for saving data shall be below 2000 bytes
- [x] Memory usage for saving data (with pre-loading disabled) shall be below 115000 bytes
- [x] Memory usage for saving data (with pre-loading disabled) shall be below 125000 bytes

## Storage Mutability (Phpolar\CsvFileStorage\StorageMutability)
- [x] Shall create persistent storage
- [x] Shall add items to storage without removing existing values
- [x] Shall update objects in storage without removing other objects
- [x] Shall update items in storage without removing other values
- [x] Shall remove objects from storage without removing other objects
- [x] Shall remove items from storage without removing other values

## Project Size (Phpolar\CsvFileStorage\ProjectSize)
- [x] Source code total size shall be below 4000 bytes
2 changes: 1 addition & 1 deletion docs/classes/Phpolar-CsvFileStorage-CsvFileStorage.html
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ <h4 class="phpdocumentor-element__name" id="method_count">
<aside class="phpdocumentor-element-found-in">
<abbr class="phpdocumentor-element-found-in__file" title="CsvFileStorage.php"><a href="files/csvfilestorage.html"><abbr title="CsvFileStorage.php">CsvFileStorage.php</abbr></a></abbr>
:
<span class="phpdocumentor-element-found-in__line">124</span>
<span class="phpdocumentor-element-found-in__line">136</span>

</aside>

Expand Down
48 changes: 24 additions & 24 deletions docs/graphs/classes.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion phpunit.dev.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
<const name="SRC_GLOB" value="/src{/,/**/}*.php" />
<const name="Phpolar\Tests\PROJECT_SIZE_THRESHOLD" value="4000" />
<const name="Phpolar\Tests\PROJECT_MEMORY_USAGE_THRESHOLD" value="2000" />
<const name="Phpolar\Tests\PROJECT_MEMORY_USAGE_THRESHOLD_WITHOUT_PRELOADING" value="115000" />
<const name="Phpolar\Tests\PROJECT_MEMORY_USAGE_THRESHOLD_WITHOUT_PRELOADING" value="125000" />
</php>
</phpunit>
44 changes: 33 additions & 11 deletions src/CsvFileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ final class CsvFileStorage extends AbstractStorage implements Countable

private const MEMORY_STREAM = "php://memory";

public function __construct(string $filename, private ?string $typeClassName = null)
public function __construct(private string $filename, private ?string $typeClassName = null)
{
$readMode = $filename === self::MEMORY_STREAM ? "r" : "r+";
$writeMode = $filename === self::MEMORY_STREAM ? "a+" : "r+";
$readMode = "r";
$writeMode = "a";
$readStream = fopen($filename, $readMode);
$writeStream = fopen($filename, $writeMode);
$this->closeWriteStream = $filename !== self::MEMORY_STREAM;
Expand Down Expand Up @@ -84,11 +84,22 @@ public function __destruct()

public function commit(): void
{
foreach ($this->getAll() as $record) {
$this->typeClassName ??= is_object($record) === true ? get_class($record) : null;
if ($this->fileSize > 0) {
fclose($this->writeStream);
$file = fopen($this->filename, "w");
// @codeCoverageIgnoreStart
if ($file !== false) {
$this->writeStream = $file;
}
// @codeCoverageIgnoreEnd
}
foreach ($this->getAll() as $index => $record) {
switch (true) {
case is_object($record):
fputcsv($this->writeStream, $this->convertObjVars($record));
$objVars = get_object_vars($record);
$this->setUpObject($index, $record, $objVars);
$convertedVars = $this->convertObjVars($objVars);
fputcsv($this->writeStream, $convertedVars);
break;
case is_scalar($record):
fputcsv($this->writeStream, [$record]);
Expand All @@ -104,17 +115,18 @@ public function commit(): void
}

/**
* @return array<int|string, string>
* @param array<string,mixed> $objVars
* @return array<string,string>
*/
private function convertObjVars(object $record): array
private function convertObjVars(array $objVars): array
{
return array_map(
static fn (mixed $item) => match (true) {
$item instanceof DateTimeImmutable => $item->format(DATE_RSS),
$item instanceof DateTimeImmutable => $item->format(DATE_RFC3339),
is_scalar($item) => (string) $item,
default => "",
},
get_object_vars($record),
$objVars,
);
}

Expand All @@ -135,7 +147,6 @@ protected function load(): void
if ($this->hasEmptyHeader() === true) {
throw new DomainException("Malformed CSV file");
}

if ($this->hasObjects() === false) {
$this->storeLine($this->firstLine);
}
Expand Down Expand Up @@ -165,6 +176,17 @@ private function setFirstLine(): void
}
}

/**
* @param array<string,mixed> $objVars
*/
private function setUpObject(int|string $index, object $record, array $objVars): void
{
if ($index === 0) {
$this->typeClassName = get_class($record);
fputcsv($this->writeStream, array_keys($objVars));
}
}

/**
* @param array<int,non-empty-string> $headers
* @param list<?string> $line
Expand Down
190 changes: 190 additions & 0 deletions tests/acceptance/StorageMutabilityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php

declare(strict_types=1);

namespace Phpolar\CsvFileStorage;

use Phpolar\CsvFileStorage\Tests\Fakes\FakeValueObject;
use Phpolar\Phpolar\Storage\Item;
use Phpolar\Phpolar\Storage\ItemKey;
use Phpolar\Phpolar\Storage\ItemNotFound;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;

#[CoversNothing]
final class StorageMutabilityTest extends TestCase
{
protected string $filename;

protected function setUp(): void
{
$this->filename = tempnam(sys_get_temp_dir(), uniqid());
}

protected function tearDown(): void
{
file_exists($this->filename) && unlink($this->filename);
}

#[Test]
#[TestDox("Shall create persistent storage")]
public function criterionA()
{
$sutA = new CsvFileStorage($this->filename);
$key = new ItemKey(0);
$item = new Item(PHP_INT_MAX);
$sutA->storeByKey($key, $item);
$sutA->commit();
$this->assertFileExists($this->filename);
$sutB = new CsvFileStorage($this->filename);
$stored = $sutB->getByKey($key);
$this->assertContains((string) $item->bind(), $stored->bind());
}

#[Test]
#[TestDox("Shall add items to storage without removing existing values")]
public function criterionB()
{
$sutA = new CsvFileStorage($this->filename);
$key0 = new ItemKey(0);
$item0 = new Item(PHP_INT_MAX);
$sutA->storeByKey($key0, $item0);
$sutA->commit();
$sutB = new CsvFileStorage($this->filename);
$key1 = new ItemKey(1);
$item1 = new Item(PHP_INT_MIN);
$sutB->storeByKey($key1, $item1);
$sutB->commit();
$sutC = new CsvFileStorage($this->filename);
$stored0 = $sutC->getByKey($key0);
$stored1 = $sutC->getByKey($key1);
$this->assertContains((string) $item0->bind(), $stored0->bind());
$this->assertContains((string) $item1->bind(), $stored1->bind());
}

#[Test]
#[TestDox("Shall remove items from storage without removing other values")]
public function criterionC()
{
$sutA = new CsvFileStorage($this->filename);
$key0 = new ItemKey(0);
$item0 = new Item(PHP_INT_MAX);
$sutA->storeByKey($key0, $item0);
$sutA->commit();
unset($sutA);
$sutB = new CsvFileStorage($this->filename);
$key1 = new ItemKey(1);
$item1 = new Item(PHP_INT_MIN);
$key2 = new ItemKey(2);
$item2 = new Item(PHP_OS);
$sutB->storeByKey($key1, $item1);
$sutB->storeByKey($key2, $item2);
$sutB->commit();
unset($sutB);
$sutC = new CsvFileStorage($this->filename);
$sutC->removeByKey($key2);
$sutC->commit();
unset($sutC);
$sutD = new CsvFileStorage($this->filename);
$stored0 = $sutD->getByKey($key0);
$stored1 = $sutD->getByKey($key1);
$stored2 = $sutD->getByKey($key2);
$this->assertContains((string) $item0->bind(), $stored0->bind());
$this->assertContains((string) $item1->bind(), $stored1->bind());
$this->assertInstanceOf(ItemNotFound::class, $stored2);
}

#[Test]
#[TestDox("Shall update items in storage without removing other values")]
public function criterionD()
{
$sutA = new CsvFileStorage($this->filename);
$key0 = new ItemKey(0);
$item0 = new Item(PHP_INT_MAX);
$sutA->storeByKey($key0, $item0);
$sutA->commit();
$sutB = new CsvFileStorage($this->filename);
$key1 = new ItemKey(1);
$item1 = new Item(PHP_INT_MIN);
$item2 = new Item(PHP_OS);
$key2 = new ItemKey(2);
$sutB->storeByKey($key1, $item1);
$sutB->storeByKey($key2, $item2);
$sutB->commit();
$sutC = new CsvFileStorage($this->filename);
$item2Updated = new Item(PHP_SAPI);
$sutC->replaceByKey($key2, $item2Updated);
$sutC->commit();
$sutD = new CsvFileStorage($this->filename);
$stored0 = $sutD->getByKey($key0);
$stored1 = $sutD->getByKey($key1);
$stored2 = $sutD->getByKey($key2);
$this->assertContains((string) $item0->bind(), $stored0->bind());
$this->assertContains((string) $item1->bind(), $stored1->bind());
$this->assertContains((string) $item2Updated->bind(), $stored2->bind());
}

#[Test]
#[TestDox("Shall remove objects from storage without removing other objects")]
public function criterionE()
{
$sutA = new CsvFileStorage($this->filename, FakeValueObject::class);
$key0 = new ItemKey(0);
$item0 = new Item(new FakeValueObject("fake1"));
$sutA->storeByKey($key0, $item0);
$sutA->commit();
unset($sutA);
$sutB = new CsvFileStorage($this->filename, FakeValueObject::class);
$key1 = new ItemKey(1);
$item1 = new Item(new FakeValueObject("fake2"));
$key2 = new ItemKey(2);
$item2 = new Item(new FakeValueObject("fake3"));
$sutB->storeByKey($key1, $item1);
$sutB->storeByKey($key2, $item2);
$sutB->commit();
unset($sutB);
$sutC = new CsvFileStorage($this->filename, FakeValueObject::class);
$sutC->removeByKey($key2);
$sutC->commit();
unset($sutC);
$sutD = new CsvFileStorage($this->filename, FakeValueObject::class);
$stored0 = $sutD->getByKey($key0);
$stored1 = $sutD->getByKey($key1);
$stored2 = $sutD->getByKey($key2);
$this->assertSame($item0->bind()->title, $stored0->bind()->title);
$this->assertSame($item1->bind()->title, $stored1->bind()->title);
$this->assertInstanceOf(ItemNotFound::class, $stored2);
}

#[Test]
#[TestDox("Shall update objects in storage without removing other objects")]
public function criterionF()
{
$sutA = new CsvFileStorage($this->filename, FakeValueObject::class);
$key0 = new ItemKey(0);
$item0 = new Item(new FakeValueObject("fake1"));
$sutA->storeByKey($key0, $item0);
$sutA->commit();
$sutB = new CsvFileStorage($this->filename, FakeValueObject::class);
$key1 = new ItemKey(1);
$item1 = new Item(new FakeValueObject("fake2"));
$item2 = new Item(new FakeValueObject("fake3"));
$key2 = new ItemKey(2);
$sutB->storeByKey($key1, $item1);
$sutB->storeByKey($key2, $item2);
$sutB->commit();
$sutC = new CsvFileStorage($this->filename, FakeValueObject::class);
$item2Updated = new Item(new FakeValueObject("fake3 UPDATED"));
$sutC->replaceByKey($key2, $item2Updated);
$sutC->commit();
$sutD = new CsvFileStorage($this->filename, FakeValueObject::class);
$stored0 = $sutD->getByKey($key0);
$stored1 = $sutD->getByKey($key1);
$stored2 = $sutD->getByKey($key2);
$this->assertSame($item0->bind()->title, $stored0->bind()->title);
$this->assertSame($item1->bind()->title, $stored1->bind()->title);
$this->assertSame($item2Updated->bind()->title, $stored2->bind()->title);
}
}