EntityTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Entity;
  3. use Cake\ORM\TableRegistry;
  4. use Shim\TestSuite\TestCase;
  5. use Tools\Model\Entity\Entity;
  6. class EntityTest extends TestCase {
  7. /**
  8. * @var array
  9. */
  10. protected $fixtures = [
  11. 'plugin.Tools.ToolsUsers',
  12. 'plugin.Tools.Roles',
  13. ];
  14. /**
  15. * @var \Tools\Model\Table\Table
  16. */
  17. protected $Users;
  18. /**
  19. * @return void
  20. */
  21. public function setUp(): void {
  22. parent::setUp();
  23. $this->Users = TableRegistry::getTableLocator()->get('ToolsUsers');
  24. }
  25. /**
  26. * @return void
  27. */
  28. public function tearDown(): void {
  29. TableRegistry::clear();
  30. parent::tearDown();
  31. }
  32. /**
  33. * @return void
  34. */
  35. public function testEnum() {
  36. $array = [
  37. 1 => 'foo',
  38. 2 => 'bar',
  39. ];
  40. $res = Entity::enum(null, $array);
  41. $this->assertSame($array, $res);
  42. $res = Entity::enum(2, $array);
  43. $this->assertSame('bar', $res);
  44. $res = Entity::enum('2', $array);
  45. $this->assertSame('bar', $res);
  46. $res = Entity::enum(3, $array);
  47. $this->assertNull($res);
  48. }
  49. /**
  50. * @return void
  51. */
  52. public function testEnumPartialOptions() {
  53. $array = [
  54. 1 => 'foo',
  55. 2 => 'bar',
  56. 3 => 'yeah',
  57. ];
  58. $res = Entity::enum([2, 3], $array);
  59. $expected = $array;
  60. unset($expected[1]);
  61. $this->assertSame($expected, $res);
  62. }
  63. /**
  64. * @return void
  65. */
  66. public function testEnumDefaultValue() {
  67. $array = [
  68. 1 => 'foo',
  69. 2 => 'bar',
  70. ];
  71. $res = Entity::enum(null, $array, false);
  72. $this->assertSame($array, $res);
  73. $res = Entity::enum(2, $array, false);
  74. $this->assertSame('bar', $res);
  75. $res = Entity::enum('2', $array, false);
  76. $this->assertSame('bar', $res);
  77. $res = Entity::enum(3, $array, false);
  78. $this->assertFalse($res);
  79. }
  80. }