Skip to content

Commit 379bba6

Browse files
author
Cyril Mizzi
committed
adds scalar unit tests
1 parent 8c23e2c commit 379bba6

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

database/factories/User.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,20 @@
33
use StudioNet\GraphQL\Tests\Entity;
44

55
$factory->define(Entity\User::class, function(Generator $faker) {
6+
// Create faked permissions
7+
$permissions = [
8+
'user.create' => $faker->boolean,
9+
'user.edit' => $faker->boolean,
10+
'user.delete' => $faker->boolean,
11+
'user.view' => $faker->boolean
12+
];
13+
614
return [
7-
'name' => $faker->name,
8-
'email' => $faker->email,
9-
'password' => bcrypt(str_random(10))
15+
'name' => $faker->name,
16+
'email' => $faker->email,
17+
'password' => bcrypt(str_random(10)),
18+
'last_login' => $faker->datetime(),
19+
'is_admin' => $faker->boolean,
20+
'permissions' => $permissions
1021
];
1122
});

database/migrations/2013_07_26_182750_create_users_table.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public function up() {
1818

1919
$table->string('name');
2020
$table->string('email');
21+
$table->timestamp('last_login')->nullable();
22+
$table->json('permissions')->nullable();
23+
$table->boolean('is_admin');
2124
$table->string('password');
2225

2326
$table->timestamps();

tests/Entity/User.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ class User extends Model {
1818
/** @var array $guarded */
1919
protected $guarded = [];
2020

21+
/** @var array $dates */
22+
protected $dates = [
23+
'last_login'
24+
];
25+
26+
/** @var array $casts */
27+
protected $casts = [
28+
'is_admin' => 'boolean',
29+
'permissions' => 'array'
30+
];
31+
2132
/**
2233
* Return related posts
2334
*
@@ -26,4 +37,14 @@ class User extends Model {
2637
public function posts() {
2738
return $this->hasMany(Post::class);
2839
}
40+
41+
/**
42+
* Override name
43+
*
44+
* @param string $value
45+
* @return string
46+
*/
47+
public function getNameAttribute($value) {
48+
return strtoupper($value);
49+
}
2950
}

tests/GraphQLTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,35 @@ public function testMutation() {
112112
]
113113
]);
114114
}
115+
116+
/**
117+
* testTypeResponse
118+
*
119+
* @return void
120+
*/
121+
public function testTypeResponse() {
122+
factory(Entity\User::class, 1)->create();
123+
124+
$graphql = app(GraphQL::class);
125+
$graphql->registerSchema('default', []);
126+
$graphql->registerType('user', Entity\User::class);
127+
128+
$params = ['query' => 'query { user (id: 1) { last_login, is_admin, permissions }}'];
129+
$this->json('POST', '/graphql', $params);
130+
$data = $this->response->getData(true);
131+
132+
$this->assertArrayHasKey('data', $data);
133+
$this->assertArrayHasKey('user', $data['data']);
134+
135+
$data = $data['data']['user'];
136+
$user = Entity\User::find(1);
137+
138+
$this->assertTrue(is_bool($data['is_admin']));
139+
$this->assertTrue(is_array($data['permissions']));
140+
$this->assertTrue(is_int($data['last_login']));
141+
142+
$this->assertSame($user->last_login->timestamp, $data['last_login']);
143+
$this->assertSame($user->permissions, $data['permissions']);
144+
$this->assertSame($user->is_admin, $data['is_admin']);
145+
}
115146
}

0 commit comments

Comments
 (0)