Skip to content

Commit 01aeb6b

Browse files
committed
Test for user field on url
1 parent 2e56196 commit 01aeb6b

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

tests/TestCase.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests;
44

55
use Gallib\ShortUrl\Parsers\UrlParser;
6+
use Illuminate\Database\Schema\Blueprint;
67
use Orchestra\Testbench\TestCase as OrchestraTestCase;
78

89
abstract class TestCase extends OrchestraTestCase
@@ -13,6 +14,8 @@ abstract class TestCase extends OrchestraTestCase
1314
protected function setUp(): void
1415
{
1516
parent::setUp();
17+
18+
$this->setUpDatabase($this->app);
1619
}
1720

1821
/**
@@ -35,6 +38,20 @@ protected function getEnvironmentSetUp($app)
3538
\ShortUrl::routes();
3639
}
3740

41+
/**
42+
* Set up the database.
43+
*
44+
* @param \Illuminate\Foundation\Application $app
45+
*/
46+
protected function setUpDatabase($app)
47+
{
48+
$app['db']->connection()->getSchemaBuilder()->create('users', function (Blueprint $table) {
49+
$table->increments('id');
50+
$table->string('name');
51+
$table->timestamps();
52+
});
53+
}
54+
3855
/**
3956
* Get package providers. At a minimum this is the package being tested, but also
4057
* would include packages upon which our package depends, e.g. Cartalyst/Sentry
@@ -124,4 +141,19 @@ public function createUrl(array $parameters = [])
124141

125142
return $this->postJson(route('shorturl.url.store'), $parameters)->json();
126143
}
144+
145+
/**
146+
* Create a user.
147+
*
148+
* @param array $parameters
149+
* @return User
150+
*/
151+
public function createUser(array $parameters = [])
152+
{
153+
$provider = config('auth.guards.api.provider');
154+
$model = config("auth.providers.{$provider}.model");
155+
$parameters = array_merge(['name' => 'Thibault Timeo'], $parameters);
156+
157+
return $model::create(['name' => 'Test']);
158+
}
127159
}

tests/UpdateUrlTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,19 @@ public function an_url_could_be_blacklisted_on_update()
6565

6666
$this->assertArrayHasKey('url', $response['errors']);
6767
}
68+
69+
/** @test */
70+
public function a_user_id_could_be_updated_on_update()
71+
{
72+
$url = $this->createUrl();
73+
74+
$user = $this->createUser();
75+
76+
$this->actingAs($user);
77+
78+
$response = $this->putJson(route('shorturl.url.update', ['id' => $url['id']]), $url)->json();
79+
80+
$this->assertNotNull($response['user_id']);
81+
$this->assertEquals($response['user_id'], $user->id);
82+
}
6883
}

tests/UrlTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,25 @@ public function an_expiration_date_must_be_in_the_future()
181181

182182
$this->assertArrayHasKey('expires_at', $response['errors']);
183183
}
184+
185+
/** @test */
186+
public function user_field_could_be_null()
187+
{
188+
$response = $this->createUrl();
189+
190+
$this->assertNull($response['user_id']);
191+
}
192+
193+
/** @test */
194+
public function user_field_is_set_when_a_user_is_logged_in()
195+
{
196+
$user = $this->createUser();
197+
198+
$this->actingAs($user);
199+
200+
$response = $this->createUrl();
201+
202+
$this->assertNotNull($response['user_id']);
203+
$this->assertEquals($response['user_id'], $user->id);
204+
}
184205
}

0 commit comments

Comments
 (0)