Skip to content

Commit

Permalink
Merge pull request #136 from ker0x/hotfix/quick-replies
Browse files Browse the repository at this point in the history
[n/a] update tests for QuickReplies, bump PHPUnit version
  • Loading branch information
ker0x authored Dec 6, 2018
2 parents b118bc6 + 933db41 commit 96f7a99
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 16 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ before_script:

script:
- vendor/bin/php-cs-fixer fix --diff --dry-run
- vendor/bin/phpcs -s --config-set ignore_warnings_on_exit 1
- vendor/bin/phpunit --stderr --coverage-clover build/coverage/xml

after_script:
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ The Messenger library follows [SemVer](http://semver.org/).

## 3.x

**Changelog** (since [`3.1.0`](https://github.com/ker0x/messenger/compare/3.1.0...3.1.1))

- 3.1.0 (2018-10)
- Method `addQuickReply` will no longer thrown an exception if no quick replies were previously set (Thanks to @Khodl)
- Add new tests for QuickReplies
- Update `phpunit/phpunit` version to `7.4`.
- Remove `squizlabs/php_codesniffer` as a require-dev dependencies.

**Changelog** (since [`3.0.0`](https://github.com/ker0x/messenger/compare/3.0.0...3.1.0))

- 3.1.0 (2018-10)
Expand Down
1 change: 0 additions & 1 deletion _config.yml

This file was deleted.

3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
"psr/http-message": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^6.2.0",
"squizlabs/php_codesniffer": "^3.0",
"phpunit/phpunit": "^7.4",
"friendsofphp/php-cs-fixer": "^2.5",
"codacy/coverage": "^1.4"
},
Expand Down
11 changes: 1 addition & 10 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit color="true"
processIsolation="false"
<phpunit processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./tests/bootstrap.php">
<testsuites>
<testsuite name="Messenger Test Suite">
Expand All @@ -14,13 +12,6 @@
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>

<!-- Prevent coverage reports from looking in tests and vendors -->
<blacklist>
<directory suffix=".php">./vendor</directory>
<directory suffix=".php">./tests</directory>
<file>./tests/bootstrap.php</file>
</blacklist>
</filter>

<logging>
Expand Down
6 changes: 4 additions & 2 deletions src/Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ public function setQuickReplies(array $quickReplies): self
/**
* @param \Kerox\Messenger\Model\Message\QuickReply $quickReply
*
* @throws \Kerox\Messenger\Exception\MessengerException
*
* @return \Kerox\Messenger\Model\Message
*/
public function addQuickReply(QuickReply $quickReply): self
{
$this->isValidQuickReplies($this->quickReplies);
$this->isValidArray($this->quickReplies, 11);

$this->quickReplies[] = $quickReply;

Expand Down Expand Up @@ -126,7 +128,7 @@ public function setMetadata(string $metadata): self
*/
private function isValidQuickReplies(array $quickReplies): void
{
$this->isValidArray($quickReplies, 11);
$this->isValidArray($quickReplies, 11, 1);
foreach ($quickReplies as $quickReply) {
if (!$quickReply instanceof QuickReply) {
throw new InvalidClassException(sprintf(
Expand Down
45 changes: 45 additions & 0 deletions tests/TestCase/Model/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ public function testMessageWithQuickReplies(): void
$this->assertJsonStringEqualsJsonString($json, json_encode($message));
}

public function testMessageWithQuickRepliesUsingOnlyAddQuickReply(): void
{
$json = file_get_contents(__DIR__ . '/../../Mocks/Message/quick_reply.json');

$message = Message::create('Pick a color:')
->addQuickReply(QuickReply::create()
->setTitle('Red')
->setPayload('DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_RED')
->setImageUrl('http://petersfantastichats.com/img/red.png')
)->addQuickReply(QuickReply::create()
->setTitle('Green')
->setPayload('DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_GREEN')
->setImageUrl('http://petersfantastichats.com/img/green.png')
)
->addQuickReply(QuickReply::create(QuickReply::CONTENT_TYPE_LOCATION))
->setMetadata('some metadata');

$this->assertJsonStringEqualsJsonString($json, json_encode($message));
}

public function testMessageWithInvalidArgument(): void
{
$this->expectException(MessengerException::class);
Expand All @@ -108,4 +128,29 @@ public function testMessageWithInvalidQuickReplies(): void
'Hello',
]);
}

public function testMessageWithoutQuickReplies(): void
{
$this->expectException(MessengerException::class);
$this->expectExceptionMessage('The minimum number of items for this array is 1.');
$message = Message::create('Pick a color:')
->setQuickReplies([]);
}

public function testMessageWithToManyQuickReplies(): void
{
$quickReplies = [];
for ($i = 0; $i <= 11; $i++) {
$quickReplies[] = QuickReply::create()
->setTitle('Red')
->setPayload('DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_RED')
->setImageUrl('http://petersfantastichats.com/img/red.png');
}

$this->expectException(MessengerException::class);
$this->expectExceptionMessage('The maximum number of items for this array is 11.');
$message = Message::create('Pick a color:')
->setQuickReplies($quickReplies)
->addQuickReply(QuickReply::create(QuickReply::CONTENT_TYPE_LOCATION));
}
}

0 comments on commit 96f7a99

Please sign in to comment.