UserGridTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace Dcat\Admin\Tests\Feature;
  3. use Dcat\Admin\Tests\Models\Profile as ProfileModel;
  4. use Dcat\Admin\Tests\Models\User as UserModel;
  5. use Dcat\Admin\Tests\TestCase;
  6. /**
  7. * @group user-grid
  8. */
  9. class UserGridTest extends TestCase
  10. {
  11. public function testIndexPage()
  12. {
  13. $this->visit('admin/tests/users')
  14. ->see('All users')
  15. ->seeInElement('tr th', 'Username')
  16. ->seeInElement('tr th', 'Email')
  17. ->seeInElement('tr th', 'Mobile')
  18. ->seeInElement('tr th', 'Full name')
  19. ->seeInElement('tr th', 'Avatar')
  20. ->seeInElement('tr th', 'Post code')
  21. ->seeInElement('tr th', 'Address')
  22. // ->seeInElement('tr th', 'Position')
  23. ->seeInElement('tr th', 'Color')
  24. ->seeInElement('tr th', '开始时间')
  25. ->seeInElement('tr th', '结束时间')
  26. ->seeInElement('tr th', 'Color')
  27. ->seeInElement('tr th', 'Created at')
  28. ->seeInElement('tr th', 'Updated at');
  29. $action = url('/admin/tests/users');
  30. //$this->seeInElement('a[href="http://localhost:8000/admin/tests/users?_export_=all"]', 'All')
  31. // ->seeInElement('a[href="http://localhost:8000/admin/tests/users/create"]', 'New');
  32. $this->seeElement("form[action='$action'][method=get]")
  33. // ->seeElement("form[action='$action'][method=get] input[name=id]")
  34. ->seeElement("form[action='$action'][method=get] input[name=username]")
  35. ->seeElement("form[action='$action'][method=get] input[name=email]")
  36. ->seeElement("form[action='$action'][method=get] input[name='profile[start_at][start]']")
  37. ->seeElement("form[action='$action'][method=get] input[name='profile[start_at][end]']")
  38. ->seeElement("form[action='$action'][method=get] input[name='profile[end_at][start]']")
  39. ->seeElement("form[action='$action'][method=get] input[name='profile[end_at][end]']");
  40. }
  41. protected function seedsTable($count = 100)
  42. {
  43. factory(\Dcat\Admin\Tests\Models\User::class, $count)
  44. ->create()
  45. ->each(function ($u) {
  46. $u->profile()->save(factory(\Dcat\Admin\Tests\Models\Profile::class)->make());
  47. $u->tags()->saveMany(factory(\Dcat\Admin\Tests\Models\Tag::class, 5)->make());
  48. });
  49. }
  50. public function testGridWithData()
  51. {
  52. $this->seedsTable();
  53. $this->visit('admin/tests/users')
  54. ->see('All users');
  55. $this->assertCount(100, UserModel::all());
  56. $this->assertCount(100, ProfileModel::all());
  57. }
  58. public function testGridPagination()
  59. {
  60. $this->seedsTable(65);
  61. $this->visit('admin/tests/users')
  62. ->see('All users');
  63. $this->visit('admin/tests/users?page=2');
  64. $this->assertCount(20, $this->crawler()->filter('td a i[class*=fa-ellipsis-v]'));
  65. $this->visit('admin/tests/users?page=3');
  66. $this->assertCount(20, $this->crawler()->filter('td a i[class*=fa-ellipsis-v]'));
  67. $this->visit('admin/tests/users?page=4');
  68. $this->assertCount(5, $this->crawler()->filter('td a i[class*=fa-ellipsis-v]'));
  69. $this->click(1)->seePageIs('admin/tests/users?page=1');
  70. $this->assertCount(20, $this->crawler()->filter('td a i[class*=fa-ellipsis-v]'));
  71. }
  72. public function testEqualFilter()
  73. {
  74. $this->seedsTable(50);
  75. $this->visit('admin/tests/users')
  76. ->see('All users');
  77. $this->assertCount(50, UserModel::all());
  78. $this->assertCount(50, ProfileModel::all());
  79. $id = mt_rand(1, 50);
  80. $user = UserModel::find($id);
  81. $this->visit('admin/tests/users?id='.$id)
  82. ->seeInElement('td', $user->username)
  83. ->seeInElement('td', $user->email)
  84. ->seeInElement('td', $user->mobile)
  85. // ->seeElement("img[src='{$user->avatar}']")
  86. ->seeInElement('td', "{$user->profile->first_name} {$user->profile->last_name}")
  87. ->seeInElement('td', $user->postcode)
  88. ->seeInElement('td', $user->address)
  89. // ->seeInElement('td', "{$user->profile->latitude} {$user->profile->longitude}")
  90. ->seeInElement('td', $user->color)
  91. ->seeInElement('td', $user->start_at)
  92. ->seeInElement('td', $user->end_at);
  93. }
  94. public function testLikeFilter()
  95. {
  96. $this->seedsTable(50);
  97. $this->visit('admin/tests/users')
  98. ->see('All users');
  99. $this->assertCount(50, UserModel::all());
  100. $this->assertCount(50, ProfileModel::all());
  101. $users = UserModel::where('username', 'like', '%mi%')->get();
  102. $this->visit('admin/tests/users?username=mi');
  103. $this->assertSame($this->crawler()->filter('td a i[class*=fa-ellipsis-v]')->count(), count($users->toArray()));
  104. foreach ($users as $user) {
  105. $this->seeInElement('td', $user->username);
  106. }
  107. }
  108. public function testFilterRelation()
  109. {
  110. $this->seedsTable(20);
  111. $user = UserModel::with('profile')->find(mt_rand(1, 20));
  112. $this->visit('admin/tests/users?email='.$user->email)
  113. ->seeInElement('td', $user->username)
  114. ->seeInElement('td', $user->email)
  115. ->seeInElement('td', $user->mobile)
  116. // ->seeElement("img[src='{$user->avatar}']")
  117. ->seeInElement('td', "{$user->profile->first_name} {$user->profile->last_name}")
  118. ->seeInElement('td', $user->postcode)
  119. ->seeInElement('td', $user->address)
  120. // ->seeInElement('td', "{$user->profile->latitude} {$user->profile->longitude}")
  121. ->seeInElement('td', $user->color)
  122. ->seeInElement('td', $user->start_at)
  123. ->seeInElement('td', $user->end_at);
  124. }
  125. public function testDisplayCallback()
  126. {
  127. $this->seedsTable(1);
  128. $user = UserModel::with('profile')->find(1);
  129. $this->visit('admin/tests/users')
  130. ->seeInElement('th', 'Column1 not in table')
  131. ->seeInElement('th', 'Column2 not in table')
  132. ->seeInElement('td', "full name:{$user->profile->first_name} {$user->profile->last_name}")
  133. ->seeInElement('td', "{$user->email}#{$user->profile->color}");
  134. }
  135. public function testHasManyRelation()
  136. {
  137. factory(\Dcat\Admin\Tests\Models\User::class, 10)
  138. ->create()
  139. ->each(function ($u) {
  140. $u->profile()->save(factory(\Dcat\Admin\Tests\Models\Profile::class)->make());
  141. $u->tags()->saveMany(factory(\Dcat\Admin\Tests\Models\Tag::class, 5)->make());
  142. });
  143. $this->visit('admin/tests/users')
  144. ->seeElement('td code');
  145. $this->assertCount(50, $this->crawler()->filter('td code'));
  146. }
  147. public function testGridActions()
  148. {
  149. $this->seedsTable(15);
  150. $this->visit('admin/tests/users');
  151. $this->assertCount(15, $this->crawler()->filter('td a i[class*=fa-ellipsis-v]'));
  152. }
  153. public function testGridRows()
  154. {
  155. $this->seedsTable(10);
  156. $this->visit('admin/tests/users')
  157. ->seeInElement('td a[class*=btn]', 'detail');
  158. $this->assertCount(5, $this->crawler()->filter('td a[class*=btn]'));
  159. }
  160. public function testGridPerPage()
  161. {
  162. $this->seedsTable(98);
  163. $this->visit('admin/tests/users')
  164. ->seeElement('select[class*=per-page][name=per-page]')
  165. ->seeInElement('select option', 10)
  166. ->seeInElement('select option[selected]', 20)
  167. ->seeInElement('select option', 30)
  168. ->seeInElement('select option', 50)
  169. ->seeInElement('select option', 100);
  170. $this->assertSame('http://localhost:8000/admin/tests/users?per_page=20', $this->crawler()->filter('select option[selected]')->attr('value'));
  171. $perPage = mt_rand(1, 98);
  172. $this->visit('admin/tests/users?per_page='.$perPage)
  173. ->seeInElement('select option[selected]', $perPage)
  174. ->assertCount($perPage + 1, $this->crawler()->filter('tr'));
  175. }
  176. }