Skip to content

Commit

Permalink
Merge pull request #2 from EvanDarwin/dev
Browse files Browse the repository at this point in the history
Version 2.0.0
  • Loading branch information
EvanDarwin authored Jan 29, 2020
2 parents 3457311 + 20c245b commit 0622215
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 129 deletions.
16 changes: 7 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
language: php
install: composer install -o
install:
- wget -O phpunit https://phar.phpunit.de/phpunit-8.phar
- chmod +x ./phpunit
- composer install -o
php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm
- nightly
after_success:
- CODECLIMATE_REPO_TOKEN=f9c21f5f87187047d967a0f50281bd5b663820942a7b3e914c9d0b19c3d819f3 ./vendor/bin/test-reporter # CodeClimate
- 7.2
- 7.3
- 7.4
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "evandarwin/laravel-jsend",
"version": "2.0.0",
"description": "Laravel JSend Response Helper",
"keywords": [
"laravel",
Expand All @@ -15,13 +16,13 @@
}
],
"require": {
"php": ">=5.4.0",
"evandarwin/jsend": "1.2.*",
"illuminate/support": "~5.0"
"php": ">=7.2.0",
"evandarwin/jsend": "~2.0.0",
"illuminate/support": "~6.0",
"ext-json": "*"
},
"require-dev": {
"orchestra/testbench": "~3.0",
"codeclimate/php-test-reporter": "dev-master"
"orchestra/testbench": "~4.0"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 0 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"
mapTestClassNameToCoveredClassName="false"
printerClass="PHPUnit_TextUI_ResultPrinter"
bootstrap="tests/bootstrap.php"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"
verbose="false">

<testsuites>
Expand All @@ -24,9 +21,6 @@
</testsuites>

<filter>
<blacklist>
<file>tests/bootstrap.php</file>
</blacklist>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
<exclude>
Expand Down
10 changes: 4 additions & 6 deletions src/EvanDarwin/JSend/Laravel/Builder/JSendLaravelBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

use EvanDarwin\JSend\JSendBuilder;

class JSendLaravelBuilder extends JSendBuilder
{
public function get($statusCode = 200)
{
return response(json_encode(parent::get()->getArray()), $statusCode)
class JSendLaravelBuilder extends JSendBuilder {
public function get(int $statusCode = 200) {
return response(json_encode(parent::get()->getArray()), $statusCode)
->header('Content-Type', 'application/json');
}
}
}
52 changes: 24 additions & 28 deletions src/EvanDarwin/JSend/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,29 @@

use Illuminate\Support\ServiceProvider as LaravelServiceProvider;

class ServiceProvider extends LaravelServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerHelpers();
}

/**
* Registers custom helper functions
*/
private function registerHelpers()
{
if (!function_exists('jsend')) {
require_once 'helpers.php';
class ServiceProvider extends LaravelServiceProvider {
/**
* Register the service provider.
*
* @return void
*/
public function register(): void {
$this->registerHelpers();
}

/**
* Registers custom helper functions
*/
private function registerHelpers(): void {
if (!function_exists('jsend')) {
require_once 'helpers.php';
}
}

public function boot(): void {
// Publish our configuration files
$this->publishes([
__DIR__ . '/resources/configs/jsend.php' => config_path('jsend.php')
], 'config');
}
}

public function boot()
{
// Publish our configuration files
$this->publishes([
__DIR__ . '/resources/configs/jsend.php' => config_path('jsend.php')
], 'config');
}
}
4 changes: 3 additions & 1 deletion src/EvanDarwin/JSend/Laravel/helpers.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

use EvanDarwin\JSend\Laravel\Builder\JSendLaravelBuilder;

/**
* JSend helper function.
*
* @return \EvanDarwin\JSend\Laravel\Builder\JSendLaravelBuilder
* @return JSendLaravelBuilder
*/
function jsend()
{
Expand Down
6 changes: 4 additions & 2 deletions src/EvanDarwin/JSend/Laravel/resources/configs/jsend.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

use EvanDarwin\JSend\Laravel\Builder\JSendLaravelBuilder;

return array(
/**
* Configuration file for Laravel JSend.
*/

/*
* Default:
* \EvanDarwin\JSend\Laravel\Builder\JSendLaravelBuilder
*/
'builder' => '\EvanDarwin\JSend\Laravel\Builder\JSendLaravelBuilder',
'builder' => JSendLaravelBuilder::class,
);
17 changes: 9 additions & 8 deletions tests/LaravelJSend_TestCase.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

abstract class LaravelJSend_TestCase extends \Orchestra\Testbench\TestCase
{
protected function getPackageProviders($app)
{
// Set the default builder
$app['config']->set('jsend.builder', '\EvanDarwin\JSend\Laravel\Builder\JSendLaravelBuilder');
use EvanDarwin\JSend\Laravel\Builder\JSendLaravelBuilder;
use EvanDarwin\JSend\Laravel\ServiceProvider;
use Orchestra\Testbench\TestCase;

return ['EvanDarwin\JSend\Laravel\ServiceProvider'];
}
abstract class LaravelJSend_TestCase extends TestCase {
protected function getPackageProviders($app): array {
// Set the default builder
$app['config']->set('jsend.builder', JSendLaravelBuilder::class);
return [ServiceProvider::class];
}
}
6 changes: 4 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

$loaderPath = dirname(__DIR__) . '/vendor/autoload.php';

if (!file_exists($loaderPath))
die("Please run 'composer install' before running tests" . PHP_EOL);
if (!file_exists($loaderPath)) {
die("Please run 'composer install' before running tests" . PHP_EOL);
}

/** @noinspection PhpIncludeInspection */
require $loaderPath;
118 changes: 56 additions & 62 deletions tests/cases/JSendBuilderTest.php
Original file line number Diff line number Diff line change
@@ -1,65 +1,59 @@
<?php

class JSendBuilderTest extends LaravelJSend_TestCase
{
public function testDefaultValues()
{
$response = jsend()->get();

$this->assertJson(json_encode(array(
'status' => 'success',
'data' => array()
)), $response);
}

public function testRendersCorrectly()
{
\Route::get('/jsend', function () {
return jsend()->failed()->code(404)->get();
});

$response = $this->call('get', '/jsend');

$this->assertJson(json_encode(array(
'status' => 'failed',
'code' => 404,
'data' => array()
)), $response);
}

public function testRenderCompatibility()
{
\Route::get('/jsend', function () {
return jsend()->failed()->code(404)->get();
});

$response = $this->call('get', '/jsend');

$this->assertJson(json_encode(array(
'status' => 'failed',
'code' => 404,
'data' => array()
)), $response);
}

public function testHttpStatus()
{
\Route::get('/jsend_http', function() {
return jsend()->failed()->code(403)->get(403);
});

$response = $this->call('get', '/jsend_http');

$this->assertResponseStatus(403);
}

/**
* @expectedException InvalidArgumentException
*/
public function testInvalidBuilderException()
{
$this->app['config']->set('jsend.builder', '');

jsend()->get();
}
use Illuminate\Support\Facades\Route;

class JSendBuilderTest extends LaravelJSend_TestCase {
public function testDefaultValues(): void {
$response = jsend()->get();

$this->assertJson(json_encode(array(
'status' => 'success',
'data' => array()
)), $response);
}

public function testRendersCorrectly(): void {
Route::get('/jsend', static function () {
return jsend()->failed()->code(404)->get();
});

$response = $this->call('get', '/jsend');

$this->assertEquals(json_encode(array(
'status' => 'fail',
'data' => array(),
'errors' => null,
'code' => 404,
)), $response->getContent());
}

public function testRenderCompatibility(): void {
Route::get('/jsend', static function () {
return jsend()->failed()->code(404)->get();
});

$response = $this->call('get', '/jsend');

$this->assertEquals(json_encode(array(
'status' => 'fail',
'data' => array(),
'errors' => null,
'code' => 404,
)), $response->getContent());
}

public function testHttpStatus(): void {
Route::get('/jsend_http', static function () {
return jsend()->failed()->code(403)->get(403);
});

$res = $this->call('get', '/jsend_http');
$this->assertEquals(403, $res->status());
}

public function testInvalidBuilderException(): void {
$this->expectException(InvalidArgumentException::class);
$this->app['config']->set('jsend.builder', '');
jsend()->get();
}
}

0 comments on commit 0622215

Please sign in to comment.