Skip to content

Commit

Permalink
Uploading artifacts tests (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoapaes authored Mar 12, 2021
1 parent e88f340 commit 379c523
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ GITHUB_PR_NUMBER: Number of pull request of repository
GITHUB_TOKEN: Token of the user's github who will post the comment
TEST_LANG: Language
TEST_TITLE: Message to show in title
TEST_FOOTER: Show footer with version of PHP
TEST_FOOTER: Show footer with version of PHP
IMGBB_TOKEN: Token to upload image acceptance test
1 change: 1 addition & 0 deletions .github/workflows/ci-56.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
echo "GITHUB_REPO=codeception-github-reporter" >> .env
echo "GITHUB_PR_NUMBER=${{github.event.number}}" >> .env
echo "GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" >> .env
echo "IMGBB_TOKEN=${{ secrets.IMGBB_TOKEN }}" >> .env
echo "PHP_VERSION=56" >> .env
- name: Run tests
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci-73.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
echo "GITHUB_REPO=codeception-github-reporter" >> .env
echo "GITHUB_PR_NUMBER=${{github.event.number}}" >> .env
echo "GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" >> .env
echo "IMGBB_TOKEN=${{ secrets.IMGBB_TOKEN }}" >> .env
echo "PHP_VERSION=73" >> .env
- name: Run tests
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci-74.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
echo "GITHUB_REPO=codeception-github-reporter" >> .env
echo "GITHUB_PR_NUMBER=${{github.event.number}}" >> .env
echo "GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" >> .env
echo "IMGBB_TOKEN=${{ secrets.IMGBB_TOKEN }}" >> .env
echo "PHP_VERSION=74" >> .env
- name: Run tests
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci-80.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
echo "GITHUB_REPO=codeception-github-reporter" >> .env
echo "GITHUB_PR_NUMBER=${{github.event.number}}" >> .env
echo "GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" >> .env
echo "IMGBB_TOKEN=${{ secrets.IMGBB_TOKEN }}" >> .env
echo "PHP_VERSION=80" >> .env
- name: Run tests
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [GitHubReporter] Reporter for PHP testing frameworks
# [GitHubReporter] Reporter for PHP testing frameworks [![PHP 5.6](https://github.com/likesistemas/codeception-github-reporter/actions/workflows/ci-56.yml/badge.svg)](https://github.com/likesistemas/codeception-github-reporter/actions/workflows/ci-56.yml) [![PHP 7.3](https://github.com/likesistemas/codeception-github-reporter/actions/workflows/ci-73.yml/badge.svg)](https://github.com/likesistemas/codeception-github-reporter/actions/workflows/ci-73.yml) [![PHP 7.4](https://github.com/likesistemas/codeception-github-reporter/actions/workflows/ci-74.yml/badge.svg)](https://github.com/likesistemas/codeception-github-reporter/actions/workflows/ci-74.yml) [![PHP 8.0](https://github.com/likesistemas/codeception-github-reporter/actions/workflows/ci-80.yml/badge.svg)](https://github.com/likesistemas/codeception-github-reporter/actions/workflows/ci-80.yml)

## Installation

Expand Down Expand Up @@ -35,4 +35,9 @@ extensions:
- GITHUB_OWNER: Owner of repository
- GITHUB_REPO: Name of repository
- GITHUB_PR_NUMBER: Number of pull request of repository
- GITHUB_TOKEN: Token of the user's github who will post the comment
- GITHUB_TOKEN: Token of the user's github who will post the comment
- IMGBB_TOKEN=cfac55381b42ee1e55bf17fa185b3b51
- TEST_LANG: Language
- TEST_TITLE: Message to show in title
- TEST_FOOTER: Show footer with version of PHP
- IMGBB_TOKEN: Token to upload image acceptance test
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ services:
- TEST_LANG=$TEST_LANG
- TEST_TITLE=$TEST_TITLE
- TEST_FOOTER=$TEST_FOOTER
- IMGBB_TOKEN=$IMGBB_TOKEN
volumes:
- ./:/var/www/
networks:
Expand Down
49 changes: 43 additions & 6 deletions src/GitHubReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
use Codeception\Events;
use Codeception\Extension;
use Codeception\Test\Descriptor;
use Codeception\Test\Interfaces\ScenarioDriven;
use Codeception\TestInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use InvalidArgumentException;
use LogicException;

class GitHubReporter extends Extension
{

/** @var array */
private $lang;

/** @var string */
private $currentTest = '';

Expand All @@ -34,6 +40,7 @@ public function _initialize()
$this->options['silent'] = false; // turn on printing for this extension
$this->_reconfigure(['settings' => ['silent' => true]]); // turn off printing for everything else
$this->standardReporter = new Console($this->options);
$this->lang = Lang::getLang(getenv('TEST_LANG'));
}

// we are listening for events
Expand Down Expand Up @@ -122,9 +129,16 @@ public function printFailed(FailEvent $e)
$error[] = $fail->getMessage();
}

$trace = $this->getExceptionTrace($fail);
if ($trace) {
$error += $trace;
if ($failedTest instanceof ScenarioDriven) {
$reports = $this->getReports($failedTest);
if ($reports) {
$error[] = $reports;
}
} else {
$trace = $this->getExceptionTrace($fail);
if ($trace) {
$error += $trace;
}
}

$this->errors[] = $error;
Expand All @@ -134,17 +148,40 @@ public function printFailed(FailEvent $e)

use StackTrace;

private function getReports(TestInterface $failedTest)
{
$reports = $failedTest->getMetadata()->getReports();
if (isset($reports['png'])) {
$token = getenv('IMGBB_TOKEN');

try {
if (! $token) {
throw new InvalidArgumentException('To upload it informs the environment variable `TOKEN_IMGBB`.');
}

$upload = new UploadImage($token);
$url = $upload->upload($reports['png']);
return sprintf($this->lang['image'], $url);
} catch (RequestException $ex) {
$this->writeln("Error on upload image: {$ex->getMessage()}");
} catch (LogicException $ex) {
$this->writeln($ex->getMessage());
} catch (InvalidArgumentException $ex) {
$this->writeln($ex->getMessage());
}
}
}

public function result()
{
$TITLE = getenv('TEST_TITLE');
$FOOTER = (getenv('TEST_FOOTER') ?: 'true') === 'true';
$LANG = Lang::getLang(getenv('TEST_LANG'));
$OWNER = getenv('GITHUB_OWNER');
$REPO = getenv('GITHUB_REPO');
$PR_NUMBER = getenv('GITHUB_PR_NUMBER');
$TOKEN = getenv('GITHUB_TOKEN');

$body = $this->getBodyMessage($LANG, $TITLE, $FOOTER);
$body = $this->getBodyMessage($this->lang, $TITLE, $FOOTER);
$this->writeln('');
$this->writeln($body);

Expand Down Expand Up @@ -192,7 +229,7 @@ private function getBodyMessage(array $lang, $title = null, $footer = true)
} else {
$message .= sprintf($lang['fail'], count($this->errors))."\n\n";
foreach ($this->errors as $error) {
$message .= "```\n".join("\n", $error)."\n```\n";
$message .= "```Markdown\n".join("\n", $error)."\n```\n";
}
$message .= "\n\n";
}
Expand Down
7 changes: 7 additions & 0 deletions src/Lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ class Lang
'success' => ':sunglasses: Parabéns, seus testes passaram...',
'fail' => ':rage: Que pena, ocorreram %u erro(s) em seus testes!',
'footer' => 'Testes foram realizados usando %s',
'image' => 'Visualizar imagem em %s',
],
'en-us' => [
'success' => ':sunglasses: Congratulations, your tests have passed...',
'fail' => ':rage: Your tests failed, there were %u errors!',
'footer' => 'Tests were performed using %s',
'image' => 'To view image in %s',
],
];

Expand All @@ -27,4 +29,9 @@ public static function getLang($lang)

return self::$langs[$lang];
}

public static function add($lang, $data)
{
self::$langs[$lang] = $data;
}
}
53 changes: 53 additions & 0 deletions src/UploadImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Like\Codeception;

use GuzzleHttp\Client;
use LogicException;

class UploadImage
{
const URL = 'https://api.imgbb.com/1/upload';
const EXPIRATION_IN_SECONDS = 10080; // One week

/** @var string */
private $token;

public function __construct($token)
{
$this->token = $token;
}

public function upload($fileToUpload)
{
$url = self::URL .
'?expiration=' . self::EXPIRATION_IN_SECONDS .
'&key=' . $this->token;

$client = new Client();
$response = $client->request('POST', $url, [
'multipart' => [
[
'name' => 'image',
'contents' => fopen($fileToUpload, 'r'),
],
],
]);

$body = (string) $response->getBody();
if (! $body) {
throw new LogicException('Body is empty.');
}

$json = json_decode($body, true);
if (! is_array($json)) {
throw new LogicException('Body is json. Body: ' . $body);
}

if (! isset($json['data']) || ! isset($json['data']['url'])) {
throw new LogicException('Body is valid json. Body: ' . $body);
}

return $json['data']['url'];
}
}

0 comments on commit 379c523

Please sign in to comment.