Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test with service provider #65

Merged
merged 1 commit into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
}
25 changes: 25 additions & 0 deletions tests/HyphenatorUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,29 @@ 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);
}
}