Skip to content
This repository has been archived by the owner on May 25, 2023. It is now read-only.

Commit

Permalink
Add Test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
pcsaini committed Dec 7, 2022
1 parent a92de6f commit fa9bbda
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 6 deletions.
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"Prem\\ResponseBuilder\\Feature\\ResponseBuilderTest::test_success":3},"times":{"Prem\\ResponseBuilder\\Feature\\ResponseBuilderTest::test_success":0.149,"Prem\\ResponseBuilder\\Feature\\ResponseBuilderTest::test_error":0.01}}
13 changes: 11 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@
}
],
"require": {
"php": "^8.1 | ^8.2",
"laravel/framework": ">= 8.0"
"php": ">=8.0",
"illuminate/http": "~7.0"
},
"require-dev": {
"orchestra/testbench": "7.*",
"phpunit/phpunit": "9.*"
},
"autoload": {
"psr-4": {
"Prem\\ResponseBuilder\\": "src/",
"Prem\\ResponseBuilder\\Tests\\": "tests"
}
},
"autoload-dev": {
"psr-4": {
"Prem\\ResponseBuilder\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
Expand Down
29 changes: 29 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<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="Feature">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_KEY" value="base64:9LNymWc/qr+yXLDCC1AFo9b0rLbQSROeC1Em+XgJgtg="/>
</php>
</phpunit>
9 changes: 5 additions & 4 deletions src/Http/ValidationFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Prem\ResponseBuilder\Http;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Validation\ValidationException;
use Prem\ResponseBuilder\ResponseBuilder;

Expand All @@ -18,15 +19,15 @@ trait ValidationFailed
*/
protected function failedValidation(Validator $validator)
{
$response = ResponseBuilder::asError(config('api.response.validation_failed_message'))
->when(config('api.response.show_validation_failed_message') === 'first', function ($builder) use ($validator) {
$response = ResponseBuilder::asError(config('api-response.validation_http_code'))
->when(config('api-response.show_validation_failed_message') === 'first', function ($builder) use ($validator) {
return $builder->withMessage($validator->errors()->first());
})
->when(config('api.response.show_validation_failed_message') === 'all', function ($builder) use ($validator) {
->when(config('api-response.show_validation_failed_message') === 'all', function ($builder) use ($validator) {
return $builder->with('errors', $validator->errors());
})
->build();

throw new ValidationException($validator, $response);
throw new HttpResponseException($response);
}
}
53 changes: 53 additions & 0 deletions tests/Feature/ResponseBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Prem\ResponseBuilder\Feature;

use Illuminate\Http\Response;
use Prem\ResponseBuilder\ResponseBuilder;
use Prem\ResponseBuilder\Tests\TestCase;

class ResponseBuilderTest extends TestCase
{
public function test_success(): void
{
$httpOk = Response::HTTP_OK;

$data = [
'name' => 'Prem Chand Saini',
'email' => '[email protected]'
];

$message = 'Getting the user details';

$res = ResponseBuilder::success($data, $message);

$this->assertEquals($httpOk, $res->status());

$res = json_decode($res->getContent());

$this->assertObjectHasAttribute('status', $res);
$this->assertIsBool(true, $res->status);
$this->assertObjectHasAttribute('data', $res);
$this->assertEquals($data['name'], $res->data->name);
$this->assertEquals($data['email'], $res->data->email);
$this->assertObjectHasAttribute('message', $res);
$this->assertEquals($message, $res->message);
}

public function test_error(): void
{
$message = 'Whoops! Something went wrong please try after some time';
$code = Response::HTTP_INTERNAL_SERVER_ERROR;

$res = ResponseBuilder::error($message, $code);

$this->assertEquals($code, $res->status());

$res = json_decode($res->getContent());

$this->assertObjectHasAttribute('status', $res);
$this->assertIsBool(false, $res->status);
$this->assertObjectHasAttribute('message', $res);
$this->assertEquals($message, $res->message);
}
}
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 Prem\ResponseBuilder\Tests;

use Prem\ResponseBuilder\ResponseBuilderServiceProvider;

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

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

protected function getEnvironmentSetUp($app)
{
// perform environment setup
}
}

0 comments on commit fa9bbda

Please sign in to comment.