Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoBakx committed Feb 23, 2020
0 parents commit 3b79f0b
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Linku

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "linku/feedback-symfonystyle",
"description": "Add-on for linku/feedback for Symfony CLI feedback",
"type": "library",
"license": "MIT",
"require": {
"php": "^7.2.0",
"symfony/console": "^5.0",
"linku/feedback": "^1.0"
},
"autoload": {
"psr-4": {
"Linku\\SymfonyStyleFeedback\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Linku\\SymfonyStyleFeedback\\Tests\\": "tests/"
}
},
"require-dev": {
"phpunit/phpunit": "^9.0"
}
}
24 changes: 24 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
backupGlobals="false"
colors="true"
>
<php>
<ini name="error_reporting" value="-1" />
</php>

<testsuites>
<testsuite name="Linku SymfonyStyle Feedback Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
</phpunit>
62 changes: 62 additions & 0 deletions src/SymfonyStyleFeedback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);

namespace Linku\SymfonyStyleFeedback;

use Linku\Feedback\Feedback;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;

final class SymfonyStyleFeedback implements Feedback
{
/**
* @var SymfonyStyle
*/
private $io;

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

public function exception(Throwable $exception): void
{
$this->error($exception->getMessage());
}

public function warning(string $message): void
{
$this->io->text('<fg=yellow>'.$message.'</>');
}

public function error(string $message): void
{
$this->io->text('<fg=red>'.$message.'</>');
}

public function info(string $message): void
{
$this->io->text($message);
}

public function success(string $message): void
{
$this->io->success($message);
}

public function startProcess(int $total = 0): void
{
$this->io->writeln('');
$this->io->progressStart($total);
}

public function finishProcess(): void
{
$this->io->progressFinish();
}

public function advanceProcess(int $steps = 1): void
{
$this->io->progressAdvance($steps);
}
}
138 changes: 138 additions & 0 deletions tests/SymfonyStyleFeedbackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
declare(strict_types=1);

namespace Linku\SymfonyStyleFeedback\Tests;

use Linku\SymfonyStyleFeedback\SymfonyStyleFeedback;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use RuntimeException;
use Symfony\Component\Console\Style\SymfonyStyle;

final class SymfonyStyleFeedbackTest extends TestCase
{
/**
* @var SymfonyStyle|ObjectProphecy
*/
private $symfonyStyle;

/**
* @var SymfonyStyleFeedback
*/
private $testSubject;

protected function setUp(): void
{
$this->symfonyStyle = $this->prophesize(SymfonyStyle::class);

$this->testSubject = new SymfonyStyleFeedback(
$this->symfonyStyle->reveal()
);
}

public function testException(): void
{
$exceptionMessage = 'Something went wrong!';
$exception = new RuntimeException($exceptionMessage);
$expectedText = '<fg=red>'.$exceptionMessage.'</>';

$this->symfonyStyle->text($expectedText)
->shouldBeCalledOnce();

$this->testSubject->exception($exception);
}

public function testWarning(): void
{
$message = 'Something went wrong!';
$expectedText = '<fg=yellow>'.$message.'</>';

$this->symfonyStyle->text($expectedText)
->shouldBeCalledOnce();

$this->testSubject->warning($message);
}

public function testError(): void
{
$message = 'Something went wrong!';
$expectedText = '<fg=red>'.$message.'</>';

$this->symfonyStyle->text($expectedText)
->shouldBeCalledOnce();

$this->testSubject->error($message);
}

public function testInfo(): void
{
$message = 'Something happened.';

$this->symfonyStyle->text($message)
->shouldBeCalledOnce();

$this->testSubject->info($message);
}

public function testSuccess(): void
{
$message = 'Something went right!';

$this->symfonyStyle->success($message)
->shouldBeCalledOnce();

$this->testSubject->success($message);
}

public function testStartProcess(): void
{
$defaultTotal = 0;

$this->symfonyStyle->writeln('')
->shouldBeCalledOnce();
$this->symfonyStyle->progressStart($defaultTotal)
->shouldBeCalledOnce();

$this->testSubject->startProcess();
}

public function testStartProcessWithTotal(): void
{
$total = 25;

$this->symfonyStyle->writeln('')
->shouldBeCalledOnce();
$this->symfonyStyle->progressStart($total)
->shouldBeCalledOnce();

$this->testSubject->startProcess($total);
}

public function testFinishProcess(): void
{
$this->symfonyStyle->progressFinish()
->shouldBeCalledOnce();

$this->testSubject->finishProcess();
}

public function testAdvanceProcess(): void
{
$defaultSteps = 1;

$this->symfonyStyle->progressAdvance($defaultSteps)
->shouldBeCalledOnce();

$this->testSubject->advanceProcess();
}

public function testAdvanceProcessWithSteps(): void
{
$steps = 5;

$this->symfonyStyle->progressAdvance($steps)
->shouldBeCalledOnce();

$this->testSubject->advanceProcess($steps);
}
}

0 comments on commit 3b79f0b

Please sign in to comment.