Skip to content

Commit

Permalink
Add test with service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
heiglandreas committed Feb 20, 2023
1 parent 1b2cc2f commit c5c932f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/HyphenatorService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/**
* Copyright Andreas Heigl <andreas@heigl.org>
*
* Licensed under the MIT-license. For details see the included file LICENSE.md
*/

namespace Org\Heigl\HyphenatorTest;

use Org\Heigl\Hyphenator\Hyphenator;
use Org\Heigl\Hyphenator\Options;

final class HyphenatorService
{
private static $instance = null;

public function __construct(Hyphenator $hyphenator, array $customPattern)
{
$o = new Options();
$o->setHyphen('-')
->setDefaultLocale('de_DE')
->setRightMin(2)
->setLeftMin(2)
->setWordMin(4)
->setFilters('NonStandard')
->setTokenizers('Whitespace, Punctuation');

$hyphenator->setOptions($o);

$dictionary = $hyphenator->getDictionaries()->current();
foreach ($customPattern as $string => $pattern) {
$dictionary->addPattern($string, $pattern);
}

$this->hyphenator = $hyphenator;
}

public static function singleton(): self
{
if (self::$instance === null) {
self::$instance = new self(new Hyphenator(), [
'spender' => '08000000',
]);
}

return self::$instance;
}

public function hyphenate(string $word): array
{
return $this->hyphenator->hyphenate($word);
}
}
13 changes: 13 additions & 0 deletions tests/HyphenatorUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,17 @@ public function testUsingTwoHyphenationPatterns()

$this->assertEquals('Hand-tuch-spen-der', $hyphenator->hyphenate('Handtuchspender'));
}

public function testMultipleHyphenationCallsResultInSameHyphenation()
{
$service = HyphenatorService::singleton();

$this->assertEquals(['Hand-tuchspender', 'Handtuch-spender', 'Handtuchspen-der'], $service->hyphenate('Handtuchspender'));
$this->assertEquals(['Hand-tuchspender', 'Handtuch-spender', 'Handtuchspen-der'], $service->hyphenate('Handtuchspender'));

$service2 = HyphenatorService::singleton();
$this->assertEquals(['Hand-tuchspender', 'Handtuch-spender', 'Handtuchspen-der'], $service->hyphenate('Handtuchspender'));

$this->assertSame($service, $service2);
}
}

0 comments on commit c5c932f

Please sign in to comment.