Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Jun 27, 2024
1 parent fca2a5a commit fb785ce
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
11 changes: 9 additions & 2 deletions app/Domains/Documents/Transformers/DocXTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Jobs\VectorlizeDataJob;
use App\Models\Document;
use App\Models\DocumentChunk;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use PhpOffice\PhpWord\IOFactory;

Expand All @@ -22,6 +23,10 @@ public function handle(Document $document): array

$parser = IOFactory::createReader('Word2007');

if(!File::exists($filePath)) {
throw new \Exception('Can not fine the document '.$filePath);
}

if (! $parser->canRead($filePath)) {
throw new \Exception('Can not read the document '.$filePath);
}
Expand All @@ -44,7 +49,7 @@ public function handle(Document $document): array
* what about tables
* what about lists
*/
$content[] = $element->getText();
$content[] = str($element->getText())->trim()->toString();

}
}
Expand Down Expand Up @@ -73,7 +78,9 @@ public function handle(Document $document): array
];
}

notify_collection_ui($document->collection, CollectionStatusEnum::PROCESSING, 'Processing Document');

notify_collection_ui($this->document->collection,
CollectionStatusEnum::PROCESSING, 'Processing Document');

Log::info('DocXTransformer:handle', ['chunks' => count($chunks)]);

Expand Down
Binary file modified tests/.DS_Store
Binary file not shown.
34 changes: 18 additions & 16 deletions tests/Feature/DocXTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@

use App\Domains\Documents\Transformers\DocXTransformer;
use App\Domains\Documents\Transformers\PowerPointTransformer;
use App\Domains\Documents\TypesEnum;
use App\Models\Document;
use App\Models\DocumentChunk;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\File;
use Tests\TestCase;

class DocXTransformerTest extends TestCase
{
use SharedSetupForPptFile;

protected function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
//$this->markTestIncomplete('@TODO It works with a test PPTX but I might try a different approach since more complicated PPTX did not');
}

protected function tearDown(): void
{
if (! File::exists($this->document->pathToFile())) {
Expand All @@ -29,26 +26,31 @@ protected function tearDown(): void

public function test_gets_data_from_docx()
{
Event::fake();
Bus::fake();
$this->setupFile('example.docx');
$document = Document::factory()->docx()->create();
$document = $this->setupFile('example.docx');

$this->assertDatabaseCount('document_chunks', 0);
$transformer = new DocXTransformer();
$transformer->handle($this->document);
$transformer->handle($document);
$this->assertDatabaseCount('document_chunks', 1);

$this->assertEquals("Lorem ipsum dolor sit amet.", DocumentChunk::first()->content);

}

public function test_does_not_repeat()
{
Event::fake();
Bus::fake();
$this->setupFile();
$document = Document::factory()->pptx()->create();
$document = $this->setupFile('example.docx');

$this->assertDatabaseCount('document_chunks', 0);
$transformer = new PowerPointTransformer();
$transformer->handle($this->document);
$this->assertDatabaseCount('document_chunks', 5);
$transformer->handle($this->document);
$this->assertDatabaseCount('document_chunks', 5);
$transformer = new DocXTransformer();
$transformer->handle($document);
$this->assertDatabaseCount('document_chunks', 1);
$transformer->handle($document);
$this->assertDatabaseCount('document_chunks', 1);

}
}
Binary file modified tests/fixtures/.DS_Store
Binary file not shown.
1 change: 0 additions & 1 deletion tests/fixtures/sample_data/.~lock.example.docx#

This file was deleted.

0 comments on commit fb785ce

Please sign in to comment.