Skip to content

Commit

Permalink
Merge pull request #6 from JoshPiper/feat/more-units
Browse files Browse the repository at this point in the history
Improved Unit Testing
  • Loading branch information
JoshPiper authored Mar 12, 2020
2 parents 62864e0 + 334b6e6 commit 9ad35a8
Show file tree
Hide file tree
Showing 14 changed files with 435 additions and 166 deletions.
107 changes: 0 additions & 107 deletions .github/workflows/phpunit.yml

This file was deleted.

71 changes: 71 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Unit Testing
on: push

env:
COMPOSER_ALLOW_SUPERUSER: 1

jobs:
phpunit:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: [7.2, 7.3, 7.4]
container: php:${{ matrix.php }}-alpine
steps:
- name: Install php.ini
run: mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
- name: Install APK Dependencies
run: apk update && apk add --no-cache -q git unzip mysql-client autoconf gcc g++ make perl bash
- name: Install PECL Extensions
run: pear config-set php_ini "$PHP_INI_DIR/php.ini" && pecl install pcov && docker-php-ext-enable pcov
- name: Install Composer
id: composer-install
run: |
wget -O installer https://getcomposer.org/installer
php installer --install-dir=/usr/local/bin --filename=composer
echo "::set-output name=cache-dir::$(composer global config cache-files-dir)"
- name: Checkout
uses: actions/checkout@v1
- name: Restore Composer Cache
uses: actions/cache@v1
with:
key: composer-${{ runner.os }}-${{ hashFiles('**/composer.lock') }}
path: ${{ steps.composer-install.outputs.cache-dir }}
restore-keys: composer-${{ runner.os }}- composer--
- name: Install Dependencies & Update PHPUnit
run: composer update --no-ansi --with-dependencies phpunit/phpunit
- name: Check PHPUnit Version
id: phpunit-check
run: |
info=$(composer show phpunit/phpunit)
version=$(echo "$info" | perl -ne 'print "$&\n" if /(?<=versions : \* )[\d\w.-_]+/s')
major=$(echo "$version" | perl -ne 'print "$&\n" if /\d/s')
echo "::set-output name=major::$major"
- name: Install Legacy POCV Polyfill
run: composer pcov-polyfill
if: steps.phpunit-check.outputs.major < 8
- name: Run Tests
run: composer test
continue-on-error: true
env:
WEBHOOK_URL: ${{ secrets.WEBHOOK_URL }}
WEBHOOK_ID: ${{ secrets.WEBHOOK_ID }}
WEBHOOK_TOKEN: ${{ secrets.WEBHOOK_TOKEN }}
- name: Upload Coverage
uses: actions/upload-artifact@v1
continue-on-error: true
with:
name: Coverage Report (${{ matrix.php }})
path: coverage/
- name: Upload Test Report
uses: actions/upload-artifact@v1
continue-on-error: true
with:
name: Test Report (${{ matrix.php }})
path: test.html
- name: Upload CodeCov Report
continue-on-error: true
run: wget -qO- https://codecov.io/bash | bash
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
![Unit Testing](https://github.com/JoshPiper/PHP-InterCord/workflows/Unit%20Testing/badge.svg)
[![codecov](https://codecov.io/gh/JoshPiper/PHP-InterCord/branch/master/graph/badge.svg)](https://codecov.io/gh/JoshPiper/PHP-InterCord)

## InterCord
InterCord is a simple PHP Discord Webhooks & Rich Embed library.

Expand Down
22 changes: 16 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,27 @@
"email": "[email protected]",
"role": "Developer"
}],
"autoload": {
"psr-4": {
"Internet\\InterCord\\": "src/"
}

"scripts": {
"test": "phpunit --bootstrap vendor/autoload.php --colors=never --whitelist src/ --testdox-html test.html --coverage-clover=coverage.xml tests",
"pcov-polyfill": [
"@composer require --dev pcov/clobber",
"pcov clobber"
]
},

"require": {
"php": ">=7.2",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.4"
},
},
"autoload": {
"psr-4": {
"Internet\\InterCord\\": "src/"
}
},

"require-dev": {
"phpunit/phpunit": "^9"
"phpunit/phpunit": ">=7 <10"
}
}
2 changes: 1 addition & 1 deletion src/Internal/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static function fromHex(string $code): Color{
* @return Color
*/
public static function fromDecimal(int $number): Color{
return new static($number & 0xFF0000, $number & 0x00FF00, $number & 0x0000FF);
return new static(($number & 0xFF0000) >> 16, ($number & 0x00FF00) >> 8, $number & 0x0000FF);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Internal/EmbedImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class EmbedImage extends EmbedVideo {
* @param int $height The height of the image.
* @param int $width The width of the image.
*/
public function __construct(string $url = '', string $proxy_url = '', int $height = 0, int $width = 0){
parent::__construct($url, $height, $width);
public function __construct(string $url = '', string $proxy_url = '', int $width = 0, int $height = 0){
parent::__construct($url, $width, $height);
$this->proxy_url = $proxy_url;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Internal/EmbedVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class EmbedVideo implements JsonSerializable {
/**
* EmbedVideo constructor.
* @param string $url Video URL
* @param int $height Height of the video.
* @param int $width Width of the video.
* @param int $height Height of the video.
*/
public function __construct(string $url = '', int $height = 0, int $width = 0){
public function __construct(string $url = '', int $width = 0, int $height = 0){
$this->url = $url;
$this->height = $height;
$this->width = $width;
Expand Down
12 changes: 6 additions & 6 deletions src/RichEmbed.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ public function setAuthor(string $name = '', string $url = '', string $icon = ''
* @param int $width
* @return $this
*/
public function setImage(string $url = '', string $proxy = '', int $height = 0, int $width = 0): self{
$this->image = new EmbedImage($url, $proxy, $height, $width);
public function setImage(string $url = '', string $proxy = '', int $width = 0, int $height = 0): self{
$this->image = new EmbedImage($url, $proxy, $width, $height);
return $this;
}

Expand All @@ -153,8 +153,8 @@ public function setImage(string $url = '', string $proxy = '', int $height = 0,
* @param int $width
* @return $this
*/
public function setThumbnail(string $url = '', string $proxy = '', int $height = 0, int $width = 0): self{
$this->thumbnail = new EmbedImage($url, $proxy, $height, $width);
public function setThumbnail(string $url = '', string $proxy = '', int $width = 0, int $height = 0): self{
$this->thumbnail = new EmbedImage($url, $proxy, $width, $height);
return $this;
}

Expand All @@ -165,8 +165,8 @@ public function setThumbnail(string $url = '', string $proxy = '', int $height =
* @param int $width
* @return $this
*/
public function setVideo(string $url = '', int $height = 0, int $width = 0): self{
$this->video = new EmbedVideo($url, $height, $width);
public function setVideo(string $url = '', int $width = 0, int $height = 0): self{
$this->video = new EmbedVideo($url, $width, $height);
return $this;
}

Expand Down
38 changes: 1 addition & 37 deletions src/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,45 +93,9 @@ public function execute($content, string $username = '', string $avatar = '', bo
}

if (!$await){
if ($response->getStatusCode() == 204){
return null;
} else {
throw new Exception("Message failed to send.");
}
return null;
} else {
return json_decode($response->getBody()->getContents());
}
}

public function deliver(Payload $payload){
$delivered = false;
while (!$delivered){
try {
$response = $this->post('', [
'body' => json_encode($payload),
'query' => ['wait' => true]
]);
$left = min(array_map('intval', $response->getHeader('X-RateLimit-Remaining')));
if ($left == 0){
$time = max(array_map('intval', $response->getHeader('X-RateLimit-Reset-After')));
sleep($time);
}
$delivered = true;
} catch (ClientException $client_exception){
$code = $client_exception->getCode();
if ($code == 429){
$response = $client_exception->getResponse();
$left = min(array_map('intval', $response->getHeader('X-RateLimit-Remaining')));
if ($left == 0){
$time = max(array_map('intval', $response->getHeader('X-RateLimit-Reset-After')));
sleep($time);
}
} elseif ($code == 502){
sleep(2);
} else {
throw $client_exception;
}
}
}
}
}
Loading

0 comments on commit 9ad35a8

Please sign in to comment.