EntityTest.php 1.6 KB

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