This repository has been archived by the owner on Mar 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
212 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
Homestead.json | ||
Homestead.yaml | ||
.env | ||
database.sqlite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
/** | ||
* MIT License | ||
* | ||
* Copyright (c) 2018 Duncan Sterken | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Model Factories | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Here you may define all of your model factories. Model factories give | ||
| you a convenient way to create models for testing and seeding your | ||
| database. Just tell the factory how a default model should look. | ||
| | ||
*/ | ||
|
||
$factory->define(App\Models\StringValue::class, function (Faker\Generator $faker) { | ||
return [ | ||
'user_id' => factory(\App\Models\User::class)->create()->id, | ||
'value' => $faker->unique()->word, | ||
]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
/** | ||
* MIT License | ||
* | ||
* Copyright (c) 2018 Duncan Sterken | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
|
||
use App\Models\StringValue; | ||
use App\Models\User; | ||
use Laravel\Lumen\Testing\DatabaseMigrations; | ||
use Laravel\Lumen\Testing\DatabaseTransactions; | ||
|
||
class StringTest extends TestCase | ||
{ | ||
use DatabaseMigrations, DatabaseTransactions; | ||
|
||
/** | ||
* @var User | ||
*/ | ||
private $user; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->user = factory(User::class)->create(); | ||
} | ||
|
||
public function testUserCanRetrieveStrings() | ||
{ | ||
/** | ||
* @var \Illuminate\Database\Eloquent\Collection $stringsCollection | ||
*/ | ||
$stringsCollection = factory(StringValue::class, 5)->create(['user_id' => $this->user->id]); | ||
|
||
// Other users also have strings | ||
factory(StringValue::class, 3)->create(); | ||
|
||
$strings = $stringsCollection->map(function (StringValue $s) { | ||
return $s->toArray(); | ||
})->toArray(); | ||
|
||
$response = $this->request('GET', 'strings', $this->user)->decodeResponseJson()->strings; | ||
|
||
$userStrings = collect($response)->map(function ($s) { | ||
// Map the values to match the faker return | ||
return [ | ||
'value' => $s->value, | ||
'updated_at' => $s->updated_at, | ||
'created_at' => $s->created_at, | ||
'id' => $s->id, | ||
]; | ||
})->toArray(); | ||
|
||
$this->assertCount(5, $strings); | ||
$this->assertSameSize($strings, $userStrings); | ||
$this->assertSame($strings, $userStrings); | ||
} | ||
|
||
public function testUserCanNotRetrieveStringsWhenNotLoggedIn() | ||
{ | ||
$response = $this->request('GET', 'strings', 'bla', [], [ | ||
'Authorization' => 'Token InvalidToken', | ||
]); | ||
|
||
$response->assertResponseStatus(403); | ||
} | ||
|
||
public function testStringCreationWithValidParameters() | ||
{ | ||
$data = [ | ||
'value' => str_random(), | ||
]; | ||
|
||
$currentStrings = $this->user->strings()->get(); | ||
|
||
$response = $this->request('POST', 'strings', $this->user, $data); | ||
|
||
$newStrings = $this->user->strings()->get(); | ||
|
||
$response->assertResponseStatus(200); | ||
$this->assertCount(0, $currentStrings); | ||
$this->assertCount(1, $newStrings); | ||
} | ||
|
||
public function testStringCreationFailsWhenStringIsTooLarge() | ||
{ | ||
$data = [ | ||
'value' => str_random(51), | ||
]; | ||
|
||
$response = $this->request('POST', 'strings', $this->user, $data); | ||
|
||
$response->assertResponseStatus(422); | ||
} | ||
|
||
public function testUserCanDeleteOwnString() | ||
{ | ||
$string = factory(StringValue::class)->create(['user_id' => $this->user->id]); | ||
|
||
$response = $this->request('DELETE', "strings/{$string->id}", $this->user); | ||
|
||
$response->assertResponseOk(); | ||
} | ||
|
||
public function testUserCanNotDeleteOtherStrings() | ||
{ | ||
$string = factory(StringValue::class)->create(); | ||
|
||
$response = $this->request('DELETE', "strings/{$string->id}", $this->user); | ||
|
||
$response->assertResponseStatus(403); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters