Skip to content

Commit

Permalink
Improved code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bpolaszek committed Mar 8, 2017
1 parent 535f67c commit e762a94
Show file tree
Hide file tree
Showing 14 changed files with 371 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use BenTools\ETL\Event\ETLEvent;
use BenTools\ETL\Event\EventDispatcher\Bridge\Symfony\SymfonyEvent;
use BenTools\ETL\Event\EventDispatcher\Bridge\Symfony\SymfonyEventDispatcherBridge;
use Symfony\Component\EventDispatcher\EventDispatcher;
use PHPUnit\Framework\TestCase;


class SymfonyEventDispatcherBridgeTest extends TestCase
{
/**
Expand All @@ -20,6 +20,21 @@ public function setUp()
$this->eventDispatcher = new SymfonyEventDispatcherBridge();
}

public function testWrappedDispatcher()
{
$this->assertInstanceOf(EventDispatcher::class, $this->eventDispatcher->getWrappedDispatcher());
}

public function testMagicCall()
{
$bar = function () {};
$this->eventDispatcher->addListener('foo', $bar);
$listeners = $this->eventDispatcher->getListeners('foo');
$this->assertInternalType('array', $listeners);
$this->assertCount(1, $listeners);
$this->assertArrayHasKey(0, $listeners);
$this->assertSame($bar, $listeners[0]);
}

public function testTrigger()
{
Expand Down
22 changes: 22 additions & 0 deletions tests/src/Event/EventDispatcher/NullEventDispatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace BenTools\ETL\Tests\Event\EventDispatcher;

use BenTools\ETL\Event\ETLEvent;
use BenTools\ETL\Event\EventDispatcher\NullEventDispatcher;
use PHPUnit\Framework\TestCase;

class NullEventDispatcherTest extends TestCase
{

public function testEventDispatcher()
{
$foo = false;
$eventDispatcher = new NullEventDispatcher();
$eventDispatcher->addListener('bar', function () use (&$foo) {
$foo = true;
});
$eventDispatcher->trigger(new ETLEvent('bar'));
$this->assertFalse($foo);
}
}
22 changes: 22 additions & 0 deletions tests/src/Extractor/ArrayPropertyExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,26 @@ public function testExtractorNotShift()
'dolor' => 'sit amet',
], $element->getData());
}


/**
* @expectedException \RuntimeException
*/
public function testExtractorWithNonExistentProperty()
{

$extract = new ArrayPropertyExtractor('bar');
$extract('foo', []);
}

/**
* @expectedException \RuntimeException
*/
public function testExtractorWithAnInvalidContextClass()
{
$context = new class() {};
$class = get_class($context);
$extract = new ArrayPropertyExtractor('bar', true, $class);
$extract('foo', ['bar' => 'baz']);
}
}
16 changes: 16 additions & 0 deletions tests/src/Extractor/CallbackExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,20 @@ public function testExtractor()
'dolor' => 'sit amet',
], $element->getData());
}

/**
* @expectedException \RuntimeException
*/
public function testExtractorWithAnInvalidContextClass()
{
$callback = function (ContextElementInterface $element) {
$data = $element->getData();
$this->assertEquals('foo', $element->getId());
$element->setId($data->bar);
};
$context = new class() {};
$class = get_class($context);
$extract = new CallbackExtractor($callback, $class);
$element = $extract('foo', 'bar');
}
}
1 change: 1 addition & 0 deletions tests/src/Extractor/IncrementorExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function testExtractor()
public function testExtractorWithAnotherStartIndex()
{
$extract = new IncrementorExtractor(10);
$this->assertEquals(9, $extract->getIndex());

$element = $extract('foo', 'bar');
$this->assertInstanceOf(ContextElementInterface::class, $element);
Expand Down
47 changes: 47 additions & 0 deletions tests/src/Extractor/KeyValueExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BenTools\ETL\Tests\Extractor;

use BenTools\ETL\Context\ContextElement;
use BenTools\ETL\Context\ContextElementInterface;
use BenTools\ETL\Extractor\KeyValueExtractor;
use PHPUnit\Framework\TestCase;
Expand All @@ -14,7 +15,53 @@ public function testExtractor()
$extract = new KeyValueExtractor();
$element = $extract('foo', 'bar');
$this->assertInstanceOf(ContextElementInterface::class, $element);
$this->assertInstanceOf(ContextElement::class, $element);
$this->assertEquals('foo', $element->getId());
$this->assertEquals('bar', $element->getData());
}

public function testExtractorWithADifferentContextClass()
{
$context = new class() implements ContextElementInterface
{
public function setId($id): void {}
public function getId() {}
public function setData($data): void {}
public function getData() {}
public function skip(): void {}
public function stop(bool $flush = true): void {}
public function flush(): void {}
public function shouldSkip(): bool {}
public function shouldStop(): bool {}
public function shouldFlush(): bool {}
};

$class = get_class($context);

// Check constructor
$extract = new KeyValueExtractor($class);
$element = $extract('foo', 'bar');
$this->assertInstanceOf(ContextElementInterface::class, $element);
$this->assertNotInstanceOf(ContextElement::class, $element);
$this->assertInstanceOf($class, $element);

// Check setter
$extract = new KeyValueExtractor();
$extract->setClass($class);
$element = $extract('foo', 'bar');
$this->assertInstanceOf(ContextElementInterface::class, $element);
$this->assertNotInstanceOf(ContextElement::class, $element);
$this->assertInstanceOf($class, $element);
}

/**
* @expectedException \RuntimeException
*/
public function testExtractorWithAnInvalidContextClass()
{
$context = new class() {};
$class = get_class($context);
$extract = new KeyValueExtractor($class);
$extract('foo', 'bar');
}
}
21 changes: 21 additions & 0 deletions tests/src/Extractor/ObjectPropertyExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,25 @@ public function testExtractor()
'dolor' => 'sit amet',
], $element->getData());
}

/**
* @expectedException \RuntimeException
*/
public function testExtractorWithNonExistentProperty()
{

$extract = new ObjectPropertyExtractor('bar');
$extract('foo', new \stdClass());
}

/**
* @expectedException \RuntimeException
*/
public function testExtractorWithAnInvalidContextClass()
{
$context = new class() {};
$class = get_class($context);
$extract = new ObjectPropertyExtractor('bar', $class);
$extract('foo', (object) ['bar' => 'baz']);
}
}
53 changes: 48 additions & 5 deletions tests/src/Iterator/JsonIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
class JsonIteratorTest extends TestCase
{

public function testIterator()
private static $jsonString;
private static $expectedArray;
private static $expectedObject;

public static function setUpBeforeClass()
{
$json = file_get_contents(TestSuite::getDataFile('dictators.json'));
$iterator = new JsonIterator($json);
$this->assertEquals([
static::$jsonString = file_get_contents(TestSuite::getDataFile('dictators.json'));
static::$expectedArray = [
'usa' =>
[
'country' => 'USA',
Expand All @@ -24,6 +27,46 @@ public function testIterator()
'country' => 'Russia',
'name' => 'Vladimir Poutine',
],
], iterator_to_array($iterator));
];
static::$expectedObject = [
'usa' =>
(object) [
'country' => 'USA',
'name' => 'Donald Trump',
],
'russia' =>
(object) [
'country' => 'Russia',
'name' => 'Vladimir Poutine',
],
];
}

public function testIteratorWithArrayIterator()
{
$json = new \ArrayIterator(json_decode(static::$jsonString, true));
$iterator = new JsonIterator($json);
$this->assertEquals(static::$expectedArray, iterator_to_array($iterator));
}

public function testIteratorWithJsonString()
{
$json = static::$jsonString;
$iterator = new JsonIterator($json);
$this->assertEquals(static::$expectedArray, iterator_to_array($iterator));
}

public function testIteratorWithJsonArray()
{
$json = json_decode(static::$jsonString, true);
$iterator = new JsonIterator($json);
$this->assertEquals(static::$expectedArray, iterator_to_array($iterator));
}

public function testIteratorWithJsonObject()
{
$json = json_decode(static::$jsonString, false);
$iterator = new JsonIterator($json);
$this->assertEquals(static::$expectedObject, iterator_to_array($iterator));
}
}
27 changes: 27 additions & 0 deletions tests/src/Loader/CsvFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BenTools\ETL\Tests\Loader;

use BenTools\ETL\Context\ContextElement;
use BenTools\ETL\Context\ContextElementInterface;
use BenTools\ETL\Extractor\IncrementorExtractor;
use BenTools\ETL\Iterator\JsonIterator;
Expand Down Expand Up @@ -34,4 +35,30 @@ public function testLoader()
$generated = implode(null, iterator_to_array($output));
$this->assertSame($compared, $generated);
}

public function testKeys()
{
// Test constructor
$keys = ['country', 'name'];
$output = new SplTempFileObject();
$loader = new CsvFileLoader($output, null, ',', '"', '\\', $keys);
$this->assertEquals(['country', 'name'], $loader->getKeys());

// Test setter
$loader = $loader->setKeys(['foo', 'bar']);
$this->assertInstanceOf(CsvFileLoader::class, $loader);
$this->assertEquals(['foo', 'bar'], $loader->getKeys());
}

/**
* @expectedException \RuntimeException
*/
public function testSetKeysTooLate()
{

$output = new SplTempFileObject();
$loader = new CsvFileLoader($output);
$loader(new ContextElement('foo', ['bar', 'baz']));
$loader->setKeys(['key1', 'key2']);
}
}
10 changes: 10 additions & 0 deletions tests/src/Loader/DebugLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BenTools\ETL\Tests\Loader;

use BenTools\ETL\Context\ContextElement;
use BenTools\ETL\Extractor\KeyValueExtractor;
use BenTools\ETL\Runner\ETLRunner;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -31,4 +32,13 @@ public function testLoader()
], $debug);
}

/**
* @expectedException \RuntimeException
*/
public function testInvalidCallable()
{
$load = new DebugLoader([], 'not_callable');
$load(new ContextElement('foo', 'bar'));
$load->flush();
}
}
Loading

0 comments on commit e762a94

Please sign in to comment.