TableModelCollectionTest.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. // Copyright 2019 Hackware SpA <human@hackware.cl>
  3. // "Hackware Web Services Core" is released under the MIT License terms.
  4. namespace Hawese\Tests;
  5. use Hawese\Core\TableModelCollection;
  6. use Laravel\Lumen\Testing\DatabaseTransactions;
  7. class TableModelCollectionTest extends TestCase
  8. {
  9. use DatabaseTransactions;
  10. public function setUp(): void
  11. {
  12. parent::setUp();
  13. for ($i = 0; $i < 10; $i++) {
  14. (new DumbTableModel([
  15. 'attr1' => bin2hex(random_bytes(12)),
  16. 'foreign_id' => (new ForeignTableModel())->insert()
  17. ]))->insert();
  18. }
  19. $this->dumbObjs = new TableModelCollection(
  20. DumbTableModel::select()->get(),
  21. DumbTableModel::class
  22. );
  23. }
  24. public function testConstructAndGet()
  25. {
  26. $this->assertInstanceOf(
  27. \Illuminate\Support\Collection::class,
  28. $this->dumbObjs->get()
  29. );
  30. $this->assertInstanceOf(
  31. DumbTableModel::class,
  32. $this->dumbObjs->get()->last()
  33. );
  34. $this->assertInstanceOf(
  35. DumbTableModel::class,
  36. $this->dumbObjs->get()->random()
  37. );
  38. }
  39. public function testAppendForeignObjects()
  40. {
  41. $this->dumbObjs->appendForeignObjects(['foreign']);
  42. $this->assertIsInt(
  43. $this->dumbObjs->get()->random()->foreign->id
  44. );
  45. $this->assertInstanceOf(
  46. ForeignTableModel::class,
  47. $this->dumbObjs->get()->random()->foreign
  48. );
  49. }
  50. }