Skip to content

Commit

Permalink
Add ability to replace existing tokens easily
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Oct 14, 2024
1 parent 2113e27 commit 5c5d552
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Token/TokenCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public static function fromSerializedArray(array $data): self
return new self($tokens);
}

public function replaceToken(Token $token): self
{
$existing = $this->getByName($token->getName());

if (null !== $existing) {
$this->remove($existing);
}

$this->addToken($token);

return $this;
}

/**
* Provides a fluent interface alternative to add() with a type hint.
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/Token/TokenCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ public function testCollectionHandling(): void
$this->assertNull($tokenCollection->getByName('form_i_do_not_exist'));
}

public function testReplace(): void
{
$tokenCollection = new TokenCollection();
$tokenCollection->addToken(Token::fromValue('test', 'foobar'));
$tokenCollection->addToken(Token::fromValue('test', 'foobar new'));

$this->assertSame(2, $tokenCollection->count());

Check failure on line 70 in tests/Token/TokenCollectionTest.php

View workflow job for this annotation

GitHub Actions / ci / build-tools

You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).

Check failure on line 70 in tests/Token/TokenCollectionTest.php

View workflow job for this annotation

GitHub Actions / ci / build-tools

You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).
$this->assertSame('foobar', $tokenCollection->getByName('test')->getValue(), 'foobar');

$tokenCollection = new TokenCollection();
$tokenCollection->replaceToken(Token::fromValue('test', 'foobar'));
$tokenCollection->replaceToken(Token::fromValue('test', 'foobar new'));

$this->assertSame(1, $tokenCollection->count());

Check failure on line 77 in tests/Token/TokenCollectionTest.php

View workflow job for this annotation

GitHub Actions / ci / build-tools

You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).

Check failure on line 77 in tests/Token/TokenCollectionTest.php

View workflow job for this annotation

GitHub Actions / ci / build-tools

You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).
$this->assertSame('foobar new', $tokenCollection->getByName('test')->getValue(), 'foobar');
}

public function testMerge(): void
{
$tokenCollectionA = new TokenCollection();
Expand Down

0 comments on commit 5c5d552

Please sign in to comment.