Fix bug when handling book titles

This commit is contained in:
Leonard Smith 2021-05-04 08:25:48 -05:00
parent 9fec3f7c05
commit 5d69a0e89f
2 changed files with 58 additions and 6 deletions

View File

@ -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("<info>DONE.</info>", OutputInterface::VERBOSITY_NORMAL);
$this->writeln("DONE");
}
/**
@ -112,7 +112,7 @@ class UlbXmlImportHandler
{
$this->currentBookNumber = $this->parseBookNumberFromFilepath($filepath);
$this->output->write("<info>Importing $bookTitle from $filepath</info> 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("<info>Done.</info>", 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("<info>$message</info>", $newline, OutputInterface::VERBOSITY_NORMAL);
}
}
protected function writeln($message)
{
if ($this->output !== null && $this->output instanceof OutputStyle) {
$this->output->writeln("<info>$message</info>", OutputInterface::VERBOSITY_NORMAL);
}
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace Tests\Unit;
use App\Handlers\UlbXmlImportHandler;
use Tests\TestCase;
class UlbXmlImportHandlerTest extends TestCase
{
public function testThatParseBookTitleHandlesNumberedBooksCorrectly()
{
$handler = new UlbXmlImportHandler();
$reflection_class = new \ReflectionClass(UlbXmlImportHandler::class);
$method = $reflection_class->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);
}
}