|
| 1 | +# Eloquent PHPUnit |
| 2 | + |
| 3 | +#### Test your Laravel Eloquent model's and database schema |
| 4 | + |
| 5 | +This package was written for a project of mine. It was inspired by the Rails testing framework RSpec and how Rails world tests their models and database. So what can you test? You can test the following in your Laravel Eloquent models: |
| 6 | + |
| 7 | +- Casted attribute array |
| 8 | +- Fillable attribute array |
| 9 | +- Hidden attribute array |
| 10 | +- Dates attribute array |
| 11 | +- Relationship methods |
| 12 | + |
| 13 | +You can also test your database tables such as: |
| 14 | +- Table exists |
| 15 | +- Table column exists |
| 16 | +- Table column type (string, text, date, datetime, boolean, etc.). |
| 17 | +- Column default value |
| 18 | +- Null/Not Null |
| 19 | +- Auto-incremented primary keys. |
| 20 | +- Table indexes |
| 21 | +- Unique indexes |
| 22 | +- Foreign Key relationships |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +The easiest way to install is through composer using the terminal: |
| 27 | +``` |
| 28 | +composer require erikgall/eloquent-phpunit |
| 29 | +``` |
| 30 | + |
| 31 | +## Usage |
| 32 | + |
| 33 | +```php |
| 34 | +Class UserModelTest extends \EGALL\EloquentPHPUnit\EloquentTestCase { |
| 35 | + |
| 36 | + protected $model = 'App\User'; |
| 37 | + |
| 38 | + // If you want to run the DatabaseSeeder class |
| 39 | + protected $seedDatabase = true; |
| 40 | + |
| 41 | + // If you only want to run a specific seeder |
| 42 | + protected $seeders = ['UsersTableSeeder', 'SchoolsTableSeeder']; |
| 43 | + |
| 44 | + // Change the default seeder that calls the rest of your seeders. |
| 45 | + // The default is the default Laravel Seeder named: DatabaseSeeder. |
| 46 | + // Ex. (You have a TestDatabaseSeeder and the default DatabaseSeeder). |
| 47 | + protected $defaultSeeder = 'TestDatabaseSeeder' |
| 48 | + |
| 49 | + /** |
| 50 | + * Test the database table. |
| 51 | + */ |
| 52 | + public function testDatabaseTable() { |
| 53 | + $this->table->column('id')->integer()->increments(); |
| 54 | + $this->table->column('name')->string()->nullable(); |
| 55 | + $this->table->column('email')->string()->notNullable()->unique(); |
| 56 | + $this->table->column('password')->string()->notNullable(); |
| 57 | + $this->table->column('dob')->date()->nullable(); |
| 58 | + $this->table->column('avatar_id')->integer()->foreign('images', 'id', $onUpdate = 'cascade', $onDelete = 'cascade'); |
| 59 | + $this->table->column('is_verified')->boolean()->default(false); |
| 60 | + $this->table->column('is_admin')->boolean()->default(false); |
| 61 | + $this->table->column('verification_sent_at')->dateTime()->nullable(); |
| 62 | + $this->table->column('invite_sent_at')->dateTime()->nullable(); |
| 63 | + $this->table->column('api_token')->string()->index(); |
| 64 | + $this->table->column('remember_token')->string()->nullable(); |
| 65 | + $this->table->hasTimestamps(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Test the model's properties. |
| 70 | + */ |
| 71 | + public function testModelProperties() { |
| 72 | + $this->hasFillable('name', 'email', 'password', 'dob', 'avatar_id') |
| 73 | + ->hasHidden('password', 'remember_token') |
| 74 | + ->hasCasts('is_verified', 'boolean') // or |
| 75 | + ->hasCasts(['is_verified' => 'boolean', 'is_admin' => 'boolean']) |
| 76 | + ->hasDates('verification_sent_at', 'invite_sent_at') |
| 77 | + ->belongsTo(Image::class) // if method name = 'image()' or |
| 78 | + ->belongsTo(Image::class, $customMethod = 'avatar') |
| 79 | + ->hasMany(Profile::class) |
| 80 | + ->morphsTo($method = 'slug', $morphTo = 'sluggable') // or |
| 81 | + // example below assumes the db fields are: 'sluggable_id' and 'sluggable_type' |
| 82 | + ->morphsTo('sluggable'); |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## Contributing |
| 88 | + |
| 89 | +1. Fork it. |
| 90 | +2. Create your branch: `git checkout -b my-new-feature` |
| 91 | +3. Commit your changes: `git commit -am 'Add some feature'` |
| 92 | +4. Push to the branch: `git push origin my-new-feature` |
| 93 | +5. Submit a pull request. |
| 94 | + |
| 95 | +## History |
| 96 | + |
| 97 | +- Initial Release: 8/5/2016 |
| 98 | + |
| 99 | +## Credits |
| 100 | + |
| 101 | +- [Erik Galloway](https://github.com/erikgall) |
| 102 | +- [Laravel Framework](https://laravel.com) |
| 103 | + |
| 104 | +## License |
| 105 | + |
| 106 | +TODO: Write license |
0 commit comments