SchemaTest.php 4.1 KB

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