trove/tests/Unit/Services/LanguageDetectionServiceTest.php

75 lines
2.6 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Tests\Unit\Services;
use App\Services\LanguageDetectionService;
use Tests\TestCase;
class LanguageDetectionServiceTest extends TestCase
{
private LanguageDetectionService $service;
protected function setUp(): void
{
parent::setUp();
$this->service = new LanguageDetectionService;
}
public function test_detects_english_from_english_paragraph(): void
{
$text = 'The solar system is the gravitationally bound system of the Sun and the
objects that orbit it. Of the bodies that orbit the Sun directly, the largest
are the eight planets, with the remainder being smaller objects, the dwarf
planets and small solar system bodies. Planets and most other large bodies
in the solar system orbit the Sun in the same direction, counterclockwise
when viewed from above the Sun\'s north pole.';
$result = $this->service->detect($text);
$this->assertIsArray($result);
$this->assertCount(2, $result);
$this->assertTrue(
str_starts_with($result[0], 'en'),
"Expected an English-family tag, got '{$result[0]}'.",
);
$this->assertIsFloat($result[1]);
$this->assertGreaterThan(0.0, $result[1]);
$this->assertLessThanOrEqual(1.0, $result[1]);
}
public function test_detects_portuguese_from_portuguese_paragraph(): void
{
$text = 'O sistema solar é o sistema gravitacionalmente ligado composto pelo Sol e
pelos objetos que orbitam ao seu redor. Dos corpos que orbitam o Sol
diretamente, os maiores são os oito planetas, sendo o restante composto por
objetos menores, como planetas anões e corpos menores do sistema solar.
A Terra é o único planeta conhecido a abrigar vida, possuindo uma atmosfera
rica em nitrogênio e oxigênio que sustenta os seres vivos.';
$result = $this->service->detect($text);
$this->assertIsArray($result);
$this->assertCount(2, $result);
$this->assertTrue(
str_starts_with($result[0], 'pt'),
"Expected a Portuguese-family tag, got '{$result[0]}'.",
);
$this->assertIsFloat($result[1]);
$this->assertGreaterThan(0.0, $result[1]);
$this->assertLessThanOrEqual(1.0, $result[1]);
}
public function test_returns_null_for_empty_string(): void
{
$this->assertNull($this->service->detect(''));
}
public function test_returns_null_for_whitespace_only_string(): void
{
$this->assertNull($this->service->detect(' '));
}
}