diff --git a/app/Handlers/UlbXmlImportHandler.php b/app/Handlers/UlbXmlImportHandler.php
index 575f63e..7b1657b 100644
--- a/app/Handlers/UlbXmlImportHandler.php
+++ b/app/Handlers/UlbXmlImportHandler.php
@@ -75,7 +75,7 @@ class UlbXmlImportHandler
*/
protected $availableFiles = [];
- public function __construct(OutputStyle $output)
+ public function __construct(OutputStyle $output = null)
{
$this->output = $output;
}
@@ -100,7 +100,7 @@ class UlbXmlImportHandler
foreach ($this->availableFiles as $name => $filepath) {
$this->importBook($name, $filepath);
}
- $this->output->writeln("DONE.", OutputInterface::VERBOSITY_NORMAL);
+ $this->writeln("DONE");
}
/**
@@ -112,7 +112,7 @@ class UlbXmlImportHandler
{
$this->currentBookNumber = $this->parseBookNumberFromFilepath($filepath);
- $this->output->write("Importing $bookTitle from $filepath Chapters: ", false, OutputInterface::VERBOSITY_NORMAL);
+ $this->write("Importing $bookTitle from $filepath Chapters: ", false);
$this->document = $this->openBook($filepath);
@@ -128,7 +128,7 @@ class UlbXmlImportHandler
$book->chapters()->save($chapter);
}
- $this->output->write("Done.", true, OutputInterface::VERBOSITY_NORMAL);
+ $this->write("Done.", true);
return $book;
}
@@ -142,7 +142,7 @@ class UlbXmlImportHandler
$chapterName = $this->parseChapterName($chapterElem);
$this->currentChapterNumber = $chapterName;
- $this->output->write($chapterName . ".", false, OutputInterface::VERBOSITY_NORMAL);
+ $this->write($chapterName . ".", false);
$chapter = Chapter::create([
'id' => implode('-', [$book_id, $chapterName]),
@@ -498,10 +498,24 @@ class UlbXmlImportHandler
*/
protected function parseBookTitle(string $bookTitle) : array
{
- if (is_int((int) substr($bookTitle, 0, 1))) {
+ if (is_numeric(substr($bookTitle, 0, 1))) {
return [ substr($bookTitle, 0, 1), substr($bookTitle, 1) ];
} else {
return [ $bookTitle ];
}
}
+
+ protected function write($message, $newline)
+ {
+ if ($this->output !== null && $this->output instanceof OutputStyle) {
+ $this->output->write("$message", $newline, OutputInterface::VERBOSITY_NORMAL);
+ }
+ }
+
+ protected function writeln($message)
+ {
+ if ($this->output !== null && $this->output instanceof OutputStyle) {
+ $this->output->writeln("$message", OutputInterface::VERBOSITY_NORMAL);
+ }
+ }
}
diff --git a/tests/Unit/UlbXmlImportHandlerTest.php b/tests/Unit/UlbXmlImportHandlerTest.php
new file mode 100644
index 0000000..2d28379
--- /dev/null
+++ b/tests/Unit/UlbXmlImportHandlerTest.php
@@ -0,0 +1,38 @@
+getMethod('parseBookTitle');
+ $method->setAccessible(true);
+
+ $result = $method->invoke($handler, '1corinthians');
+ $this->assertEquals(['1', 'corinthians'], $result);
+
+ $result = $method->invoke($handler, 'john');
+ $this->assertEquals(['john'], $result);
+ }
+
+ public function testThatPrepareBookNameReturnsProperlyFormattedName()
+ {
+ $handler = new UlbXmlImportHandler();
+
+ $reflection_class = new \ReflectionClass(UlbXmlImportHandler::class);
+ $method = $reflection_class->getMethod('prepareBookName');
+ $method->setAccessible(true);
+
+ $result = $method->invoke($handler, '1corinthians');
+ $this->assertEquals('1 Corinthians', $result);
+
+ $result = $method->invoke($handler, 'john');
+ $this->assertEquals('John', $result);
+ }
+}