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

Improve Coverage for HashTable, Fix Clone #2130

Merged
merged 2 commits into from
May 30, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/PhpSpreadsheet/HashTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,15 @@ public function __clone()
{
$vars = get_object_vars($this);
foreach ($vars as $key => $value) {
if (is_object($value)) {
$this->$key = clone $value;
// each member of this class is an array
if (is_array($value)) {
$array1 = $value;
foreach ($array1 as $key1 => $value1) {
if (is_object($value1)) {
$array1[$key1] = clone $value1;
}
}
$this->$key = $array1;
}
}
}
Expand Down
92 changes: 92 additions & 0 deletions tests/PhpSpreadsheetTests/HashTableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests;

use PhpOffice\PhpSpreadsheet\Comment; // need Comparable object
use PhpOffice\PhpSpreadsheet\HashTable;
use PHPUnit\Framework\TestCase;

class HashTableTest extends TestCase
{
public static function createArray(): array
{
$comment1 = new Comment();
$comment1->setAuthor('Author1');
$comment2 = new Comment();
$comment2->setAuthor('Author2');

return [$comment1, $comment2];
}

/**
* @param mixed $comment
*/
public static function getAuthor($comment): string
{
return ($comment instanceof Comment) ? $comment->getAuthor() : '';
}

public function testAddRemoveClear(): void
{
$array1 = self::createArray();
$hash1 = new HashTable($array1);
self::assertSame(2, $hash1->count());
$comment3 = new Comment();
$comment3->setAuthor('Author3');
$hash1->add($comment3);
$comment4 = new Comment();
$comment4->setAuthor('Author4');
$hash1->add($comment4);
$comment5 = new Comment();
$comment5->setAuthor('Author5');
// don't add comment5
self::assertSame(4, $hash1->count());
self::assertNull($hash1->getByIndex(10));
$comment = $hash1->getByIndex(2);
self::assertSame('Author3', self::getAuthor($comment));
$hash1->remove($comment3);
self::assertSame(3, $hash1->count());
$comment = $hash1->getByIndex(2);
self::assertSame('Author4', self::getAuthor($comment));
$hash1->remove($comment5);
self::assertSame(3, $hash1->count(), 'Remove non-hash member');
$comment = $hash1->getByIndex(2);
self::assertSame('Author4', self::getAuthor($comment));
self::assertNull($hash1->getByHashCode('xyz'));
$hash1->clear();
self::AssertSame(0, $hash1->count());
}

public function testToArray(): void
{
$array1 = self::createArray();
$count1 = count($array1);
$hash1 = new HashTable($array1);
$array2 = $hash1->toArray();
self::assertCount($count1, $array2);
$idx = 0;
foreach ($array2 as $key => $value) {
self::assertEquals($array1[$idx], $value, "Item $idx");
self::assertSame($idx, $hash1->getIndexForHashCode($key));
++$idx;
}
}

public function testClone(): void
{
$array1 = self::createArray();
$hash1 = new HashTable($array1);
$hash2 = new HashTable();
self::assertSame(0, $hash2->count());
$hash2->addFromSource();
self::assertSame(0, $hash2->count());
$hash2->addFromSource($array1);
self::assertSame(2, $hash2->count());
self::assertEquals($hash1, $hash2, 'Add in constructor same as addFromSource');
$hash3 = clone $hash1;
self::assertEquals($hash1, $hash3, 'Clone equal to original');
self::assertSame($hash1->getByIndex(0), $hash2->getByIndex(0));
self::assertEquals($hash1->getByIndex(0), $hash3->getByIndex(0));
self::assertNotSame($hash1->getByIndex(0), $hash3->getByIndex(0));
}
}