Skip to content

Commit 442146e

Browse files
committed
Update package tests and readme.
Define test routes in a RouteServiceProvider class. Fix phpunit running tests without declaring specific class.
1 parent 7fb2a4f commit 442146e

File tree

6 files changed

+77
-57
lines changed

6 files changed

+77
-57
lines changed

composer.lock

Lines changed: 13 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
>
1313
<testsuites>
1414
<testsuite name="Package Test Suite">
15-
<directory suffix=".php">./tests/</directory>
15+
<directory suffix="Test.php">./tests</directory>
1616
</testsuite>
1717
</testsuites>
1818
<filter>

readme.md

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,14 @@ The views are built using bootstrap (v3), but the styling can easily be override
1111

1212
## Installation
1313

14-
### Step 1: Composer
14+
### Composer
1515

1616
From the command line, run:
1717

1818
```
1919
composer require achillesp/laravel-crud-forms
2020
```
2121

22-
### Step 2: Service Provider (Laravel < 5.5)
23-
24-
If using a Laravel version before 5.5, you will need to add the service provider to your app's `providers` array in `config/app.php`:
25-
26-
```
27-
Achillesp\CrudForms\CrudFormsServiceProvider::class
28-
```
29-
3022
### Configuration
3123

3224
This package uses a config file which you can override by publishing it to your app's config dir.
@@ -67,16 +59,16 @@ class PostController extends Controller
6759
}
6860
```
6961

70-
In the controller's constructor we define the properties which are handled by the controller.
71-
The available properties we can define are as follows.
62+
In the controller's constructor you can define the properties which are handled by the controller.
63+
The available properties that can be defined are as follows.
7264

7365
### The model
7466

7567
This is the model, which should be passed in the constructor through Dependency Injection.
7668

7769
### The formFields array
7870

79-
This is an array of all the fields you need in the forms. Each field is an array that has:
71+
This is an array of all the fields you need in the forms. Each field is declared as an array that has:
8072
1. `name`: This is the model's attribute name, as it is in the database.
8173
2. `label`: This is the field's label in the forms.
8274
3. `type`: The type of the form input field that will be used. Accepted types are:
@@ -93,7 +85,7 @@ This is an array of all the fields you need in the forms. Each field is an array
9385
- radio
9486
4. `relationship`: This is needed in case of a select, select_multiple, radio or checkbox_multiple buttons.
9587
You can state here the name of the relationship as it is defined in the model.
96-
In the example above, the `Post` model has a `belongsTo` relationship to `category` and a `belongsToMany` relationship to `tags`.
88+
In the example bellow, the `Post` model has a `belongsTo` relationship to `category` and a `belongsToMany` relationship to `tags`.
9789
For `belongsTo` relationships you can use a select or a radio(group of radios) input.
9890
For `belongsToMany` relationships you can use a select_multiple or checkbox_multiple inputs.
9991
5. `relFieldName`: This is optional. It is used only in case we have a relationship, to set the name of the attribute of the related model that is displayed (ie. in a select's options).
@@ -123,7 +115,8 @@ If not defined, then the first of the `formFields` is shown.
123115

124116
### The `formTitle` (optional)
125117

126-
You can optionally, define the name of the model as we want it to appear in the views. If not defined, the name of the model will be used.
118+
You can optionally, define the name of the model as we want it to appear in the views.
119+
If not defined, the name of the model will be used.
127120

128121
### The `bladeLayout` (optional)
129122

@@ -149,12 +142,12 @@ These are the rules we want to use to validate data before saving the model.
149142

150143
```php
151144
$this->validationRules = [
152-
'title' => 'string|required|max:255',
153-
'slug' => 'string|required|max:100',
145+
'title' => 'required|max:255',
146+
'slug' => 'required|max:100',
154147
'body' => 'required',
155148
'publish_on' => 'date',
156149
'published' => 'boolean',
157-
'category_id' => 'int|required',
150+
'category_id' => 'required|int',
158151
];
159152
```
160153

@@ -185,7 +178,8 @@ The views are built with bootstrap v.3 and also have css classes to support some
185178
- datepicker class is used in date inputs
186179
- data-table class is used in the index view table
187180

188-
It is also possible to publish the views, so you can change them anyway you need. To publish them, use the following artisan command:
181+
It is also possible to publish the views, so you can change them anyway you need.
182+
To publish them, use the following artisan command:
189183

190184
```
191185
php artisan vendor:publish --provider=Achillesp\CrudForms\CrudFormsServiceProvider --tag=views

tests/CrudForms.php renamed to tests/CrudFormsTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Achillesp\CrudForms\Test\Models\Post;
66

7-
class CrudForms extends TestCase
7+
class CrudFormsTest extends TestCase
88
{
99
public function setUp()
1010
{
@@ -18,7 +18,7 @@ public function it_loads_all_resources_in_the_index()
1818
$response = $this->get('/post');
1919

2020
$response->assertViewHas('entities', $posts)
21-
->assertSee($posts[0]->title);
21+
->assertSee($posts[0]->title);
2222
}
2323

2424
/** @test */
@@ -28,7 +28,7 @@ public function it_shows_a_single_resource()
2828
$response = $this->get('/post/1');
2929

3030
$response->assertViewHas('entity', $post)
31-
->assertSee($post->title);
31+
->assertSee($post->title);
3232
}
3333

3434
/** @test */
@@ -38,8 +38,8 @@ public function it_loads_the_form_for_editing_resource()
3838
$response = $this->get('/post/1/edit');
3939

4040
$response->assertViewHas('entity', $post)
41-
->assertSee('Submit Form')
42-
->assertSee($post->title);
41+
->assertSee('Submit Form')
42+
->assertSee($post->title);
4343
}
4444

4545
/** @test */
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Achillesp\CrudForms\Test\Providers;
4+
5+
use Illuminate\Routing\Router;
6+
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
7+
8+
class RouteServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* This namespace is applied to your controller routes.
12+
*
13+
* In addition, it is set as the URL generator's root namespace.
14+
*
15+
* @var string
16+
*/
17+
protected $namespace = 'Achillesp\CrudForms\Test\Controllers';
18+
19+
/**
20+
* Define the routes for the application.
21+
*
22+
* @param \Illuminate\Routing\Router $router
23+
* @return void
24+
*/
25+
public function map(Router $router)
26+
{
27+
$router->group(['namespace' => $this->namespace], function ($router) {
28+
$router->get('/', function () {
29+
return 'home';
30+
});
31+
32+
$router->group(['middleware' => 'web'], function($router) {
33+
$router->resource('/post', 'PostController');
34+
35+
$router->put('/post/{post}/restore',
36+
[
37+
'as' => 'post.restore',
38+
'uses' => 'PostController@restore']);
39+
});
40+
});
41+
}
42+
}

tests/TestCase.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace Achillesp\CrudForms\Test;
44

5-
use Illuminate\Support\Facades\Route;
5+
use Carbon\Carbon;
66
use Achillesp\CrudForms\Test\Models\Tag;
77
use Achillesp\CrudForms\Test\Models\Post;
88
use Achillesp\CrudForms\Test\Models\Category;
99
use Illuminate\Database\Capsule\Manager as DB;
1010
use Achillesp\CrudForms\CrudFormsServiceProvider;
1111
use Orchestra\Testbench\TestCase as OrchestraTestCase;
12+
use Achillesp\CrudForms\Test\Providers\RouteServiceProvider;
1213

1314
abstract class TestCase extends OrchestraTestCase
1415
{
@@ -36,6 +37,7 @@ protected function getPackageProviders($app)
3637
{
3738
return [
3839
CrudFormsServiceProvider::class,
40+
RouteServiceProvider::class,
3941
];
4042
}
4143

@@ -48,27 +50,10 @@ protected function getPackageProviders($app)
4850
protected function getEnvironmentSetUp($app)
4951
{
5052
$this->app = $app;
51-
$this->setRoutes();
5253
$app['config']->set('app.url', 'http://localhost');
5354
$app['config']->set('app.key', 'base64:WpZ7D2IUkBA+99f8HABIVujw2HqzR6kLGsTpDdV5nao=');
5455
}
5556

56-
/**
57-
* Define the routes used in tests.
58-
*
59-
* @return void
60-
*/
61-
protected function setRoutes()
62-
{
63-
Route::get('/', function () {
64-
return 'home';
65-
});
66-
67-
Route::resource('/post', 'Achillesp\CrudForms\Test\Controllers\PostController')->middleware('web');
68-
Route::put('/post/{post}/restore',
69-
['as' => 'post.restore', 'uses' => 'Achillesp\CrudForms\Test\Controllers\PostController@restore'])->middleware('web');
70-
}
71-
7257
/**
7358
* Setup the test database.
7459
*
@@ -154,7 +139,7 @@ protected function seedTables()
154139
'title' => "post $i",
155140
'slug' => "post-$i",
156141
'body' => "post $i body",
157-
'publish_on' => date_sub(now(), date_interval_create_from_date_string("$i days")),
142+
'publish_on' => date_sub(Carbon::now(), date_interval_create_from_date_string("$i days")),
158143
'published' => rand(0, 1),
159144
'category_id'=> $i,
160145
]);

0 commit comments

Comments
 (0)