blessing-skin-server/tests/ClosetControllerTest.php

311 lines
10 KiB
PHP
Raw Normal View History

2017-11-04 17:18:01 +08:00
<?php
2018-08-17 15:25:08 +08:00
namespace Tests;
2017-11-04 17:18:01 +08:00
use App\Models\User;
use App\Models\Closet;
use App\Models\Texture;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ClosetControllerTest extends TestCase
{
use DatabaseTransactions;
/**
* @var User
*/
private $user;
2019-02-27 23:44:50 +08:00
protected function setUp(): void
2017-11-04 17:18:01 +08:00
{
parent::setUp();
$this->user = factory(User::class)->create();
2019-02-27 23:44:50 +08:00
$this->actAs($this->user);
2017-11-04 17:18:01 +08:00
}
public function testIndex()
{
2018-07-13 15:51:17 +08:00
$this->get('/user/closet')->assertViewHas('user');
2017-11-04 17:18:01 +08:00
}
public function testGetClosetData()
{
2017-11-06 11:07:24 +08:00
$textures = factory(Texture::class, 10)->create();
2017-11-04 17:18:01 +08:00
$closet = new Closet($this->user->uid);
$textures->each(function ($texture) use ($closet) {
$closet->add($texture->tid, $texture->name);
});
$closet->save();
// Use default query parameters
2018-07-13 15:51:17 +08:00
$this->getJson('/user/closet-data')
->assertJsonStructure([
2017-11-05 16:54:01 +08:00
'category',
'total_pages',
'items' => [['tid', 'name', 'type', 'add_at']]
2017-11-04 17:18:01 +08:00
]);
2017-11-06 11:07:24 +08:00
// Responsive
2018-07-13 15:51:17 +08:00
$result = $this->json('get', '/user/closet-data?perPage=0')->json();
$this->assertCount(6, $result['items']);
$result = $this->json('get', '/user/closet-data?perPage=8')->json();
$this->assertCount(8, $result['items']);
$result = $this->json('get', '/user/closet-data?perPage=8&page=2')->json();
$this->assertCount(2, $result['items']);
2017-11-06 11:07:24 +08:00
2017-11-04 17:18:01 +08:00
// Get capes
$cape = factory(Texture::class, 'cape')->create();
2017-11-05 16:54:01 +08:00
$closet->add($cape->tid, 'custom_name');
2017-11-04 17:18:01 +08:00
$closet->save();
2018-07-13 15:51:17 +08:00
$this->getJson('/user/closet-data?category=cape')
->assertJson([
2017-11-04 17:18:01 +08:00
'category' => 'cape',
'total_pages' => 1,
2017-11-05 16:54:01 +08:00
'items' => [[
'tid' => $cape->tid,
'name' => 'custom_name',
'type' => 'cape',
'add_at' => $closet->get($cape->tid)['add_at']
]]
2017-11-04 17:18:01 +08:00
]);
// Search by keyword
$random = $textures->random();
2018-07-13 15:51:17 +08:00
$this->getJson('/user/closet-data?q='.$random->name)
->assertJson([
2017-11-04 17:18:01 +08:00
'category' => 'skin',
'total_pages' => 1,
2017-11-05 16:54:01 +08:00
'items' => [[
'tid' => $random->tid,
'name' => $random->name,
'type' => $random->type,
'add_at' => $closet->get($random->tid)['add_at']
]]
2017-11-04 17:18:01 +08:00
]);
}
public function testAdd()
{
$texture = factory(Texture::class)->create();
$name = 'my';
option(['score_per_closet_item' => 10]);
// Missing `tid` field
2018-07-13 15:51:17 +08:00
$this->postJson('/user/closet/add', [], ['X-Requested-With' => 'XMLHttpRequest'])
->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'tid'])
]);
// `tid` is not a integer
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/add',
['tid' => 'string'],
['X-Requested-With' => 'XMLHttpRequest']
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.integer', ['attribute' => 'tid'])
]);
// Missing `name` field
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/add',
['tid' => 0],
['X-Requested-With' => 'XMLHttpRequest']
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
2018-08-14 01:00:02 +08:00
'msg' => trans('validation.required', ['attribute' => 'name'])
2017-11-04 17:18:01 +08:00
]);
// `name` field has special characters
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/add',
['tid' => 0, 'name' => '\\'],
['X-Requested-With' => 'XMLHttpRequest']
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
2018-08-14 01:00:02 +08:00
'msg' => trans('validation.no_special_chars', ['attribute' => 'name'])
2017-11-04 17:18:01 +08:00
]);
// The user doesn't have enough score to add a texture
$this->user->setScore(0);
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/add',
['tid' => $texture->tid, 'name' => $name]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 7,
'msg' => trans('user.closet.add.lack-score')
]);
// Add a not-existed texture
$this->user->setScore(100);
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/add',
['tid' => -1, 'name' => 'my']
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('user.closet.add.not-found')
]);
// Add a texture successfully
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/add',
['tid' => $texture->tid, 'name' => $name]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 0,
'msg' => trans('user.closet.add.success', ['name' => $name])
]);
$this->assertEquals($texture->likes + 1, Texture::find($texture->tid)->likes);
$this->user = User::find($this->user->uid);
$this->assertEquals(90, $this->user->score);
$closet = new Closet($this->user->uid);
$this->assertTrue($closet->has($texture->tid));
// If the texture is duplicated, should be warned
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/add',
['tid' => $texture->tid, 'name' => $name]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('user.closet.add.repeated')
]);
}
public function testRename()
{
$texture = factory(Texture::class)->create();
$name = 'new';
// Missing `tid` field
2018-07-13 15:51:17 +08:00
$this->postJson('/user/closet/rename', [], ['X-Requested-With' => 'XMLHttpRequest'])
->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'tid'])
]);
// `tid` is not a integer
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/rename',
['tid' => 'string'],
['X-Requested-With' => 'XMLHttpRequest']
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.integer', ['attribute' => 'tid'])
]);
// Missing `new_name` field
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/rename',
['tid' => 0],
['X-Requested-With' => 'XMLHttpRequest']
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'new name'])
]);
// `new_name` field has special characters
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/rename',
['tid' => 0, 'new_name' => '\\'],
['X-Requested-With' => 'XMLHttpRequest']
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.no_special_chars', ['attribute' => 'new name'])
]);
// Rename a not-existed texture
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/rename',
['tid' => -1, 'new_name' => $name]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('user.closet.remove.non-existent')
]);
// Rename a closet item successfully
$closet = new Closet($this->user->uid);
$closet->add($texture->tid, 'name');
$closet->save();
$closet = new Closet($this->user->uid);
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/rename',
['tid' => $texture->tid, 'new_name' => $name]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 0,
'msg' => trans('user.closet.rename.success', ['name' => 'new'])
]);
$closet->save();
$closet = new Closet($this->user->uid);
2017-11-05 16:54:01 +08:00
$this->assertFalse(collect($closet->getItems())->where('name', 'new')->isEmpty());
2017-11-04 17:18:01 +08:00
}
public function testRemove()
{
$texture = factory(Texture::class)->create();
// Missing `tid` field
2018-07-13 15:51:17 +08:00
$this->postJson('/user/closet/remove', [], ['X-Requested-With' => 'XMLHttpRequest'])
->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'tid'])
]);
// `tid` is not a integer
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/remove',
['tid' => 'string'],
['X-Requested-With' => 'XMLHttpRequest']
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.integer', ['attribute' => 'tid'])
]);
// Rename a not-existed texture
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/remove',
['tid' => -1]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('user.closet.remove.non-existent')
]);
// Should return score if `return_score` is true
$closet = new Closet($this->user->uid);
$closet->add($texture->tid, 'name');
$closet->save();
$score = $this->user->score;
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/remove',
['tid' => $texture->tid]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 0,
'msg' => trans('user.closet.remove.success')
]);
$closet = new Closet($this->user->uid);
$this->assertEquals($texture->likes - 1, Texture::find($texture->tid)->likes);
$this->assertEquals($score + option('score_per_closet_item'), $this->user->score);
$this->assertFalse($closet->has($texture->tid));
$texture = Texture::find($texture->tid);
// Should not return score if `return_score` is false
option(['return_score' => false]);
$closet = new Closet($this->user->uid);
$closet->add($texture->tid, 'name');
$closet->save();
$score = $this->user->score;
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/remove',
['tid' => $texture->tid]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 0,
'msg' => trans('user.closet.remove.success')
]);
$closet = new Closet($this->user->uid);
$this->assertEquals($texture->likes - 1, Texture::find($texture->tid)->likes);
$this->assertEquals($score, $this->user->score);
$this->assertFalse($closet->has($texture->tid));
}
}