-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Head command interface * Correct author * Revert vendor autoload for github test environment * Lint the file I touched * Refactor onHeadFollows * Refactor HeadCommand to return the string un-parsed
- Loading branch information
1 parent
378e2f9
commit 604c4bb
Showing
5 changed files
with
195 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the NNTP library. | ||
* | ||
* (c) Robin van der Vleuten <robin@webstronauts.co> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rvdv\Nntp\Command; | ||
|
||
use Rvdv\Nntp\Exception\RuntimeException; | ||
use Rvdv\Nntp\Response\MultiLineResponse; | ||
use Rvdv\Nntp\Response\Response; | ||
|
||
/** | ||
* HeadCommand. | ||
* | ||
* @author elabz | ||
*/ | ||
class HeadCommand extends Command implements CommandInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $article; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $article | ||
*/ | ||
public function __construct($article) | ||
{ | ||
$this->article = $article; | ||
|
||
parent::__construct(true); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __invoke() | ||
{ | ||
return sprintf('HEAD %s', $this->article); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function onHeadFollows(MultiLineResponse $response) | ||
{ | ||
return implode("\r\n", $response->getLines()); | ||
} | ||
|
||
public function onNoNewsGroupCurrentSelected(Response $response) | ||
{ | ||
throw new RuntimeException('A group must be selected first before getting an article header.', $response->getStatusCode()); | ||
} | ||
|
||
public function onNoSuchArticleNumber(Response $response) | ||
{ | ||
throw new RuntimeException('No article with that number.', $response->getStatusCode()); | ||
} | ||
|
||
public function onNoSuchArticleId(Response $response) | ||
{ | ||
throw new RuntimeException('No article with that message-id.', $response->getStatusCode()); | ||
} | ||
} |
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,113 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the NNTP library. | ||
* | ||
* (c) Robin van der Vleuten <robin@webstronauts.co> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rvdv\Nntp\Tests\Command; | ||
|
||
use Rvdv\Nntp\Command\HeadCommand; | ||
|
||
/** | ||
* HeadCommandTest. | ||
* | ||
* @author Robin van der Vleuten <robin@webstronauts.co> | ||
*/ | ||
class HeadCommandTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testItExpectsMultilineResponses() | ||
{ | ||
$command = $this->createCommandInstance(); | ||
$this->assertTrue($command->isMultiLine()); | ||
} | ||
|
||
public function testItNotExpectsCompressedResponses() | ||
{ | ||
$command = $this->createCommandInstance(); | ||
$this->assertFalse($command->isCompressed()); | ||
} | ||
|
||
public function testItReturnsStringWhenExecuting() | ||
{ | ||
$command = $this->createCommandInstance(); | ||
$this->assertEquals('HEAD 12345', $command()); | ||
} | ||
|
||
public function testItReceivesAResultWhenHeadFollowsResponse() | ||
{ | ||
$command = $this->createCommandInstance(); | ||
|
||
$response = $this->getMockBuilder('Rvdv\Nntp\Response\MultiLineResponse') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$lines = ['Message-ID: <1234567890@1234567890.com>', 'Date: Thu, 28 Sep 2017 12:56:35 +0000', 'Newsgroups: php.announce']; | ||
|
||
$response->expects($this->once()) | ||
->method('getLines') | ||
->will($this->returnValue($lines)); | ||
|
||
$this->assertEquals(implode("\r\n", $lines), $command->onHeadFollows($response)); | ||
} | ||
|
||
public function testItErrorsWhenGroupNotSelectedResponse() | ||
{ | ||
$command = $this->createCommandInstance(); | ||
|
||
$response = $this->getMockBuilder('Rvdv\Nntp\Response\Response') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
try { | ||
$command->onNoNewsGroupCurrentSelected($response); | ||
$this->fail('->onNoNewsGroupCurrentSelected() throws a Rvdv\Nntp\Exception\RuntimeException because the server indicated a newsgroup has not been selected'); | ||
} catch (\Exception $e) { | ||
$this->assertInstanceof('Rvdv\Nntp\Exception\RuntimeException', $e, '->onNoNewsGroupCurrentSelected() throws a Rvdv\Nntp\Exception\RuntimeException because the server indicated a newsgroup has not been selected'); | ||
} | ||
} | ||
|
||
public function testItErrorsWhenNoSuchArticleNumberResponse() | ||
{ | ||
$command = $this->createCommandInstance(); | ||
|
||
$response = $this->getMockBuilder('Rvdv\Nntp\Response\Response') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
try { | ||
$command->onNoSuchArticleNumber($response); | ||
$this->fail('->onNoSuchArticleNumber() throws a Rvdv\Nntp\Exception\RuntimeException because the server indicated the article number does not exist'); | ||
} catch (\Exception $e) { | ||
$this->assertInstanceof('Rvdv\Nntp\Exception\RuntimeException', $e, '->onNoSuchArticleNumber() throws a Rvdv\Nntp\Exception\RuntimeException because the server indicated the article number does not exist'); | ||
} | ||
} | ||
|
||
public function testItErrorsWhenNoSuchArticleIdResponse() | ||
{ | ||
$command = $this->createCommandInstance(); | ||
|
||
$response = $this->getMockBuilder('Rvdv\Nntp\Response\Response') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
try { | ||
$command->onNoSuchArticleId($response); | ||
$this->fail('->onNoSuchArticleId() throws a Rvdv\Nntp\Exception\RuntimeException because the server indicated the article id does not exist'); | ||
} catch (\Exception $e) { | ||
$this->assertInstanceof('Rvdv\Nntp\Exception\RuntimeException', $e, '->onNoSuchArticleId() throws a Rvdv\Nntp\Exception\RuntimeException because the server indicated the article id does not exist'); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function createCommandInstance() | ||
{ | ||
return new HeadCommand('12345'); | ||
} | ||
} |