/**
  * Returns a MockRepository. The following entities are defined:
  *
  * - Items Q23
  * - Item Q42
  * - Redirect Q2233 -> Q23
  * - Redirect Q222333 -> Q23
  * - Property P5 (item reference)
  *
  * @return MockRepository
  */
 private function getMockRepository()
 {
     $mockRepo = new MockRepository();
     $p5 = new Property(new PropertyId('P5'), null, 'wikibase-item');
     $p5->getFingerprint()->setLabel('en', 'Label5');
     $mockRepo->putEntity($p5);
     $q23 = new Item(new ItemId('Q23'));
     $q23->getFingerprint()->setLabel('en', 'Label23');
     $mockRepo->putEntity($q23);
     $q2233 = new EntityRedirect(new ItemId('Q2233'), new ItemId('Q23'));
     $mockRepo->putRedirect($q2233);
     $q222333 = new EntityRedirect(new ItemId('Q222333'), new ItemId('Q23'));
     $mockRepo->putRedirect($q222333);
     $q42 = new Item(new ItemId('Q42'));
     $q42->getFingerprint()->setLabel('en', 'Label42');
     $snak = new PropertyValueSnak($p5->getId(), new EntityIdValue($q2233->getEntityId()));
     $q42->getStatements()->addNewStatement($snak, null, null, 'Q42$DEADBEEF');
     $mockRepo->putEntity($q42);
     return $mockRepo;
 }
 public static function getMockRepository()
 {
     $mockRepository = new MockRepository();
     foreach (self::getEntityRevisions() as $entityRev) {
         $mockRepository->putEntity($entityRev->getEntity(), $entityRev->getRevisionId(), $entityRev->getTimestamp());
     }
     foreach (self::getEntityRedirects() as $entityRedir) {
         $mockRepository->putRedirect($entityRedir);
     }
     return $mockRepository;
 }
 /**
  * @param array $ids
  *
  * @return GenericEntityInfoBuilder
  */
 protected function newEntityInfoBuilder(array $ids)
 {
     $idParser = new BasicEntityIdParser();
     $repo = new MockRepository();
     foreach ($this->getKnownEntities() as $entity) {
         $repo->putEntity($entity);
     }
     foreach ($this->getKnownRedirects() as $from => $toId) {
         $fromId = $idParser->parse($from);
         $repo->putRedirect(new EntityRedirect($fromId, $toId));
     }
     return new GenericEntityInfoBuilder($ids, $idParser, $repo);
 }
 protected function setUp()
 {
     parent::setUp();
     $this->mockRepository = new MockRepository();
     // empty item
     $item = new Item(new ItemId('Q11'));
     $this->mockRepository->putEntity($item);
     // non-empty item
     $item->setLabel('en', 'Foo');
     $item->setId(new ItemId('Q12'));
     $this->mockRepository->putEntity($item);
     // a property
     $prop = Property::newFromType('string');
     $prop->setId(new PropertyId('P11'));
     $this->mockRepository->putEntity($prop);
     // another property
     $prop->setId(new PropertyId('P12'));
     $this->mockRepository->putEntity($prop);
     // redirect
     $redirect = new EntityRedirect(new ItemId('Q22'), new ItemId('Q12'));
     $this->mockRepository->putRedirect($redirect);
 }
 /**
  * Adds a list of redirects to the test data.
  *
  * @param array $redirects A list of EntityRedirect objects, EntityId objects or strings.
  *        If a value in the list is not an EntityRedirect, a redirect is constructed from
  *        the corresponding array key and value; the key is parsed as an EntityId, the value
  *        is also parsed if it's a string.
  */
 public function putRedirects(array $redirects)
 {
     foreach ($redirects as $key => $redirect) {
         if (!$redirect instanceof EntityRedirect) {
             $from = $this->idParser->parse($key);
             if ($redirect instanceof EntityId) {
                 $target = $redirect;
             } else {
                 $target = $this->idParser->parse($redirect);
             }
             $redirect = new EntityRedirect($from, $target);
         }
         $this->mockRepository->putRedirect($redirect);
     }
 }
Пример #6
0
 /**
  * Construct mock repository matching the test data.
  *
  * @return MockRepository
  */
 public function getMockRepository()
 {
     static $repo;
     if (!empty($repo)) {
         return $repo;
     }
     $repo = new MockRepository();
     foreach (self::getTestProperties() as $prop) {
         list($id, $type) = $prop;
         $fingerprint = new Fingerprint();
         $fingerprint->setLabel('en', "Property{$id}");
         $entity = new Property(PropertyId::newFromNumber($id), $fingerprint, $type);
         $repo->putEntity($entity);
     }
     $q42 = new ItemId('Q42');
     $fingerprint = new Fingerprint();
     $fingerprint->setLabel('en', 'Item42');
     $entity = new Item($q42, $fingerprint);
     $repo->putEntity($entity);
     $repo->putRedirect(new EntityRedirect(new ItemId('Q4242'), $q42));
     return $repo;
 }
 public function testGetRedirectForEntityId()
 {
     $mock = new MockRepository();
     $q5 = new ItemId('Q5');
     $q55 = new ItemId('Q55');
     $q77 = new ItemId('Q77');
     $mock->putEntity(new Item($q5));
     $mock->putRedirect(new EntityRedirect($q55, $q5));
     $this->assertNull($mock->getRedirectForEntityId($q5), 'not a redirect');
     $this->assertEquals($q5, $mock->getRedirectForEntityId($q55));
     $this->setExpectedException('Wikibase\\DataModel\\Services\\Lookup\\EntityRedirectLookupException');
     $mock->getRedirectForEntityId($q77);
 }
 public function testRedirectUpdated()
 {
     $mock = new MockRepository();
     $id = new ItemId('Q123');
     $item = new Item($id);
     $mock->putEntity($item, 11);
     $lookup = new CachingEntityRevisionLookup($mock, new HashBagOStuff());
     $lookup->setVerifyRevision(false);
     // fetch first revision, so it gets cached
     $lookup->getEntityRevision($id);
     // replace by a redirect
     $targetId = new ItemId('Q222');
     $redir = new EntityRedirect($id, $targetId);
     $mock->putRedirect($redir);
     // now, notify the cache
     $lookup->redirectUpdated($redir, 17);
     // make sure we get the new revision now
     try {
         $lookup->getEntityRevision($id);
         $this->fail('UnresolvedRedirectException expected; perhaps the cache did not get purged properly.');
     } catch (RevisionedUnresolvedRedirectException $ex) {
         $this->assertEquals($targetId, $ex->getRedirectTargetId());
     }
 }