123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- <?php
- namespace Tests\Feature;
- use App\Http\Controllers\Controller;
- use App\Models\SystemMedia;
- use App\Models\SystemMediaCategory;
- use Illuminate\Http\UploadedFile;
- use Tests\AdminTestCase;
- use Illuminate\Foundation\Testing\WithFaker;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Tests\Traits\RequestActions;
- class SystemMediaCategoryControllerTest extends AdminTestCase
- {
- use RefreshDatabase;
- use RequestActions;
- protected $resourceName = 'system-media-categories';
- protected function setUp(): void
- {
- parent::setUp();
- $this->login();
- }
- protected function createNestedData()
- {
- SystemMediaCategory::insert([
- [
- 'id' => 1,
- 'parent_id' => 0,
- 'name' => 'level 0-1',
- ],
- [
- 'id' => 2,
- 'parent_id' => 0,
- 'name' => 'level 0-2',
- ],
- [
- 'id' => 3,
- 'parent_id' => 1,
- 'name' => 'level 1-1',
- ],
- [
- 'id' => 4,
- 'parent_id' => 3,
- 'name' => 'level 3-1',
- ],
- ]);
- }
- public function testStoreValidation()
- {
- $this->createNestedData();
- // parent_id exists
- // name max:20
- $res = $this->storeResource([
- 'parent_id' => 111,
- 'name' => str_repeat('a', 21),
- ]);
- $res->assertJsonValidationErrors(['parent_id', 'name']);
- // name 同级 unique
- $res = $this->storeResource([
- 'parent_id' => 3,
- 'name' => 'level 3-1',
- ]);
- $res->assertJsonValidationErrors(['name']);
- // parent_id 0
- // name 不同级重复
- $res = $this->storeResource([
- 'parent_id' => 0,
- 'name' => 'level 3-1',
- ]);
- $res->assertJsonMissingValidationErrors(['parent_id', 'name']);
- }
- public function testStore()
- {
- $res = $this->storeResource([
- 'name' => 'level 0-1',
- ]);
- $res->assertStatus(201);
- $id1 = $this->getLastInsertId('system_media_categories');
- $this->assertDatabaseHas('system_media_categories', [
- 'id' => $id1,
- 'name' => 'level 0-1',
- 'parent_id' => 0,
- ]);
- $res = $this->storeResource([
- 'parent_id' => $id1,
- 'name' => 'level 1-1',
- ]);
- $res->assertStatus(201);
- $id2 = $this->getLastInsertId('system_media_categories');
- $this->assertDatabaseHas('system_media_categories', [
- 'id' => $id2,
- 'parent_id' => $id1,
- 'name' => 'level 1-1',
- ]);
- }
- public function testUpdate()
- {
- $id1 = factory(SystemMediaCategory::class)->create(['name' => 'level 0-1'])->id;
- $id2 = factory(SystemMediaCategory::class)->create(['name' => 'level 0-2'])->id;
- // name 同级 unique
- $res = $this->updateResource($id1, [
- 'name' => 'level 0-2',
- ]);
- $res->assertJsonValidationErrors(['name']);
- $res = $this->updateResource($id1, [
- 'parent_id' => $id2,
- 'name' => 'level 0-2',
- ]);
- $res->assertStatus(201);
- $res = $this->updateResource($id1);
- $res->assertStatus(201);
- $this->assertDatabaseHas('system_media_categories', [
- 'id' => $id1,
- 'parent_id' => $id2,
- 'name' => 'level 0-2',
- ]);
- }
- public function testEdit()
- {
- $id = factory(SystemMediaCategory::class)->create(['name' => 'level 0-1'])->id;
- $res = $this->editResource($id);
- $res->assertStatus(200)
- ->assertJson([
- 'id' => $id,
- 'name' => 'level 0-1',
- 'parent_id' => 0,
- ]);
- }
- public function testDestroy()
- {
- $this->createNestedData();
- factory(SystemMedia::class)->create(['category_id' => 3]);
- $res = $this->destroyResource(1);
- $res->assertStatus(204);
- // 删除所有子孙分类
- $this->assertDatabaseMissing('system_media_categories', ['id' => 1]);
- $this->assertDatabaseMissing('system_media_categories', ['id' => 3]);
- $this->assertDatabaseMissing('system_media_categories', ['id' => 4]);
- // 被删除的分类下的文件的分类 id 设为 0
- $this->assertDatabaseHas('system_media', [
- 'id' => 1,
- 'category_id' => 0,
- ]);
- $this->assertDatabaseHas('system_media_categories', ['id' => 2]);
- }
- public function testIndex()
- {
- $this->createNestedData();
- $res = $this->getResources();
- $res->assertStatus(200)
- ->assertJson([
- [
- 'id' => 1,
- 'parent_id' => 0,
- 'name' => 'level 0-1',
- 'children' => [
- [
- 'id' => 3,
- 'parent_id' => 1,
- 'name' => 'level 1-1',
- 'children' => [
- [
- 'id' => 4,
- 'parent_id' => 3,
- 'name' => 'level 3-1',
- ],
- ],
- ],
- ],
- ],
- [
- 'id' => 2,
- 'parent_id' => 0,
- 'name' => 'level 0-2',
- ],
- ]);
- $res = $this->getResources(['except' => 1]);
- $res->assertStatus(200)
- ->assertJson([
- [
- 'id' => 2,
- 'parent_id' => 0,
- 'name' => 'level 0-2',
- ],
- ]);
- }
- /**
- * @param int $cateId
- * @param array $data
- *
- * @return \Illuminate\Foundation\Testing\TestResponse
- */
- protected function storeSystemMedia($cateId, $data = [])
- {
- return $this->storeResource(
- $data,
- $this->resourceName.'.system-media',
- ['id' => $cateId]
- );
- }
- public function testStoreSystemMedia()
- {
- $categoryId = factory(SystemMediaCategory::class)->create()->id;
- // file required
- $res = $this->storeSystemMedia($categoryId);
- $res->assertJsonValidationErrors(['file']);
- // file file
- $res = $this->storeSystemMedia($categoryId, ['file' => 'not a file']);
- $res->assertJsonValidationErrors(['file']);
- $file = UploadedFile::fake()->image('avatar.jpg', 200, 200);
- $res = $this->storeSystemMedia($categoryId, [
- 'file' => $file,
- Controller::UPLOAD_FOLDER_FIELD => 'tests',
- ]);
- $res->assertStatus(201);
- $filename = md5_file($file).'.jpg';
- $path = Controller::UPLOAD_FOLDER_PREFIX.'/tests/'.$filename;
- $this->assertDatabaseHas('system_media', [
- 'id' => $this->getLastInsertId('system_media'),
- 'category_id' => $categoryId,
- 'filename' => $filename,
- 'size' => $file->getSize(),
- 'ext' => 'jpg',
- 'mime_type' => $file->getMimeType(),
- 'path' => $path,
- ]);
- $this->storage->exists($path);
- $this->storage->delete($path);
- }
- protected function systemMediaIndex($params = [])
- {
- return $this->getResources($params, $this->resourceName.'.system-media');
- }
- public function testSystemMediaIndex()
- {
- factory(SystemMediaCategory::class)
- ->create()
- ->media()
- ->createMany([
- factory(SystemMedia::class)->make([
- 'filename' => 'avatar.jpg',
- 'ext' => 'jpg',
- ])->toArray(),
- factory(SystemMedia::class)->make([
- 'filename' => 'funny.gif',
- 'ext' => 'gif',
- ])->toArray(),
- ]);
- $categoryId1 = $this->getLastInsertId('system_media_categories');
- // 其他分类的图片
- factory(SystemMediaCategory::class)
- ->create()
- ->media()
- ->createMany(factory(SystemMedia::class, 2)->make(['ext' => 'jpg'])->toArray());
- // ext in 筛选
- $res = $this->systemMediaIndex([
- 'id' => $categoryId1,
- 'ext' => ['jpg'],
- ]);
- $res->assertStatus(200)
- ->assertJsonFragment(['ext' => 'jpg'])
- ->assertJsonMissing(['ext' => 'gif']);
- $res = $this->systemMediaIndex([
- 'id' => $categoryId1,
- 'ext' => 'jpg,gif',
- ]);
- $res->assertStatus(200)
- ->assertJsonCount(2, 'data');
- // filename like
- $res = $this->systemMediaIndex([
- 'id' => $categoryId1,
- 'filename' => 'ny',
- ]);
- $res->assertStatus(200)
- ->assertJsonFragment(['filename' => 'funny.gif'])
- ->assertJsonMissing(['filename' => 'avatar.jpg']);
- }
- }
|