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

Commit 7abc52a

Browse files
committed
Merge pull request #139 from wandersonwhcr/hotfix/87
Deprecate Attribs and Proxy to Attributes
2 parents bb9075d + 7f4d56e commit 7abc52a

File tree

5 files changed

+115
-24
lines changed

5 files changed

+115
-24
lines changed

composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
}
7171
},
7272
"autoload-dev": {
73+
"files": [
74+
"test/autoload.php"
75+
],
7376
"psr-4": {
7477
"ZendTest\\View\\": "test/"
7578
}

phpunit.xml.dist

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
<php>
2525
<ini name="date.timezone" value="UTC"/>
26+
<ini name="error_reporting" value="E_ALL"/>
27+
<ini name="max_execution_time" value="360"/>
2628

2729
<!-- OB_ENABLED should be enabled for some tests to check if all
2830
functionality works as expected. Such tests include those for

src/Helper/Gravatar.php

+59-16
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Gravatar extends AbstractHtmlElement
4747
*
4848
* @var array
4949
*/
50-
protected $attribs;
50+
protected $attributes;
5151

5252
/**
5353
* Email Address
@@ -86,21 +86,21 @@ class Gravatar extends AbstractHtmlElement
8686
*
8787
* @see http://pl.gravatar.com/site/implement/url
8888
* @see http://pl.gravatar.com/site/implement/url More information about gravatar's service.
89-
* @param string|null $email Email address.
90-
* @param null|array $options Options
91-
* @param array $attribs Attributes for image tag (title, alt etc.)
89+
* @param string|null $email Email address.
90+
* @param null|array $options Options
91+
* @param array $attributes Attributes for image tag (title, alt etc.)
9292
* @return Gravatar
9393
*/
94-
public function __invoke($email = "", $options = [], $attribs = [])
94+
public function __invoke($email = "", $options = [], $attributes = [])
9595
{
9696
if (! empty($email)) {
9797
$this->setEmail($email);
9898
}
9999
if (! empty($options)) {
100100
$this->setOptions($options);
101101
}
102-
if (! empty($attribs)) {
103-
$this->setAttribs($attribs);
102+
if (! empty($attributes)) {
103+
$this->setAttributes($attributes);
104104
}
105105

106106
return $this;
@@ -168,28 +168,63 @@ public function getImgTag()
168168
{
169169
$this->setSrcAttribForImg();
170170
$html = '<img'
171-
. $this->htmlAttribs($this->getAttribs())
171+
. $this->htmlAttribs($this->getAttributes())
172172
. $this->getClosingBracket();
173173

174174
return $html;
175175
}
176176

177177
/**
178-
* Set attribs for image tag
178+
* Set attributes for image tag
179179
*
180-
* Warning! You shouldn't set src attrib for image tag.
181-
* This attrib is overwritten in protected method setSrcAttribForImg().
180+
* Warning! You shouldn't set src attribute for image tag.
181+
* This attribute is overwritten in protected method setSrcAttribForImg().
182182
* This method(_setSrcAttribForImg) is called in public method getImgTag().
183183
*
184+
* @param array $attributes
185+
* @return Gravatar
186+
*/
187+
public function setAttributes(array $attributes)
188+
{
189+
$this->attributes = $attributes;
190+
return $this;
191+
}
192+
193+
/**
194+
* Set attribs for image tag
195+
*
184196
* @param array $attribs
185197
* @return Gravatar
198+
*
199+
* @deprecated Please use Zend\View\Helper\Gravatar::setAttributes
186200
*/
187201
public function setAttribs(array $attribs)
188202
{
189-
$this->attribs = $attribs;
203+
trigger_error(sprintf(
204+
'%s is deprecated; please use %s::setAttributes',
205+
__METHOD__,
206+
__CLASS__
207+
), E_USER_DEPRECATED);
208+
209+
$this->setAttributes($attribs);
190210
return $this;
191211
}
192212

213+
/**
214+
* Get attributes of image
215+
*
216+
* Warning!
217+
* If you set src attribute, you get it, but this value will be overwritten in
218+
* protected method setSrcAttribForImg(). And finally your get other src
219+
* value!
220+
*
221+
* @return array
222+
*/
223+
public function getAttributes()
224+
{
225+
return $this->attributes;
226+
}
227+
193228
/**
194229
* Get attribs of image
195230
*
@@ -199,10 +234,18 @@ public function setAttribs(array $attribs)
199234
* value!
200235
*
201236
* @return array
237+
*
238+
* @deprecated Please use Zend\View\Helper\Gravatar::getAttributes
202239
*/
203240
public function getAttribs()
204241
{
205-
return $this->attribs;
242+
trigger_error(sprintf(
243+
'%s is deprecated; please use %s::getAttributes',
244+
__METHOD__,
245+
__CLASS__
246+
), E_USER_DEPRECATED);
247+
248+
return $this->getAttributes();
206249
}
207250

208251
/**
@@ -352,8 +395,8 @@ public function getSecure()
352395
*/
353396
protected function setSrcAttribForImg()
354397
{
355-
$attribs = $this->getAttribs();
356-
$attribs['src'] = $this->getAvatarUrl();
357-
$this->setAttribs($attribs);
398+
$attributes = $this->getAttributes();
399+
$attributes['src'] = $this->getAvatarUrl();
400+
$this->setAttributes($attributes);
358401
}
359402
}

test/Helper/GravatarTest.php

+40-8
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99

1010
namespace ZendTest\View\Helper;
1111

12+
use PHPUnit\Framework\Error\Deprecated as DeprecatedError;
1213
use PHPUnit\Framework\TestCase;
14+
use ReflectionMethod;
1315
use Zend\View\Exception;
14-
use Zend\View\Renderer\PhpRenderer as View;
1516
use Zend\View\Helper\Gravatar;
17+
use Zend\View\Renderer\PhpRenderer as View;
1618

1719
/**
1820
* @group Zendview
@@ -85,17 +87,17 @@ public function testGravatarHtmlDoctype()
8587
*/
8688
public function testGetAndSetMethods()
8789
{
88-
$attribs = ['class' => 'gravatar', 'title' => 'avatar', 'id' => 'gravatar-1'];
90+
$attributes = ['class' => 'gravatar', 'title' => 'avatar', 'id' => 'gravatar-1'];
8991
$this->helper->setDefaultImg('monsterid')
9092
->setImgSize(150)
9193
->setSecure(true)
9294
->setEmail("example@example.com")
93-
->setAttribs($attribs)
95+
->setAttributes($attributes)
9496
->setRating('pg');
9597
$this->assertEquals("monsterid", $this->helper->getDefaultImg());
9698
$this->assertEquals("pg", $this->helper->getRating());
9799
$this->assertEquals("example@example.com", $this->helper->getEmail());
98-
$this->assertEquals($attribs, $this->helper->getAttribs());
100+
$this->assertEquals($attributes, $this->helper->getAttributes());
99101
$this->assertEquals(150, $this->helper->getImgSize());
100102
$this->assertTrue($this->helper->getSecure());
101103
}
@@ -164,9 +166,9 @@ public function testHttpsSource()
164166
}
165167

166168
/**
167-
* Test HTML attribs
169+
* Test HTML attributes
168170
*/
169-
public function testImgAttribs()
171+
public function testImgAttributes()
170172
{
171173
$this->assertRegExp(
172174
'/class="gravatar" title="Gravatar"/',
@@ -233,11 +235,11 @@ public function testAutoDetectLocationOnIis()
233235
);
234236
}
235237

236-
public function testSetAttribsWithSrcKey()
238+
public function testSetAttributesWithSrcKey()
237239
{
238240
$email = 'example@example.com';
239241
$this->helper->setEmail($email);
240-
$this->helper->setAttribs([
242+
$this->helper->setAttributes([
241243
'class' => 'gravatar',
242244
'src' => 'http://example.com',
243245
'id' => 'gravatarID',
@@ -285,4 +287,34 @@ public function testEmailIsProperlyNormalized()
285287
$this->helper->__invoke('Example@Example.com ')->getEmail()
286288
);
287289
}
290+
291+
public function testSetAttribsIsDeprecated()
292+
{
293+
$this->expectException(DeprecatedError::class);
294+
295+
$this->helper->setAttribs([]);
296+
}
297+
298+
public function testSetAttribsDocCommentHasDeprecated()
299+
{
300+
$method = new ReflectionMethod($this->helper, 'setAttribs');
301+
$comment = $method->getDocComment();
302+
303+
$this->assertContains('@deprecated', $comment);
304+
}
305+
306+
public function testGetAttribsIsDeprecated()
307+
{
308+
$this->expectException(DeprecatedError::class);
309+
310+
$this->helper->getAttribs();
311+
}
312+
313+
public function testGetAttribsDocCommentHasDeprecated()
314+
{
315+
$method = new ReflectionMethod($this->helper, 'getAttribs');
316+
$comment = $method->getDocComment();
317+
318+
$this->assertContains('@deprecated', $comment);
319+
}
288320
}

test/autoload.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-view for the canonical source repository
4+
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license https://github.com/zendframework/zend-view/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
if (class_exists(PHPUnit_Framework_Error_Deprecated::class)
9+
&& ! class_exists(PHPUnit\Framework\Error\Deprecated::class)) {
10+
class_alias(PHPUnit_Framework_Error_Deprecated::class, PHPUnit\Framework\Error\Deprecated::class);
11+
}

0 commit comments

Comments
 (0)