Example #1
0
 public function testGetters()
 {
     $rawResult = json_decode(self::$bodyJson);
     $pageUrl = '/my/url';
     $resultPage = new Page($rawResult->Page);
     $resultPage->setPageUrl($pageUrl);
     $this->assertInstanceOf(PageInterface::class, $resultPage);
     // Page url
     $this->assertEquals($pageUrl, $resultPage->getPageUrl());
     // Head content
     $this->assertEquals("A meta description", $resultPage->getHeadContent()->MetaDescription);
     $this->assertEquals("Some meta keywords", $resultPage->getHeadContent()->MetaKeywords);
     $this->assertEquals("A title", $resultPage->getHeadContent()->Title);
     // Head instructions
     $this->assertEquals("https://www.example.com/canonical", $resultPage->getHeadInstructions()->CanonicalUrl);
     $this->assertEquals("INDEX, FOLLOW", $resultPage->getHeadInstructions()->MetaRobots);
     // Links
     $links = $resultPage->getLinks();
     $this->assertEquals('Main Links', $links[0]->Key);
     $this->assertEquals('Main link 1', $links[0]->Value[0]->AnchorText);
     $this->assertEquals('Main link 2', $links[0]->Value[1]->AnchorText);
     // Link section
     $linkSection = $resultPage->getLinkSection('main links');
     $this->assertEquals('Main link 1', $linkSection[0]->AnchorText);
     $this->assertEquals('Main link 2', $linkSection[1]->AnchorText);
     // Page content
     $this->assertEquals("Page content abstract", $resultPage->getPageContent()->Abstract);
     // Server side
     $this->assertEquals("Existing", $resultPage->getServerSide()->PageOrigin);
 }
 /**
  * {@inheritdoc}
  */
 public function build(RequestInterface $request, HttpResponseInterface $response)
 {
     $url = $request->getParams()['url'];
     $json = $response->getBody();
     $data = json_decode($json);
     if (!isset($data->Page)) {
         throw new ResultBuilderException("Required element was not present in response (Page)");
     }
     $rawPage = $data->Page;
     // "Links" can be blank, so we don't check for it
     $requiredProperties = ["HeadContent", "HeadInstructions", "PageContent", "ServerSide"];
     foreach ($requiredProperties as $expectedProperty) {
         if (!isset($rawPage->{$expectedProperty})) {
             throw new ResultBuilderException("Required element was not present in response (Page.{$expectedProperty})");
         }
     }
     $page = new Page($rawPage);
     $page->setPageUrl($url);
     return new PageResult($response, $page);
 }
 /**
  * @param bool $mustBeNull
  * @return Page
  */
 private function getPageObject($mustBeNull = false)
 {
     $rawPageHead = new \stdClass();
     $rawPageHead->MetaDescription = $mustBeNull ? null : 'ThisIsTheMetaDescription';
     $rawPageHead->MetaKeywords = $mustBeNull ? null : 'ThisIsTheMetaKeywords';
     $rawPageHead->Title = $mustBeNull ? null : 'ThisIsTheTitle';
     $rawPage = new \stdClass();
     $rawPage->HeadContent = $rawPageHead;
     $page = new Page($rawPage);
     $page->setPageUrl('/testpage');
     return $page;
 }