You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wx.sstbc.com/tests/Feature/QccEnterpriseStudentFilterT...

62 lines
4.1 KiB

2 days ago
<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Http\Controllers\Admin\QccEnterpriseAccountController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Http\Request;
class QccEnterpriseStudentFilterTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
config(['database.default' => 'sqlite', 'database.connections.sqlite.database' => ':memory:', 'audit.enabled' => false]);
DB::purge('sqlite');
Schema::create('companies', function (Blueprint $t) { $t->id(); $t->string('company_name'); $t->string('credit_code'); $t->timestamps(); $t->softDeletes(); });
Schema::create('company_qcc_accounts', function (Blueprint $t) { $t->id(); $t->integer('company_id'); $t->string('status'); });
Schema::create('users', function (Blueprint $t) { $t->id(); $t->integer('company_id'); $t->string('name'); $t->string('username'); $t->string('no')->nullable(); $t->string('company_position')->nullable(); $t->integer('is_schoolmate'); $t->softDeletes(); });
Schema::create('courses', function (Blueprint $t) { $t->id(); $t->string('name'); $t->integer('type'); $t->date('start_date')->nullable(); $t->softDeletes(); });
Schema::create('course_signs', function (Blueprint $t) { $t->id(); $t->integer('course_id'); $t->integer('user_id'); $t->integer('status'); $t->softDeletes(); });
foreach ([1, 2, 3] as $id) {
DB::table('companies')->insert(['id' => $id, 'company_name' => '企业'.$id, 'credit_code' => 'credit'.$id]);
DB::table('company_qcc_accounts')->insert(['company_id' => $id, 'status' => 'occupied']);
}
DB::table('users')->insert([
['id' => 1, 'company_id' => 1, 'name' => '张三', 'username' => 'zhang', 'is_schoolmate' => 1],
['id' => 2, 'company_id' => 1, 'name' => '李四', 'username' => 'li', 'is_schoolmate' => 0],
['id' => 3, 'company_id' => 2, 'name' => '王五', 'username' => 'wang', 'is_schoolmate' => 0],
]);
DB::table('courses')->insert([['id' => 1, 'name' => '管理课程', 'type' => 1], ['id' => 2, 'name' => '金融课程', 'type' => 2]]);
DB::table('course_signs')->insert([
['user_id' => 1, 'course_id' => 1, 'status' => 1], ['user_id' => 2, 'course_id' => 2, 'status' => 1],
['user_id' => 1, 'course_id' => 2, 'status' => 0], ['user_id' => 3, 'course_id' => 2, 'status' => 0],
]);
}
private function query(array $params, string $method = 'candidates'): array
{
return (new QccEnterpriseAccountController())->$method(Request::create('/', 'GET', $params))->getData(true);
}
public function test_unfiltered_companies_remain_and_only_approved_students_are_shown(): void
{
$result = $this->query([]);
$this->assertEquals(3, $result['total']);
$company = collect($result['data'])->firstWhere('id', 1);
$this->assertCount(2, $company['users']);
$this->assertCount(1, $company['users'][0]['course_signs']);
$this->assertEquals('管理课程', $company['users'][0]['course_signs'][0]['course']['name']);
$this->assertEquals([], collect($result['data'])->firstWhere('id', 2)['users']);
}
public function test_combined_filters_match_same_student_and_same_approved_course(): void
{
foreach (['candidates', 'index'] as $method) {
$this->assertEquals(1, $this->query(['user_name' => '张', 'is_schoolmate' => '1', 'course_type_id' => 1, 'course_name' => '管理'], $method)['total']);
$this->assertEquals(0, $this->query(['user_name' => '张', 'course_name' => '金融'], $method)['total']);
$this->assertEquals(0, $this->query(['course_type_id' => 1, 'course_name' => '金融'], $method)['total']);
$this->assertEquals(1, $this->query(['is_schoolmate' => '0'], $method)['total']);
$this->assertEquals(0, $this->query(['user_name' => '王'], $method)['total']);
}
$this->assertCount(2, $this->query(['keyword' => 'credit1'], 'index')['data'][0]['company']['users']);
}
}