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

Corrected the exception class used and removed unnecessary strtolower in MethodScanner::setVisibility #140

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Scanner/MethodScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ public function setName($name)
*
* @param int $visibility T_PUBLIC | T_PRIVATE | T_PROTECTED
* @return self
* @throws \Zend\Code\Exception
* @throws \Zend\Code\Exception\InvalidArgumentException
*/
public function setVisibility($visibility)
{
switch (strtolower($visibility)) {
switch ($visibility) {
case T_PUBLIC:
$this->isPublic = true;
$this->isPrivate = false;
Expand All @@ -303,7 +303,7 @@ public function setVisibility($visibility)
break;

default:
throw new Exception('Invalid visibility argument passed to setVisibility.');
throw new Exception\InvalidArgumentException('Invalid visibility argument passed to setVisibility.');
}

return $this;
Expand Down
21 changes: 21 additions & 0 deletions test/Scanner/MethodScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Framework\TestCase;
use Zend\Code\Scanner\FileScanner;
use Zend\Code\Scanner\ParameterScanner;
use Zend\Code\Scanner\MethodScanner;
use ZendTest\Code\TestAsset\AbstractClass;
use ZendTest\Code\TestAsset\BarClass;
use ZendTest\Code\TestAsset\FooClass;
Expand Down Expand Up @@ -110,4 +111,24 @@ public function testMethodScannerWorksWithSingleAbstractFunction()

self::assertTrue($method->isAbstract());
}

public function testMethodScannerSetVisibilityThrowsInvalidArgumentException()
{
$methodScanner = new MethodScanner([]);

// make sure test argument is invalid
$invalidArgument = max(T_PUBLIC, T_PROTECTED, T_PRIVATE) + 1;

$this->expectException('\Zend\Code\Exception\InvalidArgumentException');
$methodScanner->setVisibility($invalidArgument);
}

public function testMethodScannerSetVisibilityAcceptsIntegerTokens()
{
$methodScanner = new MethodScanner([]);

$this->assertSame($methodScanner->setVisibility(T_PUBLIC), $methodScanner);
$this->assertSame($methodScanner->setVisibility(T_PROTECTED), $methodScanner);
$this->assertSame($methodScanner->setVisibility(T_PRIVATE), $methodScanner);
}
}