Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/139' into develop
Browse files Browse the repository at this point in the history
Close #139
  • Loading branch information
weierophinney committed Jan 17, 2018
2 parents bb9075d + 2ecf680 commit 31e1d0b
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 25 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ All notable changes to this project will be documented in this file, in reverse
the HTML5 "as" attribute to the `HeadLink` helper. This can be used to help
prioritize resource loading.

- [#139](https://github.com/zendframework/zend-view/pull/139) adds two new
methods to the `Zend\View\Helper\Gravatar` class: `setAttributes()` and
`getAttributes()`.

### Changed

- [#133](https://github.com/zendframework/zend-view/pull/133) modifies the
Expand All @@ -21,7 +25,9 @@ All notable changes to this project will be documented in this file, in reverse

### Deprecated

- Nothing.
- [#139](https://github.com/zendframework/zend-view/pull/139) deprecates the
`Zend\View\Helper\Gravatar` methods `setAttribs()` and `getAttribs()` in favor
of the new methods `setAttributes()` and `getAttributes()`, respectively.

### Removed

Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
}
},
"autoload-dev": {
"files": [
"test/autoload.php"
],
"psr-4": {
"ZendTest\\View\\": "test/"
}
Expand Down
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

<php>
<ini name="date.timezone" value="UTC"/>
<ini name="error_reporting" value="E_ALL"/>
<ini name="max_execution_time" value="360"/>

<!-- OB_ENABLED should be enabled for some tests to check if all
functionality works as expected. Such tests include those for
Expand Down
75 changes: 59 additions & 16 deletions src/Helper/Gravatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Gravatar extends AbstractHtmlElement
*
* @var array
*/
protected $attribs;
protected $attributes;

/**
* Email Address
Expand Down Expand Up @@ -86,21 +86,21 @@ class Gravatar extends AbstractHtmlElement
*
* @see http://pl.gravatar.com/site/implement/url
* @see http://pl.gravatar.com/site/implement/url More information about gravatar's service.
* @param string|null $email Email address.
* @param null|array $options Options
* @param array $attribs Attributes for image tag (title, alt etc.)
* @param string|null $email Email address.
* @param null|array $options Options
* @param array $attributes Attributes for image tag (title, alt etc.)
* @return Gravatar
*/
public function __invoke($email = "", $options = [], $attribs = [])
public function __invoke($email = "", $options = [], $attributes = [])
{
if (! empty($email)) {
$this->setEmail($email);
}
if (! empty($options)) {
$this->setOptions($options);
}
if (! empty($attribs)) {
$this->setAttribs($attribs);
if (! empty($attributes)) {
$this->setAttributes($attributes);
}

return $this;
Expand Down Expand Up @@ -168,28 +168,63 @@ public function getImgTag()
{
$this->setSrcAttribForImg();
$html = '<img'
. $this->htmlAttribs($this->getAttribs())
. $this->htmlAttribs($this->getAttributes())
. $this->getClosingBracket();

return $html;
}

/**
* Set attribs for image tag
* Set attributes for image tag
*
* Warning! You shouldn't set src attrib for image tag.
* This attrib is overwritten in protected method setSrcAttribForImg().
* Warning! You shouldn't set src attribute for image tag.
* This attribute is overwritten in protected method setSrcAttribForImg().
* This method(_setSrcAttribForImg) is called in public method getImgTag().
*
* @param array $attributes
* @return Gravatar
*/
public function setAttributes(array $attributes)
{
$this->attributes = $attributes;
return $this;
}

/**
* Set attribs for image tag
*
* @param array $attribs
* @return Gravatar
*
* @deprecated Please use Zend\View\Helper\Gravatar::setAttributes
*/
public function setAttribs(array $attribs)
{
$this->attribs = $attribs;
trigger_error(sprintf(
'%s is deprecated; please use %s::setAttributes',
__METHOD__,
__CLASS__
), E_USER_DEPRECATED);

$this->setAttributes($attribs);
return $this;
}

/**
* Get attributes of image
*
* Warning!
* If you set src attribute, you get it, but this value will be overwritten in
* protected method setSrcAttribForImg(). And finally your get other src
* value!
*
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}

/**
* Get attribs of image
*
Expand All @@ -199,10 +234,18 @@ public function setAttribs(array $attribs)
* value!
*
* @return array
*
* @deprecated Please use Zend\View\Helper\Gravatar::getAttributes
*/
public function getAttribs()
{
return $this->attribs;
trigger_error(sprintf(
'%s is deprecated; please use %s::getAttributes',
__METHOD__,
__CLASS__
), E_USER_DEPRECATED);

return $this->getAttributes();
}

/**
Expand Down Expand Up @@ -352,8 +395,8 @@ public function getSecure()
*/
protected function setSrcAttribForImg()
{
$attribs = $this->getAttribs();
$attribs['src'] = $this->getAvatarUrl();
$this->setAttribs($attribs);
$attributes = $this->getAttributes();
$attributes['src'] = $this->getAvatarUrl();
$this->setAttributes($attributes);
}
}
48 changes: 40 additions & 8 deletions test/Helper/GravatarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

namespace ZendTest\View\Helper;

use PHPUnit\Framework\Error\Deprecated as DeprecatedError;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use Zend\View\Exception;
use Zend\View\Renderer\PhpRenderer as View;
use Zend\View\Helper\Gravatar;
use Zend\View\Renderer\PhpRenderer as View;

/**
* @group Zendview
Expand Down Expand Up @@ -85,17 +87,17 @@ public function testGravatarHtmlDoctype()
*/
public function testGetAndSetMethods()
{
$attribs = ['class' => 'gravatar', 'title' => 'avatar', 'id' => 'gravatar-1'];
$attributes = ['class' => 'gravatar', 'title' => 'avatar', 'id' => 'gravatar-1'];
$this->helper->setDefaultImg('monsterid')
->setImgSize(150)
->setSecure(true)
->setEmail("example@example.com")
->setAttribs($attribs)
->setAttributes($attributes)
->setRating('pg');
$this->assertEquals("monsterid", $this->helper->getDefaultImg());
$this->assertEquals("pg", $this->helper->getRating());
$this->assertEquals("example@example.com", $this->helper->getEmail());
$this->assertEquals($attribs, $this->helper->getAttribs());
$this->assertEquals($attributes, $this->helper->getAttributes());
$this->assertEquals(150, $this->helper->getImgSize());
$this->assertTrue($this->helper->getSecure());
}
Expand Down Expand Up @@ -164,9 +166,9 @@ public function testHttpsSource()
}

/**
* Test HTML attribs
* Test HTML attributes
*/
public function testImgAttribs()
public function testImgAttributes()
{
$this->assertRegExp(
'/class="gravatar" title="Gravatar"/',
Expand Down Expand Up @@ -233,11 +235,11 @@ public function testAutoDetectLocationOnIis()
);
}

public function testSetAttribsWithSrcKey()
public function testSetAttributesWithSrcKey()
{
$email = 'example@example.com';
$this->helper->setEmail($email);
$this->helper->setAttribs([
$this->helper->setAttributes([
'class' => 'gravatar',
'src' => 'http://example.com',
'id' => 'gravatarID',
Expand Down Expand Up @@ -285,4 +287,34 @@ public function testEmailIsProperlyNormalized()
$this->helper->__invoke('Example@Example.com ')->getEmail()
);
}

public function testSetAttribsIsDeprecated()
{
$this->expectException(DeprecatedError::class);

$this->helper->setAttribs([]);
}

public function testSetAttribsDocCommentHasDeprecated()
{
$method = new ReflectionMethod($this->helper, 'setAttribs');
$comment = $method->getDocComment();

$this->assertContains('@deprecated', $comment);
}

public function testGetAttribsIsDeprecated()
{
$this->expectException(DeprecatedError::class);

$this->helper->getAttribs();
}

public function testGetAttribsDocCommentHasDeprecated()
{
$method = new ReflectionMethod($this->helper, 'getAttribs');
$comment = $method->getDocComment();

$this->assertContains('@deprecated', $comment);
}
}
11 changes: 11 additions & 0 deletions test/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* @see https://github.com/zendframework/zend-view for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-view/blob/master/LICENSE.md New BSD License
*/

if (class_exists(PHPUnit_Framework_Error_Deprecated::class)
&& ! class_exists(PHPUnit\Framework\Error\Deprecated::class)) {
class_alias(PHPUnit_Framework_Error_Deprecated::class, PHPUnit\Framework\Error\Deprecated::class);
}

0 comments on commit 31e1d0b

Please sign in to comment.