Skip to content

Commit

Permalink
added unit testing with 99% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nnjeim committed Sep 20, 2021
1 parent b9c352e commit 0c14383
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ build
composer.lock
docs
vendor
coverage
coverage
*.cache
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ All notable changes will be documented in this file
- general refactoring

## 1.1.0 - 2021-09-19
- Addition of the helpers method withAccepted and withCreated.
- Addition of the helpers method withAccepted and withCreated.

## 1.2.0 - 2021-09-20
- Unit testing with 99% code coverage.
- Possibility to add custom methods.



34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

## Respond Factory

A Laravel response helper methods.
A Laravel response helper methods. The package `respond` provides a fluent syntax to form array or json responses.
In its configuration file, it allows the addition of custom methods.

## Installation

Expand All @@ -17,6 +18,11 @@ composer require nnjeim/respond
php artisan vendor:publish --provider="Nnjeim\Respond\RespondServiceProvider"
```

The `respond.php` config file allows:
- The presetting of the response format array/json.
- The possibility to edit the response parameters.
- The possibility to add custom methods.

## Usage

##### Respond Facade
Expand Down Expand Up @@ -190,12 +196,38 @@ Not authorized reponse. The default response status code is 403.
'status' => 200,
];
```

## Custom methods

In the `respond.php` config file add in the responses array add an array entry where the key is name of the method in lower
case and the value contains desired success, message and status.

```
//example
'methodnotallowed' => [
'success' => false,
'message' => 'the method not allowed!',
'status' => Response::HTTP_METHOD_NOT_ALLOWED,
],
```

#### Usage

`Respond::withMethodNotAllowed();`

## Testing

The helpers and methods are tested with 99% coverage.
To run the tests.
``` bash
composer install
composer test
```

To run the coverage test.
``` bash
composer test-coverage
```
## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
52 changes: 28 additions & 24 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<php>
<env name="DB_CONNECTION" value="testing"/>
<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/>
</php>
</phpunit>
26 changes: 26 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Nnjeim\Respond\Tests;

use Nnjeim\Respond\RespondServiceProvider;

class TestCase extends \Orchestra\Testbench\TestCase
{
public function setUp(): void
{
parent::setUp();
// additional setup
}

protected function getPackageProviders($app)
{
return [
RespondServiceProvider::class,
];
}

protected function getEnvironmentSetUp($app)
{
// perform environment setup
}
}
213 changes: 213 additions & 0 deletions tests/Unit/RespondTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
<?php

namespace Nnjeim\Respond\Tests\Unit;

use Nnjeim\Respond\RespondException;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Http\JsonResponse;
use Illuminate\Foundation\Testing\WithFaker;
use Nnjeim\Respond\Tests\TestCase;
use Nnjeim\Respond\Respond;
use Illuminate\Support\Facades\Config;

class RespondTest extends TestCase
{
use WithFaker;

/** @test */
function can_respond_with_success()
{
$this->setToJsonToFalse();
$respond = Respond::setData($this->fakeData())->withSuccess();

self::assertTrue($respond['response']['success'] === true);
self::assertNotEmpty($respond['response']['data']);
self::assertTrue($respond['status'] === Response::HTTP_OK);
}

/** @test */
function can_respond_with_created()
{
$this->setToJsonToFalse();
$respond = Respond::setData($this->fakeData())->withCreated();

self::assertTrue($respond['response']['success'] === true);
self::assertNotEmpty($respond['response']['data']);
self::assertTrue($respond['status'] === Response::HTTP_CREATED);
}

/** @test */
function can_respond_with_accepted()
{
$this->setToJsonToFalse();
$respond = Respond::setData($this->fakeData())->withAccepted();

self::assertTrue($respond['response']['success'] === true);
self::assertNotEmpty($respond['response']['data']);
self::assertTrue($respond['status'] === Response::HTTP_ACCEPTED);
}

/** @test */
function can_respond_with_errors()
{
$this->setToJsonToFalse();
$respond = Respond::setErrors($this->fakeErrors())->withErrors();

self::assertTrue($respond['response']['success'] === false);
self::assertNotEmpty($respond['response']['errors']);
self::assertTrue($respond['status'] === Response::HTTP_UNPROCESSABLE_ENTITY);
}

/** @test */
function can_respond_with_no_content()
{
$this->setToJsonToFalse();
$respond = Respond::setMessage($this->faker->sentence())->withNoContent();

self::assertTrue($respond['response']['success'] === true);
self::assertTrue($respond['status'] === Response::HTTP_NO_CONTENT);
}

/** @test */
function can_respond_with_server_error()
{
$this->setToJsonToFalse();
$respond = Respond::setErrors($this->fakeErrors())->withServerError();

self::assertTrue($respond['response']['success'] === false);
self::assertNotEmpty($respond['response']['errors']);
self::assertTrue($respond['status'] === Response::HTTP_INTERNAL_SERVER_ERROR);
}

/** @test */
function can_respond_with_not_found()
{
$this->setToJsonToFalse();
$respond = Respond::setErrors($this->fakeErrors())->withNotFound();

self::assertTrue($respond['response']['success'] === false);
self::assertNotEmpty($respond['response']['errors']);
self::assertTrue($respond['status'] === Response::HTTP_NOT_FOUND);
}

/** @test */
function can_respond_with_not_authenticated()
{
$this->setToJsonToFalse();
$respond = Respond::setErrors($this->fakeErrors())->withNotAuthenticated();

self::assertTrue($respond['response']['success'] === false);
self::assertNotEmpty($respond['response']['errors']);
self::assertTrue($respond['status'] === Response::HTTP_UNAUTHORIZED);
}

/** @test */
function can_respond_with_not_authorized()
{
$this->setToJsonToFalse();
$respond = Respond::setErrors($this->fakeErrors())->withNotAuthorized();

self::assertTrue($respond['response']['success'] === false);
self::assertNotEmpty($respond['response']['errors']);
self::assertTrue($respond['status'] === Response::HTTP_FORBIDDEN);
}

/** @test */
function respond_can_set_status_code()
{
$statusCode = mt_rand(200, 300);

$respond = Respond::setStatusCode($statusCode)->withSuccess();

self::assertTrue($respond->getStatusCode() === $statusCode);
}

/** @test */
function respond_can_set_data()
{
$respond = Respond::setData($this->fakeData())->withSuccess();

self::assertNotEmpty($respond->getData()->data);
}

/** @test */
function respond_can_set_message()
{
$message = $this->faker->sentence();

$respond = Respond::setMessage($message)->withSuccess();

self::assertTrue($respond->getData()->message === $message);
}

/** @test */
function respond_can_set_errors()
{
$errors = $this->fakeErrors();

$respond = Respond::setErrors($errors)->withErrors();

self::assertNotEmpty($respond->getData()->errors);
}

/** @test */
function respond_can_set_meta()
{
$respond = Respond::setData($this->fakeData())
->setMeta([
'meta' => $this->faker->word(),
])
->withSuccess();

self::assertNotEmpty($respond->getData()->meta);
}

/** @test */
function respond_can_respond_in_json()
{
$respond = Respond::toJson()->setData($this->fakeData())->withSuccess();

self::assertInstanceOf(JsonResponse::class, $respond);
}

/** @test */
function respond_can_throw_exception()
{
$this->expectException(RespondException::class);

Respond::setData($this->fakeData())->withUnknowMethod();
}

/**
* set response to array.
*/
private function setToJsonToFalse(): void
{
Config::set('respond.toJson', false);
}

/**
* Fake data array.
* @return array
*/
private function fakeData(): array
{
return [
'id' => mt_rand(1, 1000),
'name' => $this->faker->name(),
'email' => $this->faker->email(),
'address' => $this->faker->address(),
];
}

/**
* Fake error array.
* @return array[]
*/
private function fakeErrors(): array
{
return [
[$this->faker->sentence()], [$this->faker->sentence()],
];
}
}

0 comments on commit 0c14383

Please sign in to comment.