Skip to content

Commit

Permalink
Version 1 PR (Open for ongoing review until finalised) (#10)
Browse files Browse the repository at this point in the history
* Various improvements to the make:api-resource generator command
Get rid of class_basename helper because laravel adds it in 5.8
Add laravel support 5.8 as a dependency

* Routes should be kebab case, not snake

* Fix missing bracket

* Update migrate command with create_table style name

* Updating $with behaviour and splitting into item and collection

* Put should take request

* viewAll policy ability & stub update

* First pass at put

* Better handling of saving model in service, immediate update to put

* Extend json formatter

* StyleCI fixes

* Minor improvements to str handling

* Move before function to laravel base repo for boilerplate, to make it more clear/transparent what is happening

* Improvements to validating model update - especially relating to multi/level rule keys (separated by dots)

* Remove blank line (cs)

* Typehints for array on some service functions

* Simplify logic with Str::before

* V1 testing approach (#12)

* First testing approach

* Much improved approach

* Removing old location of files

* Large amount of progress

* Reorganising tests directory
Automating config copying & putting overrides in phpunit.xml
Renaming routes file for clarity

* Commit travis-ci file

* Specify matrix

* Exclude test dir from styleci

* Added php 7.4 to travis

* Attempt to remove xdebug

* Update test script

* Add a testing note

* Add restful service test

* Add phpunit dist

* Remove php 7.4 snap from travis

* Add travis shield

* Separating out testing setup from base testcase

* Make the Policy stub return true from all functions by default (ie. default functionality as if there were no policy)

* Implement JSON formatting for meta in responses
Refactor the function to format keycase from transformer into apiboilerplate class

* Fixed static references

* Refactoring to put helpers in a class

* Update controllers to use new itemWith and collectionWith
Revert them back to public static as well

* StyleCI
  • Loading branch information
specialtactics authored Jul 25, 2019
1 parent 503a7f3 commit 3a6c2e8
Show file tree
Hide file tree
Showing 94 changed files with 4,125 additions and 177 deletions.
4 changes: 2 additions & 2 deletions .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ disabled:

finder:
exclude:
- "tests"
- "vendor"
- "vendor"
- "test"
31 changes: 31 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
language: php

matrix:
include:
- php: 7.1
- php: 7.2
- php: 7.3

sudo: false

dist: xenial

env:
global:
- setup=basic
- xdebug=false

cache:
directories:
- $HOME/.composer/cache

before_install:
- if [[ $xdebug = 'true' ]] ; then phpenv config-rm xdebug.ini; fi

install:
- if [[ $setup = 'basic' ]]; then travis_retry composer install --prefer-dist --no-interaction --no-suggest; fi
- if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-dist --no-interaction --no-suggest --prefer-stable; fi
- if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-dist --no-interaction --no-suggest --prefer-stable --prefer-lowest; fi

script:
- composer test
16 changes: 14 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,35 @@
"php": "^7.1.3",
"dingo/api": "^2",
"tymon/jwt-auth": "^1.0",
"illuminate/support": "^5.8",
"webpatser/laravel-uuid": "^3.0",
"ramsey/uuid": "^3.0",
"atehnix/laravel-stubs": "~6.0"
},
"require-dev": {
"ext-json": "*",
"beyondcode/laravel-dump-server": "^1.0",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^2.0",
"phpunit/phpunit": "^7.5"
"phpunit/phpunit": "^7.5",
"orchestra/testbench": "^3.8",
"fideloper/proxy": "^4.0"
},
"autoload": {
"psr-4": {
"Specialtactics\\L5Api\\": "src/"
},
"files": ["helpers/helpers.php"]
},
"autoload-dev": {
"psr-4": {
"Specialtactics\\L5Api\\Tests\\": "test/tests/",
"Specialtactics\\L5Api\\Test\\Mocks\\": "test/mocks/",
"App\\": "test/app/"
},
"classmap": ["test/database/seeds/", "test/database/factories/"]
},
"extra": {
"laravel": {
"providers": [
Expand All @@ -42,4 +54,4 @@
"scripts": {
"test": "./vendor/bin/phpunit --colors=always -v --testdox"
}
}
}
91 changes: 9 additions & 82 deletions helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,116 +13,43 @@ function APIUser()
/**
* Recursively camel-case an array's keys
*
* @deprecated Use Helpers.php
* @param $array
* @param int|null $levels How many levels of an array keys to transform - by default recurse infiniately (null)
* @param int|null $levels How many levels of an array keys to transform - by default recurse infinitely (null)
* @return array $array
*/
function camel_case_array_keys($array, $levels = null)
{
foreach (array_keys($array) as $key) {
// Get a reference to the value of the key (avoid copy)
// Then remove that array element
$value = &$array[$key];
unset($array[$key]);

// Transform key
$transformedKey = camel_case($key);

// Recurse
if (is_array($value) && (is_null($levels) || --$levels > 0)) {
$value = camel_case_array_keys($value, $levels);
}

// Store the transformed key with the referenced value
$array[$transformedKey] = $value;

// We'll be dealing with some large values, so memory cleanup is important
unset($value);
}

return $array;
return Specialtactics\L5Api\Helpers::camelCaseArrayKeys($array, $levels);
}
}

if (! function_exists('snake_case_array_keys')) {
/**
* Recursively snake-case an array's keys
*
* @deprecated Use Helpers.php
* @param $array
* @param int|null $levels How many levels of an array keys to transform - by default recurse infiniately (null)
* @param int|null $levels How many levels of an array keys to transform - by default recurse infinitely (null)
* @return array $array
*/
function snake_case_array_keys(array $array, $levels = null)
{
foreach (array_keys($array) as $key) {
// Get a reference to the value of the key (avoid copy)
// Then remove that array element
$value = &$array[$key];
unset($array[$key]);

// Transform key
$transformedKey = snake_case($key);

// Recurse
if (is_array($value) && (is_null($levels) || --$levels > 0)) {
$value = snake_case_array_keys($value, $levels);
}

// Store the transformed key with the referenced value
$array[$transformedKey] = $value;

// We'll be dealing with some large values, so memory cleanup is important
unset($value);
}

return $array;
}
}

if (! function_exists('class_basename')) {

/**
* Get the basename of a class's FQNS name. This is proven to be the fastest way to do this (for now).
*
* @param string $className
* @return string
*/
function class_basename(string $className)
{
$reflection = new ReflectionClass($className);

return $reflection->getShortName();
}
}

if (! function_exists('get_calling_method')) {
/**
* Get the calling method name
*
* @return string
*/
function get_calling_method()
{
return debug_backtrace()[1]['function'];
return Specialtactics\L5Api\Helpers::snakeCaseArrayKeys($array, $levels);
}
}

if (! function_exists('model_relation_name')) {
/**
* Converts the name of a model class to the name of the relation of this resource on another model
*
* @deprecated Use Helpers.php
* @param string $resourceName The name of the resource we are dealing with
* @param string $relationType The type of relation - ie.. one to.. X ('one', 'many')
* @return string The name of the relation, as it would appear inside an eloquent model
* @throws \Exception
*/
function model_relation_name($resourceName, $relationType = 'many')
{
if ($relationType == 'many') {
return lcfirst(str_plural(class_basename($resourceName)));
} elseif ($relationType == 'one') {
return lcfirst(class_basename($resourceName));
} else {
throw new \Exception('Undefined relation type');
}
return Specialtactics\L5Api\Helpers::modelRelationName($resourceName, $relationType);
}
}
41 changes: 41 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<testsuites>
<testsuite name="API Test Suite">
<directory suffix="Test.php">./test/tests</directory>
<exclude>./test/tests/TestCase.php</exclude>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<php>
<env name="DB_CONNECTION" value="testing"/>
<env name="APP_ENV" value="testing"/>
<!-- Test App -->
<env name="APP_DEBUG" value="true"/>
<env name="APP_URL" value="http://localhost"/>
<env name="APP_KEY" value="CC58JtJ6QNRTnAr3tdwTw6qMuiK4nDTaozD8Uk3zQ0xuTc6VbW2DBek3cArbnZTx"/>
<env name="API_PREFIX" value="/"/>
<env name="JWT_SECRET" value="WrL8dp51k231ErDyUMgazU9KceL1RKXusu1U39YARuMbUWKuurPEtqGhEDCrUMoB"/>
<env name="JWT_TTL" value="7220"/>
<!-- Test App -->
</php>
</phpunit>
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![Build Status](https://travis-ci.com/specialtactics/l5-api.svg?branch=master)](https://travis-ci.com/specialtactics/l5-api)
[![StyleCI](https://github.styleci.io/repos/131504248/shield?branch=master)](https://github.styleci.io/repos/131504248)
[![Github All Releases](https://img.shields.io/packagist/dt/specialtactics/l5-api.svg)]()
[![Github All Releases](https://img.shields.io/packagist/dm/specialtactics/l5-api.svg)]()
Expand Down
12 changes: 9 additions & 3 deletions resources/stubs/model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ use App\Transformers\BaseTransformer;
class DummyClass extends BaseModel
{
/**
* @var string UUID key
* @var string UUID key of the resource
*/
public $primaryKey = '_id';

/**
* @var array Relations to load implicitly by Restful controllers
* @var null|array What relations should one model of this entity be returned with, from a relevant controller
*/
public static $localWith = [];
public static $itemWith = [];

/**
* @var null|array What relations should a collection of models of this entity be returned with, from a relevant controller
* If left null, then $itemWith will be used
*/
public static $collectionWith = null;

/**
* @var null|BaseTransformer The transformer to use for this model, if overriding the default
Expand Down
18 changes: 16 additions & 2 deletions resources/stubs/policy.stub
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class DummyClass extends BasePolicy
*/
public function create(DummyUser $user)
{
//
//@todo
return true;
}

/**
Expand All @@ -30,6 +31,18 @@ class DummyClass extends BasePolicy
return $this->own($user, $dummyModel);
}

/**
* Determine whether the user can view the collection of DummyModel.
*
* @param \NamespacedDummyUserModel $user
* @return mixed
*/
public function viewAll(DummyUser $user)
{
// @todo
return true;
}

/**
* Determine whether the user can update the DummyModel.
*
Expand Down Expand Up @@ -62,7 +75,8 @@ class DummyClass extends BasePolicy
* @return mixed
*/
public function own(DummyUser $user, DummyModel $dummyModel) {
//
// @todo
return true;
}

/**
Expand Down
Loading

0 comments on commit 3a6c2e8

Please sign in to comment.