Skip to content

Commit 1fe41f4

Browse files
GeorgosXLNyholm
GeorgosXL
authored andcommitted
Fix parsers (#3)
* test space * test space * removed space * removed space * Added setSourceUrl to Media Model, Added first version MediaParser * Added first version test case for RewriteMediaUrl, installed phpunit * changes to mediaurl test * changes to mediaurl test * unsure if we want this test * unsure if we want this test * Added check if file exists, use the existing one * composer.lock revert * rewrote test for RewriteMediaUrl * pr changes * pr changes
1 parent 4de8c94 commit 1fe41f4

File tree

6 files changed

+98
-6
lines changed

6 files changed

+98
-6
lines changed

Model/Media.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Media
2626
*/
2727
private $sourceUrl;
2828

29-
public function __construct(array $data)
29+
public function __construct(array $data = [])
3030
{
3131
if (empty($data)) {
3232
return;
@@ -57,4 +57,9 @@ public function getSourceUrl(): string
5757
{
5858
return $this->sourceUrl;
5959
}
60+
61+
public function setSourceUrl(string $sourceUrl)
62+
{
63+
$this->sourceUrl = $sourceUrl;
64+
}
6065
}

Parser/RewriteMediaUrl.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Happyr\WordpressBundle\Parser;
6+
7+
use Happyr\WordpressBundle\Model\Media;
8+
use Happyr\WordpressBundle\Service\ImageUploaderInterface;
9+
10+
class RewriteMediaUrl implements MediaParserInterface
11+
{
12+
private $remoteUrl;
13+
private $imageUploader;
14+
15+
public function __construct(string $remoteUrl, ImageUploaderInterface $imageUploader)
16+
{
17+
$this->remoteUrl = $remoteUrl;
18+
$this->imageUploader = $imageUploader;
19+
}
20+
21+
public function parseMedia(Media $media): void
22+
{
23+
$media->setSourceUrl($this->rewriteUrl($media->getSourceUrl()));
24+
}
25+
26+
private function rewriteUrl(string $content): string
27+
{
28+
$remoteUrl = parse_url($this->remoteUrl);
29+
$contentUrl = parse_url($content);
30+
31+
if (!$contentUrl) {
32+
return $content;
33+
}
34+
35+
if (!empty($contentUrl['host']) && $remoteUrl['host'] === $contentUrl['host']) {
36+
37+
// rewrite the URL.
38+
$content = $this->imageUploader->uploadImage($content);
39+
}
40+
41+
return $content;
42+
}
43+
}

Service/LocalImageUploader.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ public function uploadImage(string $url): string
3737

3838
$filename = \basename($url);
3939

40-
// Save the file
41-
\file_put_contents(\sprintf('%s/%s', $this->uploadsFolder, $filename), $file);
40+
$path = \sprintf('%s/%s', $this->uploadsFolder, $filename);
41+
42+
// Check if file already exists
43+
if (!\file_exists($path)) {
44+
// Save the file
45+
\file_put_contents($path, $file);
46+
}
4247

4348
return \sprintf('%s/%s', $this->webPrefix, $filename);
4449
}

Tests/Parser/RewriteMediaUrlTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Happyr\WordpressBundle\Tests\Parser;
6+
7+
use Happyr\WordpressBundle\Model\Media;
8+
use Happyr\WordpressBundle\Parser\RewriteMediaUrl;
9+
use Happyr\WordpressBundle\Service\ImageUploaderInterface;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class RewriteMediaUrlTest extends TestCase
13+
{
14+
/**
15+
* @dataProvider urlProvider
16+
*/
17+
public function testRewrite($inputUrl, $outputUrl)
18+
{
19+
$media = new Media();
20+
$media->setSourceUrl($inputUrl);
21+
$imageUploader = $this->getMockBuilder(ImageUploaderInterface::class)
22+
->setMethods(['uploadImage'])
23+
->getMock();
24+
25+
$imageUploader->method('uploadImage')->willReturn($outputUrl);
26+
27+
$parser = new RewriteMediaUrl($inputUrl, $imageUploader);
28+
$parser->parseMedia($media);
29+
30+
$this->assertEquals($outputUrl, $media->getSourceUrl());
31+
}
32+
33+
public function urlProvider()
34+
{
35+
$inputUrl = 'http://wordpress.com/wp-conent/uploads/2018/foobar.jpg';
36+
$malformedUrl = 'htp:/wordpress.com/wp-conent/uploads/2018/foobar.jpg';
37+
38+
yield [$inputUrl, 'https://www.happyr.com/images/foobar.jpg'];
39+
yield [$malformedUrl, $malformedUrl];
40+
}
41+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"twig/twig": "^2.0"
1515
},
1616
"require-dev": {
17+
"phpunit/phpunit": "^7.4",
1718
"symfony/phpunit-bridge": "^4.1"
1819
},
1920
"config": {

phpunit.xml.dist

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88
convertWarningsToExceptions="true"
99
processIsolation="false"
1010
stopOnFailure="false"
11-
syntaxCheck="false"
12-
codecoverage="true"
1311
bootstrap="./vendor/autoload.php"
1412
>
1513

1614
<php>
1715
<env name="ENV" value="test" />
1816
</php>
1917

20-
<formatter type="clover" usefile="false"/>
2118
<testsuites>
2219
<testsuite name="Test Suite">
2320
<directory>./Tests</directory>

0 commit comments

Comments
 (0)