Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/5937' into develop
Browse files Browse the repository at this point in the history
Close #5937
  • Loading branch information
weierophinney committed Mar 10, 2014
2 parents 7b1e202 + 53b332e commit e74ef75
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
22 changes: 15 additions & 7 deletions library/Zend/Mail/Headers.php
Expand Up @@ -237,18 +237,26 @@ public function addHeader(Header\HeaderInterface $header)
/**
* Remove a Header from the container
*
* @param string $fieldName
* @param string|Header\HeaderInterface field name or specific header instance to remove
* @return bool
*/
public function removeHeader($fieldName)
public function removeHeader($instanceOrFieldName)
{
$key = $this->normalizeFieldName($fieldName);
$index = array_search($key, $this->headersKeys, true);
if ($index !== false) {
unset($this->headersKeys[$index]);
unset($this->headers[$index]);
if ($instanceOrFieldName instanceof Header\HeaderInterface) {
$indexes = array_keys($this->headers, $instanceOrFieldName, true);
} else {
$key = $this->normalizeFieldName($instanceOrFieldName);
$indexes = array_keys($this->headersKeys, $key, true);
}

if (!empty($indexes)) {
foreach ($indexes as $index) {
unset ($this->headersKeys[$index]);
unset ($this->headers[$index]);
}
return true;
}

return false;
}

Expand Down
27 changes: 25 additions & 2 deletions tests/ZendTest/Mail/HeadersTest.php
Expand Up @@ -195,11 +195,34 @@ public function testHeadersCanRemoveHeader()
{
$headers = new Mail\Headers();
$headers->addHeaders(array('Foo' => 'bar', 'Baz' => 'baz'));
$header = $headers->get('foo');
$this->assertEquals(2, $headers->count());
$headers->removeHeader($header->getFieldName());
$headers->removeHeader('foo');
$this->assertEquals(1, $headers->count());
$this->assertFalse($headers->has('foo'));
$this->assertTrue($headers->has('baz'));
}

public function testRemoveHeaderWithFieldNameWillRemoveAllInstances()
{
$headers = new Mail\Headers();
$headers->addHeaders(array(array('Foo' => 'foo'), array('Foo' => 'bar'), 'Baz' => 'baz'));
$this->assertEquals(3, $headers->count());
$headers->removeHeader('foo');
$this->assertEquals(1, $headers->count());
$this->assertFalse($headers->get('foo'));
$this->assertTrue($headers->has('baz'));
}

public function testRemoveHeaderWithInstanceWillRemoveThatInstance()
{
$headers = new Mail\Headers();
$headers->addHeaders(array(array('Foo' => 'foo'), array('Foo' => 'bar'), 'Baz' => 'baz'));
$header = $headers->get('foo')->current();
$this->assertEquals(3, $headers->count());
$headers->removeHeader($header);
$this->assertEquals(2, $headers->count());
$this->assertTrue($headers->has('foo'));
$this->assertNotSame($header, $headers->get('foo'));
}

public function testHeadersCanClearAllHeaders()
Expand Down

0 comments on commit e74ef75

Please sign in to comment.