Skip to content

Commit

Permalink
Merge pull request #9 from lion-packages/new
Browse files Browse the repository at this point in the history
New integrated features
  • Loading branch information
Sleon4 authored Mar 18, 2024
2 parents 3a8f515 + 2de1b9a commit 62fb7e5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 deletions.
53 changes: 45 additions & 8 deletions src/LionTest/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Lion\Test;

use Closure;
use Exception;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use RuntimeException;
Expand All @@ -13,7 +14,7 @@
* TestCase extended abstract test class, allows you to write unit tests in PHP
* using the PHPUnit framework.
*
* @property mixed $instance [Object that will be reflected]
* @property object $instance [Object that will be reflected]
* @property ReflectionClass $reflectionClass [Object of ReflectionClass class]
*
* @package Lion\Test
Expand All @@ -23,9 +24,9 @@ abstract class Test extends TestCase
/**
* [Object that will be reflected]
*
* @var mixed $instance
* @var object $instance
*/
private mixed $instance;
private object $instance;

/**
* [Object of ReflectionClass class]
Expand All @@ -37,12 +38,12 @@ abstract class Test extends TestCase
/**
* Initializes the object to perform a reflection on a class
*
* @param mixed $instance [Object of any type that is subjected to
* @param object $instance [Object of any type that is subjected to
* reflection]
*
* @return void
*/
public function initReflection(mixed $instance): void
public function initReflection(object $instance): void
{
$this->instance = $instance;

Expand Down Expand Up @@ -226,17 +227,53 @@ public function assertInstances(object $instance, array $instances): void
* Perform assertions implementing the use of outputs in the buffer
* with ob_start
*
* @param string $output [Expected Output Message]
* @param Closure $callback [Anonymous function to be executed within the
* context of output buffering]
*
* @return void
* @return string|false
*/
public function assertWithOb(Closure $callback): void
public function assertWithOb(string $output, Closure $callback): string|false
{
ob_start();

$callback();

ob_end_clean();
$outputGetClean = ob_get_clean();

$this->assertSame($output, $outputGetClean);

return $outputGetClean;
}

/**
* Gets a response string from the separation of a defined word
*
* @param string $message [Defined message]
* @param string $messageSplit [Separation text]
*
* @return string
*/
public function getResponse(string $message, string $messageSplit): string
{
$split = explode($messageSplit, $message);

return trim(end($split));
}

/**
* Gets the exception object when consuming an API
*
* @param Closure $callback [Function that executes the exception]
*
* @return Exception
*/
public function getExceptionFromApi(Closure $callback): Exception
{
try {
$callback();
} catch (Exception $e) {
return $e;
}
}
}
16 changes: 16 additions & 0 deletions tests/Provider/TestProviderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,20 @@ public function exampleMethod(): void
]
];
}

public static function getResponseProvider(): array
{
return [
[
'text' => 'testing in classes',
'split' => 'in',
'return' => 'classes'
],
[
'text' => 'example test',
'split' => ' ',
'return' => 'test'
]
];
}
}
25 changes: 21 additions & 4 deletions tests/TestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests;

use Exception;
use Lion\Test\Test;
use Tests\Provider\TestProviderTrait;

Expand All @@ -20,6 +21,8 @@ class TestTest extends Test
const FILE_NAME = 'image.png';
const FILE_NAME_CUSTOM = 'custom.png';
const JSON = ['name' => 'lion'];
const MESSAGE = 'Testing';
const EXCEPTION_MESSAGE = 'Exception in the tests';

private mixed $customClass;

Expand Down Expand Up @@ -157,11 +160,25 @@ public function testAssertInstances(object $instance, array|string $instances):

public function testAssertWithOb(): void
{
$this->assertWithOb(function() {
header('Content-Type: application/json');
echo('Testing');
$this->assertWithOb(self::MESSAGE, function() {
echo(self::MESSAGE);
});
}

/**
* @dataProvider getResponseProvider
*/
public function testGetResponse(string $text, string $split, string $return): void
{
$this->assertSame($return, $this->getResponse($text, $split));
}

$this->assertTrue(true);
public function testGetExceptionFromApi(): void
{
$exception = $this->getExceptionFromApi(function() {
throw new Exception(self::EXCEPTION_MESSAGE);
});

$this->assertSame(self::EXCEPTION_MESSAGE, $exception->getMessage());
}
}

0 comments on commit 62fb7e5

Please sign in to comment.