Skip to content

Commit

Permalink
tests / cs
Browse files Browse the repository at this point in the history
  • Loading branch information
dhirtzbruch committed Nov 20, 2023
1 parent 9cb7949 commit d4d8e0d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/EntityImporterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Fastbolt\EntityImporter\Exceptions\InvalidInputFormatException;
use Fastbolt\EntityImporter\Exceptions\SourceUnavailableException;
use Fastbolt\EntityImporter\Types\ImportResult;
use InvalidArgumentException;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Throwable;

Expand Down Expand Up @@ -72,12 +73,17 @@ public function getImporterDefinitions(): array
*
* @throws InvalidInputFormatException
* @throws SourceUnavailableException
* @throws InvalidArgumentException
*/
public function import(string $name, callable $statusCallback, callable $errorCallback, ?int $limit): ImportResult
{
$start = new DateTime();
try {
if (!$name || null === ($definition = $this->definitions[$name] ?? null)) {
if (!$name) {
throw new InvalidArgumentException('Name must not be empty');
}

if (null === ($definition = $this->definitions[$name] ?? null)) {
throw new ImporterDefinitionNotFoundException($name);
}

Expand Down
16 changes: 16 additions & 0 deletions tests/unit/EntityImporterManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Fastbolt\EntityImporter\Events\ImportSuccessEvent;
use Fastbolt\EntityImporter\Exceptions\ImporterDefinitionNotFoundException;
use Fastbolt\TestHelpers\BaseTestCase;
use InvalidArgumentException;
use PHPUnit\Framework\MockObject\MockObject;
use stdClass;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -96,6 +97,21 @@ public function testImportInvalidType(): void
self::assertSame([], $manager->getImporterDefinitions());
$manager->import('importer:def2:name', $this->statusCallback, $this->errorCallback, null);
}
public function testImportEmptyType(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Name must not be empty');
$manager = new EntityImporterManager(
$this->importer,
$this->dispatcher,
[]
);
$this->dispatcher->expects(self::once())
->method('dispatch')
->with(self::isInstanceOf(ImportFailureEvent::class));
self::assertSame([], $manager->getImporterDefinitions());
$manager->import('', $this->statusCallback, $this->errorCallback, null);
}

protected function setUp(): void
{
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/Events/ImportFailureEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* Copyright © Fastbolt Schraubengroßhandels GmbH.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fastbolt\EntityImporter\Tests\Unit\Events;

use DateTime;
use Exception;
use Fastbolt\EntityImporter\EntityImporterDefinition;
use Fastbolt\EntityImporter\Events\ImportFailureEvent;
use Fastbolt\TestHelpers\BaseTestCase;
use PHPUnit\Framework\MockObject\MockObject;

/**
* @covers \Fastbolt\EntityImporter\Events\ImportFailureEvent
*/
class ImportFailureEventTest extends BaseTestCase
{
/**
* @var Exception|MockObject
*/
private $exception;

/**
* @var EntityImporterDefinition|MockObject
*/
private $definition;

public function testEvent(): void
{
$event = new ImportFailureEvent($this->definition, $start = new DateTime(), $this->exception);

self::assertSame($this->definition, $event->getDefinition());
self::assertSame($start, $event->getImportStart());
self::assertSame($this->exception, $event->getException());
}

protected function setUp(): void
{
$this->exception = $this->getMock(Exception::class);
$this->definition = $this->getMock(EntityImporterDefinition::class);
}
}
39 changes: 38 additions & 1 deletion tests/unit/Events/ImportSuccessEventTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
<?php

/**
* Copyright © Fastbolt Schraubengroßhandels GmbH.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fastbolt\EntityImporter\Tests\Unit\Events;

use DateTime;
use Fastbolt\EntityImporter\EntityImporterDefinition;
use Fastbolt\EntityImporter\Events\ImportSuccessEvent;
use Fastbolt\EntityImporter\Types\ImportResult;
use Fastbolt\TestHelpers\BaseTestCase;
use PHPUnit\Framework\MockObject\MockObject;

/**
* @covers \Fastbolt\EntityImporter\Events\ImportSuccessEvent
*/
class ImportSuccessEventTest extends BaseTestCase
{
/**
* @var ImportResult|MockObject
*/
private $importResult;

/**
* @var EntityImporterDefinition|MockObject
*/
private $definition;

public function testEvent(): void
{
$event = new ImportSuccessEvent($this->definition, $start = new DateTime(), $this->importResult);

class ImportSuccessEventTest extends BaseTestCase {
self::assertSame($this->definition, $event->getDefinition());
self::assertSame($this->importResult, $event->getImportResult());
self::assertSame($start, $event->getImportStart());
}

protected function setUp(): void
{
$this->importResult = $this->getMock(ImportResult::class);
$this->definition = $this->getMock(EntityImporterDefinition::class);
}
}

0 comments on commit d4d8e0d

Please sign in to comment.