SchemaTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Form;
  17. use Cake\Form\Schema;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Form schema test case.
  21. */
  22. class SchemaTest extends TestCase
  23. {
  24. /**
  25. * Test adding multiple fields.
  26. */
  27. public function testAddingMultipleFields(): void
  28. {
  29. $schema = new Schema();
  30. $schema->addFields([
  31. 'email' => 'string',
  32. 'body' => ['type' => 'string', 'length' => 1000],
  33. ]);
  34. $this->assertEquals(['email', 'body'], $schema->fields());
  35. $this->assertSame('string', $schema->field('email')['type']);
  36. $this->assertSame('string', $schema->field('body')['type']);
  37. }
  38. /**
  39. * test adding fields.
  40. */
  41. public function testAddingFields(): void
  42. {
  43. $schema = new Schema();
  44. $res = $schema->addField('name', ['type' => 'string']);
  45. $this->assertSame($schema, $res, 'Should be chainable');
  46. $this->assertEquals(['name'], $schema->fields());
  47. $res = $schema->field('name');
  48. $expected = ['type' => 'string', 'length' => null, 'precision' => null, 'default' => null];
  49. $this->assertEquals($expected, $res);
  50. $res = $schema->addField('email', 'string');
  51. $this->assertSame($schema, $res, 'Should be chainable');
  52. $this->assertEquals(['name', 'email'], $schema->fields());
  53. $res = $schema->field('email');
  54. $expected = ['type' => 'string', 'length' => null, 'precision' => null, 'default' => null];
  55. $this->assertEquals($expected, $res);
  56. }
  57. /**
  58. * test adding field whitelist attrs
  59. */
  60. public function testAddingFieldsWhitelist(): void
  61. {
  62. $schema = new Schema();
  63. $schema->addField('name', ['derp' => 'derp', 'type' => 'string']);
  64. $expected = ['type' => 'string', 'length' => null, 'precision' => null, 'default' => null];
  65. $this->assertEquals($expected, $schema->field('name'));
  66. }
  67. /**
  68. * Test removing fields.
  69. */
  70. public function testRemovingFields(): void
  71. {
  72. $schema = new Schema();
  73. $schema->addField('name', ['type' => 'string']);
  74. $this->assertEquals(['name'], $schema->fields());
  75. $res = $schema->removeField('name');
  76. $this->assertSame($schema, $res, 'Should be chainable');
  77. $this->assertEquals([], $schema->fields());
  78. $this->assertNull($schema->field('name'));
  79. }
  80. /**
  81. * test fieldType
  82. */
  83. public function testFieldType(): void
  84. {
  85. $schema = new Schema();
  86. $schema->addField('name', 'string')
  87. ->addField('numbery', [
  88. 'type' => 'decimal',
  89. 'required' => true,
  90. ]);
  91. $this->assertSame('string', $schema->fieldType('name'));
  92. $this->assertSame('decimal', $schema->fieldType('numbery'));
  93. $this->assertNull($schema->fieldType('nope'));
  94. }
  95. /**
  96. * test __debugInfo
  97. */
  98. public function testDebugInfo(): void
  99. {
  100. $schema = new Schema();
  101. $schema->addField('name', 'string')
  102. ->addField('numbery', [
  103. 'type' => 'decimal',
  104. 'required' => true,
  105. ]);
  106. $result = $schema->__debugInfo();
  107. $expected = [
  108. '_fields' => [
  109. 'name' => ['type' => 'string', 'length' => null, 'precision' => null, 'default' => null],
  110. 'numbery' => ['type' => 'decimal', 'length' => null, 'precision' => null, 'default' => null],
  111. ],
  112. ];
  113. $this->assertEquals($expected, $result);
  114. }
  115. }