forked from aimeos/ai-client-html
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved generating image tags to view helper
- Loading branch information
Showing
12 changed files
with
316 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project name="ai-client-html extension - custom library extensions" default="test"> | ||
|
||
<target name="test" description="Executes unit tests"> | ||
<echo msg="Executing unit tests" /> | ||
<delete file="tests/unittest.log" quiet="true" failonerror="false" /> | ||
<exec command="${bindir}phpunit -d memory_limit=-1 --include-path ${coredir} --configuration=phpunit.xml ." | ||
dir="tests" checkreturn="true" logoutput="true" passthru="true" /> | ||
</target> | ||
|
||
<target name="coverage" description="Generates code coverage report"> | ||
<echo msg="Generating code coverage report" /> | ||
<delete file="tests/unittests.log" quiet="true" failonerror="false" /> | ||
<delete dir="tests/coverage" quiet="true" failonerror="false" /> | ||
<exec command="${bindir}phpunit -d memory_limit=-1 --include-path ${coredir} --configuration=phpunit-coverage.xml ." | ||
dir="tests" checkreturn="true" logoutput="true" passthru="true" /> | ||
</target> | ||
|
||
<target name="check" description="Executes code sniffer"> | ||
<echo msg="Executing code sniffer" /> | ||
<exec command="${bindir}phpcs -n --standard=${codestddir}/Aimeos src tests/MW" | ||
checkreturn="true" logoutput="true" passthru="true" /> | ||
</target> | ||
|
||
<target name="clean" description="Cleans up temporary files"> | ||
<delete dir="tests/coverage" quiet="true" failonerror="false" /> | ||
<delete quiet="true" failonerror="false" > | ||
<fileset dir="tests"> | ||
<include name="*.log" /> | ||
</fileset> | ||
</delete> | ||
</target> | ||
|
||
<target name="all" depends="coverage,check" description="Builds all parts of the package"> | ||
</target> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/** | ||
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 | ||
* @copyright Aimeos (aimeos.org), 2019-2020 | ||
* @package MW | ||
* @subpackage View | ||
*/ | ||
|
||
|
||
namespace Aimeos\MW\View\Helper\Image; | ||
|
||
|
||
/** | ||
* View helper class for creating an HTML image tag | ||
* | ||
* @package MW | ||
* @subpackage View | ||
*/ | ||
interface Iface extends \Aimeos\MW\View\Helper\Iface | ||
{ | ||
/** | ||
* Returns the HTML image tag for the given media item | ||
* | ||
* @param \Aimeos\MShop\Media\Item\Iface $media Media item | ||
* @return string HTML image tag | ||
*/ | ||
public function transform( \Aimeos\MShop\Media\Item\Iface $media ) : string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/** | ||
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 | ||
* @copyright Aimeos (aimeos.org), 2019-2020 | ||
* @package MW | ||
* @subpackage View | ||
*/ | ||
|
||
|
||
namespace Aimeos\MW\View\Helper\Image; | ||
|
||
|
||
/** | ||
* View helper class for creating an HTML image tag | ||
* | ||
* @package MW | ||
* @subpackage View | ||
*/ | ||
class Standard | ||
extends \Aimeos\MW\View\Helper\Base | ||
implements \Aimeos\MW\View\Helper\Image\Iface | ||
{ | ||
/** | ||
* Returns the HTML image tag for the given media item | ||
* | ||
* @param \Aimeos\MShop\Media\Item\Iface $media Media item | ||
* @return string HTML image tag | ||
*/ | ||
public function transform( \Aimeos\MShop\Media\Item\Iface $media ) : string | ||
{ | ||
$view = $this->getView(); | ||
$enc = $view->encoder(); | ||
|
||
$sources = []; | ||
foreach( $media->getPreviews() as $type => $path ) { | ||
$sources[$type] = $this->content( $path ); | ||
} | ||
|
||
$variant = ''; | ||
foreach( $media->getRefItems( 'attribute', null, 'variant' ) as $id => $item ) { | ||
$variant .= ' data-variant-' . $item->getType() . '="' . $enc->attr( $id ) . '"'; | ||
} | ||
|
||
return '<img class="item" | ||
itemprop="image" itemscope="" itemtype="http://schema.org/ImageObject" | ||
src="' . $enc->attr( $view->content( $media->getPreview() ) ) . '" | ||
srcset="' . $enc->attr( $view->imageset( $media->getPreviews() ) ) . '" | ||
data-image="' . $enc->attr( $view->content( $media->getPreview() ) ) . '" | ||
data-sources="' . $enc->attr( json_encode( $sources, JSON_FORCE_OBJECT ) ) . '" | ||
alt="' . $enc->html( $media->getName() ) . '"' . $variant . ' | ||
/>'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/** | ||
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 | ||
* @copyright Aimeos (aimeos.org), 2020 | ||
*/ | ||
|
||
|
||
namespace Aimeos\MW\View\Helper\Image; | ||
|
||
|
||
class StandardTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
private $object; | ||
|
||
|
||
protected function setUp() : void | ||
{ | ||
$view = new \Aimeos\MW\View\Standard(); | ||
$view->addHelper( 'content', new \Aimeos\MW\View\Helper\Content\Standard( $view, '/path/to' ) ); | ||
$view->addHelper( 'encoder', new \Aimeos\MW\View\Helper\Encoder\Standard( $view ) ); | ||
|
||
$this->object = new \Aimeos\MW\View\Helper\Image\Standard( $view ); | ||
} | ||
|
||
|
||
protected function tearDown() : void | ||
{ | ||
unset( $this->object ); | ||
} | ||
|
||
|
||
public function testTransform() | ||
{ | ||
$context = \TestHelper::getContext(); | ||
$manager = \Aimeos\MShop::create( $context, 'media' ); | ||
|
||
$attrItem = \Aimeos\MShop::create( $context, 'attribute' )->createItem()->setType( 'color' )->setId( 123 ); | ||
$listItem = $manager->createListsItem()->setType( 'variant' ); | ||
|
||
$mediaItem = $manager->createItem()->setLabel( 'testimage' ) | ||
->setPreviews( ['100' => 'image-1.jpg', '200' => 'image-2.jpg'] ) | ||
->addListItem( 'attribute', $listItem, $attrItem ); | ||
|
||
$result = $this->object->transform( $mediaItem ); | ||
|
||
$this->assertStringContainsString( '/path/to/image-1.jpg 100w, /path/to/image-2.jpg 200w', $result ); | ||
$this->assertStringContainsString( 'src="/path/to/image-1.jpg"', $result ); | ||
$this->assertStringContainsString( 'data-variant-color="123"', $result ); | ||
} | ||
} |
Oops, something went wrong.