From f84c3e9d4249e3b22c41afc5030111089f628070 Mon Sep 17 00:00:00 2001 From: mundschenk_at Date: Sun, 17 Mar 2024 22:36:48 +0100 Subject: [PATCH] Simplify Text_Parser by removing unused methods --- src/class-text-parser.php | 66 ++---- .../node-fixes/class-process-words-fix.php | 30 +-- tests/class-text-parser-test.php | 217 ++++-------------- .../class-process-words-fix-test.php | 15 +- 4 files changed, 73 insertions(+), 255 deletions(-) diff --git a/src/class-text-parser.php b/src/class-text-parser.php index d02fc4d7..3b0b01b7 100644 --- a/src/class-text-parser.php +++ b/src/class-text-parser.php @@ -31,10 +31,11 @@ use PHP_Typography\Text_Parser\Token; /** - * A class to parse plain text (such as the data of DOMText). + * A class to parse plain text (such as the data of DOMText). If multibyte characters are passed, + * they must be encoded as UTF-8. * - * Parse_Text assumes no HTML markup in the text (except for special html characters like >). - * If multibyte characters are passed, they must be encoded as UTF-8. + * @since 7.0.0 The `load`, `reload`, and `clear` methods have been removed in favor creating new parser + * objects for each text fragment. The method `unload` has been replaced with `get_text`. */ class Text_Parser { @@ -261,41 +262,26 @@ class Text_Parser { * * @var callable */ - private $current_strtoupper = 'strtoupper'; + private $current_strtoupper; /** * The tokenized text. * * @var Token[] $text Numerically indexed tokens. */ - private $text = []; + private array $text = []; /** - * Creates a new parser object. - */ - public function __construct() { - } - - /** - * Tokenizes a string and stores the tokens in $this->text. - * - * @param string $raw_text A text fragment without any HTML markup. + * Creates a new parser object and parses the given text. * - * @return bool Returns `true` on successful completion, `false` otherwise. + * @param string $text A text fragment without any HTML markup. */ - public function load( string $raw_text ) { - if ( empty( $raw_text ) ) { - return false; // Can't tokenize an empty string. - } - + public function __construct( string $text ) { // Detect encoding. - $this->current_strtoupper = Strings::functions( $raw_text )['strtoupper']; + $this->current_strtoupper = Strings::functions( $text )['strtoupper']; // Tokenize the raw text parts. - $this->text = self::tokenize( \preg_split( self::RE_ANY_TEXT, $raw_text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ) ?: [] ); // phpcs:ignore Universal.Operators.DisallowShortTernary -- Ensure array type in case of error. - - // The token array should never be empty. - return ! empty( $this->text ); + $this->text = self::tokenize( \preg_split( self::RE_ANY_TEXT, $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ) ?: [] ); // phpcs:ignore Universal.Operators.DisallowShortTernary -- Ensure array type in case of error. } /** @@ -387,42 +373,23 @@ protected static function is_not_preceeded_by( $type, array $tokens, $index, $st return $index - $steps >= 0 && $type !== $tokens[ $index - $steps ]->type; } - /** - * Reloads $this->text (i.e. capture new inserted text, or remove those tokens whose values have been deleted). + * Returns the complete text as a string. * - * Warning: Tokens previously acquired through 'get' methods may not match new tokenization. - * - * @return bool Returns true on successful completion. - */ - public function reload() { - return $this->load( $this->unload() ); - } - - /** - * Returns the complete text as a string and clears the parser. + * @since 7.0.0 * * @return string */ - public function unload() { + public function get_text(): string { $reassembled_text = ''; foreach ( $this->text as $token ) { $reassembled_text .= $token->value; } - $this->clear(); - return $reassembled_text; } - /** - * Clears the currently set text from the parser. - */ - public function clear(): void { - $this->text = []; - } - /** * Updates the 'value' field for all matching tokens. * @@ -471,11 +438,6 @@ public function get_punctuation() { * @return Token[] An array of numerically indexed tokens. */ public function get_words( $abc = self::ALLOW_ALL_LETTERS, $caps = self::ALLOW_ALL_CAPS, $comps = self::ALLOW_COMPOUNDS ) { - // Return early if no text has been loaded. - if ( empty( $this->text ) ) { - return []; // abort. - } - // Result set. $tokens = []; diff --git a/src/fixes/node-fixes/class-process-words-fix.php b/src/fixes/node-fixes/class-process-words-fix.php index a6caaed4..47864800 100644 --- a/src/fixes/node-fixes/class-process-words-fix.php +++ b/src/fixes/node-fixes/class-process-words-fix.php @@ -55,13 +55,6 @@ class Process_Words_Fix extends Abstract_Node_Fix { */ private $token_fixes = []; - /** - * A custom parser for \DOMText to separate words, whitespace etc. for HTML injection. - * - * @var Text_Parser|null - */ - private $text_parser; - /** * Apply the fix to a given textnode. * @@ -74,10 +67,6 @@ class Process_Words_Fix extends Abstract_Node_Fix { * @return void */ public function apply( \DOMText $textnode, Settings $settings, $is_title ) { - // Lazy-load text parser. - $text_parser = $this->get_text_parser(); - $tokens = []; - // Set up parameters for word categories. $mixed_caps = empty( $settings[ Settings::HYPHENATE_ALL_CAPS ] ) ? Text_Parser::ALLOW_ALL_CAPS : Text_Parser::NO_ALL_CAPS; $letter_caps = empty( $settings[ Settings::HYPHENATE_ALL_CAPS ] ) ? Text_Parser::NO_ALL_CAPS : Text_Parser::ALLOW_ALL_CAPS; @@ -85,7 +74,7 @@ public function apply( \DOMText $textnode, Settings $settings, $is_title ) { $letter_compounds = empty( $settings[ Settings::HYPHENATE_COMPOUNDS ] ) ? Text_Parser::NO_COMPOUNDS : Text_Parser::ALLOW_COMPOUNDS; // Break text down for a bit more granularity. - $text_parser->load( $textnode->data ); + $text_parser = $this->get_text_parser( $textnode->data ); $tokens[ Token_Fix::MIXED_WORDS ] = $text_parser->get_words( Text_Parser::NO_ALL_LETTERS, $mixed_caps, $mixed_compounds ); // prohibit letter-only words, allow caps, allow compounds (or not). $tokens[ Token_Fix::COMPOUND_WORDS ] = ! empty( $settings[ Settings::HYPHENATE_COMPOUNDS ] ) ? $text_parser->get_words( Text_Parser::NO_ALL_LETTERS, $letter_caps, Text_Parser::REQUIRE_COMPOUNDS ) : []; $tokens[ Token_Fix::WORDS ] = $text_parser->get_words( Text_Parser::REQUIRE_ALL_LETTERS, $letter_caps, $letter_compounds ); // require letter-only words allow/prohibit caps & compounds vice-versa. @@ -100,21 +89,20 @@ public function apply( \DOMText $textnode, Settings $settings, $is_title ) { // Apply updates to our text. $text_parser->update( $tokens[ Token_Fix::MIXED_WORDS ] + $tokens[ Token_Fix::COMPOUND_WORDS ] + $tokens[ Token_Fix::WORDS ] + $tokens[ Token_Fix::OTHER ] ); - $textnode->data = $text_parser->unload(); + $textnode->data = $text_parser->get_text(); } /** * Retrieves the text parser instance. * - * @return \PHP_Typography\Text_Parser + * @since 7.0.0 Parameter $text added. + * + * @param string $text The text to tokenize. + * + * @return Text_Parser */ - public function get_text_parser() { - // Lazy-load text parser. - if ( ! isset( $this->text_parser ) ) { - $this->text_parser = new Text_Parser(); - } - - return $this->text_parser; + public function get_text_parser( string $text ) { + return new Text_Parser( $text ); } /** diff --git a/tests/class-text-parser-test.php b/tests/class-text-parser-test.php index 56bb66a3..fa6f9ba8 100644 --- a/tests/class-text-parser-test.php +++ b/tests/class-text-parser-test.php @@ -39,39 +39,11 @@ * @uses PHP_Typography\Strings::functions */ class Text_Parser_Test extends Testcase { - /** - * The Text_Parser fixture. - * - * @var Text_Parser - */ - protected $parser; - - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - */ - protected function set_up() { - parent::set_up(); - - $this->parser = new \PHP_Typography\Text_Parser(); - } - - /** - * Test constructor. - * - * @covers ::__construct - */ - public function test_constructor() { - $parser = new Text_Parser(); - - $this->assert_attribute_count( 0, 'text', $parser ); - $this->assert_attribute_same( 'strtoupper', 'current_strtoupper', $parser ); - } /** * Test load. * - * @covers ::load + * @covers ::__construct * @covers ::tokenize * @covers ::parse_ambiguous_token * @covers ::is_preceeded_by @@ -80,21 +52,17 @@ public function test_constructor() { * @uses ::get_all */ public function test_load() { - $too_long = 'A really long string with a word that is wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww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wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwway too long.'; + $still_too_long = 'A really long string with a word that is aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalmost too long.'; $almost_too_long = 'A really long string with a word that is aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalmost too long.'; - $parser = $this->parser; - // Previously, we didn't allow really long strings, but this is unnecessary with PHP. - $this->assertTrue( $parser->load( $too_long ) ); - $this->assertTrue( $parser->load( $still_too_long ) ); - $this->assertTrue( $parser->load( $almost_too_long ) ); + $this->assertInstanceOf( Text_Parser::class, new Text_Parser( $too_long ) ); + $this->assertInstanceOf( Text_Parser::class, new Text_Parser( $still_too_long ) ); + $this->assertInstanceOf( Text_Parser::class, new Text_Parser( $almost_too_long ) ); $interesting = 'Quoth the raven, "nevermore"! Äöüß?'; - $this->assertTrue( $parser->load( $interesting ) ); + $parser = new Text_Parser( $interesting ); $tokens = $parser->get_all(); @@ -109,7 +77,7 @@ public function test_load() { /** * Test load with email address. * - * @covers ::load + * @covers ::__construct * @covers ::tokenize * @covers ::parse_ambiguous_token * @covers ::is_preceeded_by @@ -118,10 +86,8 @@ public function test_load() { * @uses ::get_all */ public function test_load_email() { - $parser = $this->parser; - $string = 'Quoth the raven, "nevermore"! Please mail to someone@example.org.'; - $this->assertTrue( $parser->load( $string ) ); + $parser = new Text_Parser( $string ); $tokens = $parser->get_all(); $this->assertCount( 19, $tokens ); @@ -136,7 +102,7 @@ public function test_load_email() { /** * Test load with URL. * - * @covers ::load + * @covers ::__construct * @covers ::tokenize * @covers ::parse_ambiguous_token * @covers ::is_preceeded_by @@ -145,10 +111,8 @@ public function test_load_email() { * @uses ::get_all */ public function test_load_url() { - $parser = $this->parser; - $string = 'Quoth the raven, "nevermore"! Please open http://example.org or foo:WordPress or foo:W@rdPress or @example or @:@:@:risk.'; - $this->assertTrue( $parser->load( $string ) ); + $parser = new Text_Parser( $string ); $tokens = $parser->get_all(); $this->assertCount( 33, $tokens ); @@ -167,7 +131,7 @@ public function test_load_url() { /** * Test load with a compound word. * - * @covers ::load + * @covers ::__construct * @covers ::tokenize * @covers ::parse_ambiguous_token * @covers ::is_preceeded_by @@ -176,10 +140,8 @@ public function test_load_url() { * @uses ::get_all */ public function test_load_compound_word() { - $parser = $this->parser; - $string = 'Some don\'t trust the captain-owner.'; - $this->assertTrue( $parser->load( $string ) ); + $parser = new Text_Parser( $string ); $tokens = $parser->get_all(); $this->assertCount( 10, $tokens ); @@ -194,94 +156,33 @@ public function test_load_compound_word() { /** * Test load with an invalid encoding. * - * @covers ::load + * @covers ::__construct */ public function test_load_invalid_encoding() { $string = mb_convert_encoding( 'Ein längerer String im falschen Zeichensatz', 'ISO-8859-2' ); - $parser = $this->parser; $this->expect_exception( Invalid_Encoding_Exception::class ); - $this->assertFalse( $parser->load( $string ) ); + new Text_Parser( $string ); } /** - * Test reload. + * Test get_text. * - * @covers ::reload + * @covers ::get_text * - * @depends test_load - * @uses ::clear - * @uses ::get_all * @uses ::is_not_preceeded_by * @uses ::is_preceeded_by - * @uses ::load + * @uses ::__construct * @uses ::parse_ambiguous_token * @uses ::tokenize - * @uses ::unload - * @uses ::update - - * @param \PHP_Typography\Text_Parser $parser The parser to use. */ - public function test_reload( Text_Parser $parser ) { - // Parsed string: 'Quoth the raven, "nevermore"! Äöüß?'. - $tokens = $parser->get_all(); - $tokens[12] = $tokens[12]->with_value( '' ); // "?". - $tokens[11] = $tokens[11]->with_value( '' ); // "Äöüß". - $tokens[10] = $tokens[10]->with_value( '' ); // " ". - $tokens[9] = $tokens[9]->with_value( $tokens[9]->value . '!' ); - $parser->update( $tokens ); - - $this->assertTrue( $parser->reload() ); - $this->assertSame( 'Quoth the raven, "nevermore"!!', $parser->unload() ); - - return $parser; - } - - /** - * Test unload. - * - * @covers ::unload - * - * @uses ::clear - * @uses ::is_not_preceeded_by - * @uses ::is_preceeded_by - * @uses ::load - * @uses ::parse_ambiguous_token - * @uses ::tokenize - */ - public function test_unload() { + public function test_get_text() { $interesting = 'Quoth the raven, "nevermore"! Äöüß?'; - $parser = $this->parser; + $parser = new Text_Parser( $interesting ); - $this->assertTrue( $parser->load( $interesting ) ); - - $result = $parser->unload(); + $result = $parser->get_text(); $this->assertSame( $interesting, $result ); - $this->assertNotSame( $result, $parser->unload() ); // the parser is empty now. - } - - /** - * Test clear. - * - * @covers ::clear - * - * @uses ::get_all - * @uses ::is_not_preceeded_by - * @uses ::is_preceeded_by - * @uses ::load - * @uses ::parse_ambiguous_token - * @uses ::tokenize - */ - public function test_clear() { - $parser = $this->parser; - $interesting = 'Quoth the raven, "nevermore"!'; - - $this->assertTrue( $parser->load( $interesting ) ); - $this->assertGreaterThan( 0, count( $parser->get_all() ) ); - - $parser->clear(); - $this->assertCount( 0, $parser->get_all() ); } /** @@ -289,19 +190,17 @@ public function test_clear() { * * @covers ::update * - * @uses ::clear * @uses ::get_all * @uses ::is_not_preceeded_by * @uses ::is_preceeded_by - * @uses ::load + * @uses ::__construct * @uses ::parse_ambiguous_token * @uses ::tokenize - * @uses ::unload + * @uses ::get_text */ public function test_update() { - $parser = $this->parser; $interesting = 'Quoth the raven, "nevermore"! Äöüß?'; - $this->assertTrue( $parser->load( $interesting ) ); + $parser = new Text_Parser( $interesting ); $tokens = $parser->get_all(); $tokens[12] = $tokens[12]->with_value( '' ); // "?". @@ -310,7 +209,7 @@ public function test_update() { $tokens[9] = $tokens[9]->with_value( $tokens[9]->value . '!' ); $parser->update( $tokens ); - $this->assertSame( 'Quoth the raven, "nevermore"!!', $parser->unload() ); + $this->assertSame( 'Quoth the raven, "nevermore"!!', $parser->get_text() ); return $parser; } @@ -320,7 +219,7 @@ public function test_update() { * * @covers ::get_all * - * @uses ::load + * @uses ::__construct * @uses ::is_not_preceeded_by * @uses ::is_preceeded_by * @uses ::parse_ambiguous_token @@ -328,8 +227,7 @@ public function test_update() { */ public function test_get_all() { $interesting = 'Quoth the raven, "nevermore"!'; - $parser = $this->parser; - $this->assertTrue( $parser->load( $interesting ) ); + $parser = new Text_Parser( $interesting ); $tokens = $parser->get_all(); $this->assertCount( 10, $tokens ); @@ -383,7 +281,6 @@ public function test_get_punctuation( Text_Parser $parser ) { * @uses ::check_policy * @uses ::get_type * @uses ::is_preceeded_by - * @uses ::load * @uses ::parse_ambiguous_token * @uses ::tokenize * @@ -393,7 +290,7 @@ public function test_get_words( Text_Parser $parser ) { $tokens = $parser->get_words(); $this->assertCount( 4, $tokens ); - $parser->load( 'A few m1xed W0RDS.' ); + $parser = new Text_Parser( 'A few m1xed W0RDS.' ); $tokens = $parser->get_words( Text_Parser::REQUIRE_ALL_LETTERS, Text_Parser::NO_ALL_CAPS ); $this->assertCount( 1, $tokens ); $this->assert_contains_equals( new Token( 'few', Token::WORD ), $tokens, '' ); @@ -468,7 +365,7 @@ public function provide_conforms_to_letters_policy_data() { * @covers ::check_policy * @dataProvider provide_conforms_to_letters_policy_data * - * @uses ::load + * @uses ::__construct * @uses ::is_preceeded_by * @uses ::parse_ambiguous_token * @uses ::tokenize @@ -479,7 +376,8 @@ public function provide_conforms_to_letters_policy_data() { * @param bool $result Expected result. */ public function test_conforms_to_letters_policy( $value, $type, $policy, $result ) { - $parser = $this->parser; + // Ensure that encoding can be determined. + $parser = new Text_Parser( $value ); $token = new Token( $value, $type ); $this->assertSame( $result, $this->invoke_method( $parser, 'conforms_to_letters_policy', [ $token, $policy ] ) ); @@ -515,7 +413,7 @@ public function provide_conforms_to_caps_policy_data() { * @covers ::check_policy * @dataProvider provide_conforms_to_caps_policy_data * - * @uses ::load + * @uses ::__construct * @uses ::is_preceeded_by * @uses ::parse_ambiguous_token * @uses ::tokenize @@ -526,10 +424,9 @@ public function provide_conforms_to_caps_policy_data() { * @param bool $result Expected result. */ public function test_conforms_to_caps_policy( $value, $type, $policy, $result ) { - $parser = $this->parser; - $parser->load( $value ); // Ensure that encoding can be determined. - - $token = new Token( $value, $type ); + // Ensure that encoding can be determined. + $parser = new Text_Parser( $value ); + $token = new Token( $value, $type ); $this->assertSame( $result, $this->invoke_method( $parser, 'conforms_to_caps_policy', [ $token, $policy ] ) ); } @@ -564,7 +461,7 @@ public function provide_conforms_to_compounds_policy() { * @covers ::check_policy * @dataProvider provide_conforms_to_compounds_policy * - * @uses ::load + * @uses ::__construct * @uses ::is_preceeded_by * @uses ::parse_ambiguous_token * @uses ::tokenize @@ -575,40 +472,13 @@ public function provide_conforms_to_compounds_policy() { * @param bool $result Expected result. */ public function test_conforms_to_compounds_policy( $value, $type, $policy, $result ) { - $parser = $this->parser; - $parser->load( $value ); // Ensure that encoding can be determined. - - $token = new Token( $value, $type ); + // Ensure that encoding can be determined. + $parser = new Text_Parser( $value ); + $token = new Token( $value, $type ); $this->assertSame( $result, $this->invoke_method( $parser, 'conforms_to_compounds_policy', [ $token, $policy ] ) ); } - /** - * Test get_words. - * - * @covers ::get_words - * @depends test_get_all - * - * @uses ::clear - * @uses ::is_preceeded_by - * @uses ::load - * @uses ::parse_ambiguous_token - * @uses ::tokenize - * @uses ::unload - * - * @param \PHP_Typography\Text_Parser $parser The parser to use. - */ - public function test_get_words_unloaded( Text_Parser $parser ) { - $parser->load( 'A few m1xed W0RDS.' ); - $parser->unload(); - - $tokens = $parser->get_words( Text_Parser::REQUIRE_ALL_LETTERS, Text_Parser::NO_ALL_CAPS ); - $this->assertCount( 0, $tokens ); - $this->assertSame( [], $tokens ); - - return $parser; - } - /** * Test get_other. * @@ -630,18 +500,15 @@ public function test_get_other( Text_Parser $parser ) { * Test get_type. * * @covers ::get_type - * @depends test_get_all * * @uses ::get_all * @uses ::is_preceeded_by - * @uses ::load + * @uses ::__construct * @uses ::parse_ambiguous_token * @uses ::tokenize - * - * @param \PHP_Typography\Text_Parser $parser The parser to use. */ - public function test_get_type( Text_Parser $parser ) { - $parser->load( 'A few m1xed W0RDS.' ); + public function test_get_type() { + $parser = new Text_Parser( 'A few m1xed W0RDS.' ); $words = []; $tokens = $parser->get_all(); diff --git a/tests/fixes/node-fixes/class-process-words-fix-test.php b/tests/fixes/node-fixes/class-process-words-fix-test.php index 191358fd..7020a0e6 100644 --- a/tests/fixes/node-fixes/class-process-words-fix-test.php +++ b/tests/fixes/node-fixes/class-process-words-fix-test.php @@ -2,7 +2,7 @@ /** * This file is part of PHP-Typography. * - * Copyright 2015-2020 Peter Putzer. + * Copyright 2015-2024 Peter Putzer. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -83,19 +83,20 @@ public function provide_process_words_data() { * * @covers ::get_text_parser * - * @uses PHP_Typography\Text_Parser::__construct + * @uses PHP_Typography\Text_Parser + * @uses PHP_Typography\Text_Parser\Token */ public function test_get_text_parser() { - $this->assert_attribute_empty( 'text_parser', $this->fix ); + $some_words = 'A short text of no importance at all.'; - $parser1 = $this->fix->get_text_parser(); + $parser1 = $this->fix->get_text_parser( $some_words ); $this->assertInstanceOf( '\PHP_Typography\Text_Parser', $parser1 ); - $parser2 = $this->fix->get_text_parser(); + $parser2 = $this->fix->get_text_parser( $some_words ); $this->assertInstanceOf( '\PHP_Typography\Text_Parser', $parser2 ); - $this->assertSame( $parser1, $parser2 ); - $this->assert_attribute_instance_of( '\PHP_Typography\Text_Parser', 'text_parser', $this->fix ); + // The text parser is not stored as part of the object state anymore. + $this->assertNotSame( $parser1, $parser2 ); } /**