Skip to content

Commit 7d620d3

Browse files
committed
Add EntityRedirect & Test
This is a copy and paste from Wikibase Lib
1 parent d71d4f2 commit 7d620d3

2 files changed

Lines changed: 154 additions & 0 deletions

File tree

src/Entity/EntityRedirect.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Wikibase\DataModel\Entity;
4+
5+
use InvalidArgumentException;
6+
7+
/**
8+
* Represents a redirect from one EntityId to another.
9+
*
10+
* @since 4.2
11+
*
12+
* @licence GNU GPL v2+
13+
* @author Daniel Kinzler
14+
*/
15+
class EntityRedirect {
16+
17+
/**
18+
* @var EntityId
19+
*/
20+
private $entityId;
21+
22+
/**
23+
* @var EntityId
24+
*/
25+
private $targetId;
26+
27+
/**
28+
* @param EntityId $entityId
29+
* @param EntityId $targetId
30+
*
31+
* @throws InvalidArgumentException
32+
*/
33+
public function __construct( EntityId $entityId, EntityId $targetId ) {
34+
if ( $entityId->getEntityType() !== $targetId->getEntityType() ) {
35+
throw new InvalidArgumentException(
36+
'$entityId and $targetId must refer to the same kind of entity.'
37+
);
38+
}
39+
40+
$this->entityId = $entityId;
41+
$this->targetId = $targetId;
42+
}
43+
44+
/**
45+
* @return EntityId
46+
*/
47+
public function getEntityId() {
48+
return $this->entityId;
49+
}
50+
51+
/**
52+
* @return EntityId
53+
*/
54+
public function getTargetId() {
55+
return $this->targetId;
56+
}
57+
58+
/**
59+
* @param EntityRedirect $that
60+
*
61+
* @return bool
62+
*/
63+
public function equals( $that ) {
64+
if ( $that === $this ) {
65+
return true;
66+
}
67+
68+
return is_object( $that )
69+
&& get_class( $that ) === get_called_class()
70+
&& $this->entityId->equals( $that->entityId )
71+
&& $this->targetId->equals( $that->targetId );
72+
}
73+
74+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Wikibase\DataModel\Tests\Entity;
4+
5+
use Wikibase\DataModel\Entity\EntityRedirect;
6+
use Wikibase\DataModel\Entity\ItemId;
7+
use Wikibase\DataModel\Entity\PropertyId;
8+
9+
/**
10+
* @covers Wikibase\Lib\Store\EntityRedirect
11+
*
12+
* @group Wikibase
13+
* @group WikibaseDataModel
14+
*
15+
* @licence GNU GPL v2+
16+
* @author Daniel Kinzler
17+
*/
18+
class EntityRedirectTest extends \PHPUnit_Framework_TestCase {
19+
20+
public function testConstruction() {
21+
$entityId = new ItemId( 'Q123' );
22+
$targetId = new ItemId( 'Q345' );
23+
24+
$redirect = new EntityRedirect( $entityId, $targetId );
25+
26+
$this->assertEquals( $entityId, $redirect->getEntityId(), '$redirect->getEntityId()' );
27+
$this->assertEquals( $targetId, $redirect->getTargetId(), '$redirect->getTargetId()' );
28+
}
29+
30+
public function testConstruction_baseType() {
31+
$this->setExpectedException( 'InvalidArgumentException' );
32+
33+
$entityId = new ItemId( 'Q123' );
34+
$targetId = new PropertyId( 'P345' );
35+
36+
new EntityRedirect( $entityId, $targetId );
37+
}
38+
39+
public function equalsProvider() {
40+
$q123 = new ItemId( 'Q123' );
41+
$q345 = new ItemId( 'Q345' );
42+
$q567 = new ItemId( 'Q567' );
43+
$q123_345 = new EntityRedirect( $q123, $q345 );
44+
45+
$p123 = new PropertyId( 'P123' );
46+
$p345 = new PropertyId( 'P345' );
47+
$p123_345 = new EntityRedirect( $p123, $p345 );
48+
49+
return array(
50+
'same' => array( $q123_345, $q123_345, true ),
51+
'equal' => array( $q123_345, new EntityRedirect( $q123, $q345 ), true ),
52+
53+
'different base' => array( $q123_345, new EntityRedirect( $q567, $q345 ), false ),
54+
'different target' => array( $q123_345, new EntityRedirect( $q123, $q567 ), false ),
55+
56+
'different entity type' => array( $q123_345, $p123_345, false ),
57+
'different number' => array( $q123_345, new EntityRedirect( $q345, $q123 ), false ),
58+
59+
'null' => array( $q123_345, null, false ),
60+
'string' => array( $q123_345, 'foo', false ),
61+
'id' => array( $q123_345, $q123, false ),
62+
);
63+
}
64+
65+
/**
66+
* @dataProvider equalsProvider
67+
*
68+
* @param EntityRedirect $a
69+
* @param mixed $b
70+
* @param bool $expected
71+
*/
72+
public function testEquals( $a, $b, $expected ) {
73+
$this->assertEquals( $expected, $a->equals( $b ), '$a->equals( $b )' );
74+
75+
if ( $b instanceof EntityRedirect ) {
76+
$this->assertEquals( $expected, $b->equals( $a ), '$b->equals( $a )' );
77+
}
78+
}
79+
80+
}

0 commit comments

Comments
 (0)