BaseModelTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Models\Model;
  4. use Illuminate\Http\Request;
  5. use Tests\TestCase;
  6. use Illuminate\Foundation\Testing\WithFaker;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. class BaseModelTest extends TestCase
  9. {
  10. public function testGetPerPage()
  11. {
  12. $model = app(Model::class);
  13. $this->mockGetPerPage('4');
  14. $this->assertEquals(4, $model->getPerPage());
  15. // 不规范测试
  16. foreach (['4.4', 'not number', '-1', '0'] as $page) {
  17. $this->mockGetPerPage($page);
  18. $this->assertEquals(15, $model->getPerPage());
  19. }
  20. // 大于最大每页数测试
  21. $model = new class() extends Model
  22. {
  23. public function getMaxPerPage()
  24. {
  25. return $this->maxPerPage;
  26. }
  27. };
  28. $maxPerPage = $model->getMaxPerPage();
  29. $this->mockGetPerPage((string) ($maxPerPage + 1));
  30. $this->assertEquals($maxPerPage, $model->getPerPage());
  31. }
  32. /**
  33. * 伪造请求的 get 返回
  34. *
  35. * @param $page
  36. */
  37. protected function mockGetPerPage($page)
  38. {
  39. $this->setUp();
  40. $request = new class($page) extends Request
  41. {
  42. public $mockPage;
  43. public function __construct($page)
  44. {
  45. parent::__construct();
  46. $this->mockPage = $page;
  47. }
  48. public function get($key, $default = null)
  49. {
  50. return $this->mockPage;
  51. }
  52. };
  53. $this->instance('request', $request);
  54. }
  55. }