Skip to content

Commit

Permalink
Test FieldCollectionFactory with empty list
Browse files Browse the repository at this point in the history
  • Loading branch information
jeckel committed Oct 24, 2023
1 parent 4f5c216 commit 69a990f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"analyze": "@analyse",
"cs-fix": "vendor/bin/php-cs-fixer fix --diff --verbose",
"phpmd": "vendor/bin/phpmd src,tests text ruleset.xml",
"test": "vendor/bin/phpunit --testdox",
"test": "vendor/bin/phpunit --testdox --display-warnings",
"test-coverage": [
"Composer\\Config::disableProcessTimeout",
"XDEBUG_MODE=coverage vendor/bin/phpunit --testdox --coverage-html=.build/coverage"
Expand Down
4 changes: 2 additions & 2 deletions src/Factory/FieldCollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public function __construct(
}

/**
* @param array{fields: array<string, mixed>} $jsonData
* @param array<string, mixed> $jsonData
* @return FieldCollection
*/
public function constructFromJson(array $jsonData): FieldCollection
{
$this->fieldCollectionBuilder->reset();
/** @var array{alias: string, group: string, id: int, label: string, type: string, value: mixed} $fieldData */
foreach ($jsonData['fields'] as $fieldData) {
foreach ($jsonData as $fieldData) {
$field = $this->fieldParser->parseFieldValue($fieldData);
if (null === $field) {
continue;
Expand Down
8 changes: 7 additions & 1 deletion src/Model/FieldCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

namespace JeckelLab\MauticWebhookParser\Model;

use Countable;
use JeckelLab\MauticWebhookParser\ValueObject\Field\Field;

readonly class FieldCollection
readonly class FieldCollection implements Countable
{
/**
* @param array<string, Field> $fields
Expand All @@ -22,4 +23,9 @@ public function __get(string $name): ?Field
{
return $this->fields[$name] ?? null;
}

public function count(): int
{
return count($this->fields);
}
}
16 changes: 16 additions & 0 deletions tests/Factory/FieldCollectionFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace JeckelLab\MauticWebhookParser\Tests\Factory;

use JeckelLab\MauticWebhookParser\Factory\FieldCollectionFactory;
use PHPUnit\Framework\TestCase;

class FieldCollectionFactoryTest extends TestCase
{
public function testFactoryWithEmptyArrayShouldReturnEmptyCollection(): void
{
$data = [];
$collection = (new FieldCollectionFactory())->constructFromJson($data);
self::assertCount(0, $collection);
}
}

0 comments on commit 69a990f

Please sign in to comment.