Skip to content

Commit 1563640

Browse files
committed
Revert "Remove Entity::copy"
1 parent 614d1e4 commit 1563640

4 files changed

Lines changed: 39 additions & 7 deletions

File tree

RELEASE-NOTES.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
# Wikibase DataModel release notes
22

3-
## Version 5.0 (dev)
4-
5-
#### Breaking changes
6-
7-
* Removed `Entity::copy`
8-
93
## Version 4.0 (2015-07-28)
104

115
#### Breaking changes

src/Entity/Entity.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,18 @@ public function setAllAliases( array $aliasLists ) {
288288
}
289289
}
290290

291+
/**
292+
* Returns a deep copy of the entity.
293+
*
294+
* @since 0.1
295+
* @deprecated since 1.0
296+
*
297+
* @return self
298+
*/
299+
public function copy() {
300+
return unserialize( serialize( $this ) );
301+
}
302+
291303
/**
292304
* @since 0.3
293305
* @deprecated since 1.0, use getStatements()->toArray() instead.

tests/unit/Entity/EntityTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,32 @@ public function instanceProvider() {
360360
return $argLists;
361361
}
362362

363+
/**
364+
* @dataProvider instanceProvider
365+
* @param Entity $entity
366+
*/
367+
public function testCopy( Entity $entity ) {
368+
$copy = $entity->copy();
369+
370+
// The equality method alone is not enough since it does not check the IDs.
371+
$this->assertTrue( $entity->equals( $copy ) );
372+
$this->assertEquals( $entity->getId(), $copy->getId() );
373+
374+
$this->assertFalse( $entity === $copy );
375+
}
376+
377+
public function testCopyRetainsLabels() {
378+
$item = new Item();
379+
380+
$item->getFingerprint()->setLabel( 'en', 'foo' );
381+
$item->getFingerprint()->setLabel( 'de', 'bar' );
382+
383+
$newItem = unserialize( serialize( $item ) );
384+
385+
$this->assertTrue( $newItem->getFingerprint()->getLabels()->hasTermForLanguage( 'en' ) );
386+
$this->assertTrue( $newItem->getFingerprint()->getLabels()->hasTermForLanguage( 'de' ) );
387+
}
388+
363389
/**
364390
* @dataProvider instanceProvider
365391
* @param Entity $entity

tests/unit/Entity/ItemTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function itemProvider() {
8080
$items[] = $item;
8181

8282
/** @var Item $item */
83-
$item = unserialize( serialize( $item ) );
83+
$item = $item->copy();
8484
$item->getStatements()->addNewStatement(
8585
new PropertyNoValueSnak( new PropertyId( 'P42' ) )
8686
);

0 commit comments

Comments
 (0)