Skip to content

Commit

Permalink
Merge pull request #30 from yepwoo/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
mahmoodahmad100 authored Dec 29, 2022
2 parents 6ffffe6 + dc5b65e commit 97de1aa
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 121 deletions.
6 changes: 3 additions & 3 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ Summary of all the commands:

### Create new module

`php artisan laragine:module {ModuleName}`
`php artisan laragine:module ModuleName`

### Initialize the unit

`php artisan laragine:unit {UnitName} {--module=ModuleName} {--init}`
`php artisan laragine:unit UnitName --module=ModuleName --init`

### Create all the related stuff for the unit

`php artisan laragine:unit {UnitName} {--module=ModuleName}`
`php artisan laragine:unit UnitName --module=ModuleName`
2 changes: 1 addition & 1 deletion docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ After including Laragine, you have to install it by running the following comman
php artisan laragine:install
```

After installing the package you will find a directory called `unit_template` inside `core/Base`, that's the directory that has the default views that will be included in every unit you generate (after running this command `php artisan laragine:unit {UnitName} {--module=ModuleName}` keep reading to learn more about this command).
After installing the package you will find a directory called `unit_template` inside `core/Base`, that's the directory that has the default views that will be included in every unit you generate (after running this command `php artisan laragine:unit UnitName --module=ModuleName` keep reading to learn more about this command).

Please take a look at the blade files inside `core/Base/views` and `core/Base/unit_template` you will notice that `$global` variable is shared across all the views.
3 changes: 3 additions & 0 deletions docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ It's also very important to understand the following terms:

* The system response (including errors response if you applied what's in `Error Handling` section) to any request will be as in below examples (`status_code` is the http status code):

* to use pagination you can use the following (ex: index method in the base controller):
`return $this->sendResponse($this->resource::collection($this->model->paginate(30))->response()->getData(true))`

**Success Response:**

```json
Expand Down
2 changes: 1 addition & 1 deletion docs/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

To create a new module, run the following:

`php artisan laragine:module {ModuleName}`
`php artisan laragine:module ModuleName`

Here is an example:

Expand Down
4 changes: 2 additions & 2 deletions docs/unit.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ To create a new unit, there are 2 commands we have to run:

### Initialization

`php artisan laragine:unit {UnitName} {--module=ModuleName} {--init}`
`php artisan laragine:unit UnitName --module=ModuleName --init`

To initialize the unit with basic stuff (model, API controller and Web Controller) and after running the command you can configure the unit, here is an example:

Expand Down Expand Up @@ -51,7 +51,7 @@ You may have noticed that the values in `type` and `definition` are designed the

### Publishing

`php artisan laragine:unit {UnitName} {--module=ModuleName}`
`php artisan laragine:unit UnitName --module=ModuleName`

To create all the related stuff (migration, request, resource, factory, unit test ...etc) based on the previous command:

Expand Down
10 changes: 5 additions & 5 deletions src/Core/Base/ApiController.stub
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ class Controller extends \App\Http\Controllers\Controller
*/
public function update($id)
{
$model = $this->model->find($id);
$model->update($this->request->all());
return $this->sendResponse(new $this->resource($model), 'successfully updated.');
$current_model = $this->model->find($id);
$current_model->update($this->request->all());
return $this->sendResponse(new $this->resource($current_model), 'successfully updated.');
}

/**
Expand All @@ -105,8 +105,8 @@ class Controller extends \App\Http\Controllers\Controller
*/
public function destroy($id)
{
$model = $this->model->find($id);
$model->delete();
$current_model = $this->model->find($id);
$current_model->delete();
return $this->sendResponse([], 'successfully deleted.');
}
}
10 changes: 8 additions & 2 deletions src/Core/Base/SendResponse.stub
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,18 @@ trait SendResponse
*/
protected function sendExceptionResponse($e, $report = true)
{
$errors = [];
$message = 'OOPS! there is a problem in our side! we got your problem and we will fix that very soon.';

if ($report) {
report($e);
}

$message = 'OOPS! there is a problem in our side! we got your problem and we will fix that very soon.';
if (env('APP_DEBUG') == true) {
$errors = $e->errors();
$message = $e->getMessage();
}

return $this->sendResponse([], $message, false, 500);
return $this->sendResponse($errors, $message, false, 500);
}
}
107 changes: 107 additions & 0 deletions src/Core/Base/TestCase.stub
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ class TestCase extends BaseTestCase
{
use RefreshDatabase;

/**
* the base url
*
* @var string
*/
protected $base_url;

/**
* the data that will be sent in the request (create/update)
*
* @var array
*/
protected $data;

/**
* the json response
*
* @var array
*/
protected $json;

/**
* get test user credentials
*
Expand Down Expand Up @@ -100,4 +121,90 @@ class TestCase extends BaseTestCase
$result_key => []
];
}

/**
* create new entry
*
* @return Model
*/
protected function getNewEntry()
{
return Model::factory()->create();
}

/**
* get the id
*
* @return int
*/
protected function getId()
{
return $this->getNewEntry()->id;
}

/**
* Display a listing of the resource.
*
* @return void
*/
public function testItShouldGetListingOfTheResource()
{
$this->getNewEntry();
$current_json = $this->json;
$current_json['data'] = [];
$current_json['data']['*'] = $this->json['data'];

$this->json('GET', $this->base_url, [], $this->getHeaders())
->assertStatus(200)
->assertJsonStructure($current_json);
}

/**
* Store a newly created resource in storage.
*
* @return void
*/
public function testItShouldStoreNewlyCreatedResource()
{
$this->json('POST', $this->base_url, $this->data, $this->getHeaders())
->assertStatus(201)
->assertJsonStructure($this->json);
}

/**
* Display the specified resource.
*
* @return void
*/
public function testItShouldGetSpecifiedResource()
{
$this->json('GET', $this->base_url . $this->getId(), [], $this->getHeaders())
->assertStatus(200)
->assertJsonStructure($this->json);
}

/**
* update a resource in storage.
*
* @return void
*/
public function testItShouldUpdateSpecifiedResource()
{
$this->json('PUT', $this->base_url . $this->getId(), $this->data, $this->getHeaders())
->assertStatus(200)
->assertJsonStructure($this->json);
}

/**
* Remove the specified resource from storage.
*
* @return void
*/
public function testItShouldRemoveSpecifiedResource()
{
$this->json['data'] = [];
$this->json('DELETE', $this->base_url . $this->getId(), [], $this->getHeaders())
->assertStatus(200)
->assertJsonStructure($this->json);
}
}
107 changes: 0 additions & 107 deletions src/Core/Module/UnitTest.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,6 @@ use Core\#MODULE_NAME#\Models\#UNIT_NAME# as Model;

class #UNIT_NAME#Test extends TestCase
{
/**
* the base url
*
* @var string
*/
protected $base_url;

/**
* the data that will be sent in the request (create/update)
*
* @var array
*/
protected $data;

/**
* the json response
*
* @var array
*/
protected $json;

/**
* setting up
*
Expand All @@ -47,90 +26,4 @@ class #UNIT_NAME#Test extends TestCase
$this->json['data'][] = $key;
}
}

/**
* create new entry
*
* @return Model
*/
protected function getNewEntry()
{
return Model::factory()->create();
}

/**
* get the id
*
* @return int
*/
protected function getId()
{
return $this->getNewEntry()->id;
}

/**
* Display a listing of the resource.
*
* @return void
*/
public function testItShouldGetListingOfTheResource()
{
$this->getNewEntry();
$json = $this->json;
$json['data'] = [];
$json['data']['*'] = $this->json['data'];

$this->json('GET', $this->base_url, [], $this->getHeaders())
->assertStatus(200)
->assertJsonStructure($json);
}

/**
* Store a newly created resource in storage.
*
* @return void
*/
public function testItShouldStoreNewlyCreatedResource()
{
$this->json('POST', $this->base_url, $this->data, $this->getHeaders())
->assertStatus(201)
->assertJsonStructure($this->json);
}

/**
* Display the specified resource.
*
* @return void
*/
public function testItShouldGetSpecifiedResource()
{
$this->json('GET', $this->base_url . $this->getId(), [], $this->getHeaders())
->assertStatus(200)
->assertJsonStructure($this->json);
}

/**
* update a resource in storage.
*
* @return void
*/
public function testItShouldUpdateSpecifiedResource()
{
$this->json('PUT', $this->base_url . $this->getId(), $this->data, $this->getHeaders())
->assertStatus(200)
->assertJsonStructure($this->json);
}

/**
* Remove the specified resource from storage.
*
* @return void
*/
public function testItShouldRemoveSpecifiedResource()
{
$this->json['data'] = [];
$this->json('DELETE', $this->base_url . $this->getId(), [], $this->getHeaders())
->assertStatus(200)
->assertJsonStructure($this->json);
}
}

0 comments on commit 97de1aa

Please sign in to comment.