Skip to content

Commit

Permalink
added url getter methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
nonz250 committed Jan 25, 2022
1 parent c04171c commit 16336bb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 13 deletions.
18 changes: 15 additions & 3 deletions src/SimpleOgp.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use DOMDocument;
use DOMXPath;
use InvalidArgumentException;
use RuntimeException;

class SimpleOgp implements SimpleOgpInterface
Expand Down Expand Up @@ -39,6 +40,9 @@ class SimpleOgp implements SimpleOgpInterface
*/
public function __construct(string $url)
{
if ($url === '') {
throw new InvalidArgumentException('Url is required.');
}
$this->url = $url;
}

Expand All @@ -56,11 +60,11 @@ public function getHtml(): self
@$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$xpath = new DOMXPath($dom);
$image = $xpath->query('//meta[@property="og:image"]/@content');
$this->imagePath = (string)$image[0]->value ?? '';
$this->imagePath = $image[0] ? (string)$image[0]->value : '';
$title = $xpath->query('//meta[@property="og:title"]/@content');
$this->title = (string)$title[0]->value ?? '';
$this->title = $title[0] ? (string)$title[0]->value : '';
$description = $xpath->query('//meta[@property="og:description"]/@content');
$this->description = (string)$description[0]->value ?? $title . 'を見る';
$this->description = $description[0] ? (string)$description[0]->value : '';
if (mb_strlen($this->description) > 100) {
$this->description = mb_substr($this->description, 0, 100) . '...';
}
Expand Down Expand Up @@ -98,4 +102,12 @@ public function imagePath(): string
{
return $this->imagePath;
}

/**
* @return string
*/
public function url(): string
{
return $this->url;
}
}
5 changes: 5 additions & 0 deletions src/SimpleOgpInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ public function description(): string;
* @return string
*/
public function imagePath(): string;

/**
* @return string
*/
public function url(): string;
}
58 changes: 48 additions & 10 deletions tests/SimpleOgpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Tests;

use InvalidArgumentException;
use ReflectionClass;
use ReflectionException;
use SimpleOgp\SimpleOgp;
Expand All @@ -11,13 +12,17 @@

class SimpleOgpTest extends TestCase
{
/**
* @var string
*/
private string $nonLabo = 'https://labo.nozomi.bike';

/**
* @return SimpleOgpInterface
*/
public function test__construct(): SimpleOgpInterface
{
$nonLabo = 'https://labo.nozomi.bike';
$simpleOgp = new SimpleOgp($nonLabo);
$simpleOgp = new SimpleOgp($this->nonLabo);
$this->assertInstanceOf(SimpleOgpInterface::class, $simpleOgp);
return $simpleOgp;
}
Expand All @@ -43,35 +48,68 @@ public function testGetHtml(SimpleOgpInterface $simpleOgp): SimpleOgpInterface
* @depends testGetHtml
* @param SimpleOgpInterface $simpleOgp
*/
public function testHtml(SimpleOgpInterface $simpleOgp)
public function testHtml(SimpleOgpInterface $simpleOgp): void
{
$this->assertNotSame('', $simpleOgp->html());
}

/**
* @depends testGetHtml
* @param SimpleOgpInterface $simpleOgp
*/
public function testTitle(SimpleOgpInterface $simpleOgp): void
{
$this->assertTrue(mb_strlen($simpleOgp->html()) > 0);
$this->assertNotSame('', $simpleOgp->title());
}

/**
* @depends testGetHtml
* @param SimpleOgpInterface $simpleOgp
*/
public function testTitle(SimpleOgpInterface $simpleOgp)
public function testDescription(SimpleOgpInterface $simpleOgp): void
{
$this->assertTrue(mb_strlen($simpleOgp->title()) > 0);
$this->assertNotSame('', $simpleOgp->description());
}

/**
* @depends testGetHtml
* @param SimpleOgpInterface $simpleOgp
*/
public function testDescription(SimpleOgpInterface $simpleOgp)
public function testImagePath(SimpleOgpInterface $simpleOgp): void
{
$this->assertTrue(mb_strlen($simpleOgp->description()) > 0);
$this->assertNotSame('', $simpleOgp->imagePath());
}

/**
* @depends testGetHtml
* @param SimpleOgpInterface $simpleOgp
*/
public function testImagePath(SimpleOgpInterface $simpleOgp)
public function testUrl(SimpleOgpInterface $simpleOgp): void
{
$this->assertSame($this->nonLabo, $simpleOgp->url());
}

/**
* @return void
*/
public function testEmptyUrl(): void
{
$this->expectException(InvalidArgumentException::class);
new SimpleOgp('');
}

/**
* @return void
*/
public function testNoContent(): void
{
$this->assertTrue(mb_strlen($simpleOgp->imagePath()) > 0);
$exampleUrl = 'https://example.com';
$example = new SimpleOgp($exampleUrl);
$example->getHtml();
$this->assertNotSame('', $example->html());
$this->assertSame($exampleUrl, $example->url());
$this->assertSame('', $example->description());
$this->assertSame('', $example->title());
$this->assertSame('', $example->imagePath());
}
}

0 comments on commit 16336bb

Please sign in to comment.