SchemaTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * Test adding multiple fields.
  24. *
  25. * @return void
  26. */
  27. public function testAddingMultipleFields() {
  28. $schema = new Schema();
  29. $schema->addFields([
  30. 'email' => 'string',
  31. 'body' => ['type' => 'string', 'length' => 1000]
  32. ]);
  33. $this->assertEquals(['email', 'body'], $schema->fields());
  34. $this->assertEquals('string', $schema->field('email')['type']);
  35. $this->assertEquals('string', $schema->field('body')['type']);
  36. }
  37. /**
  38. * test adding fields.
  39. *
  40. * @return void
  41. */
  42. public function testAddingFields() {
  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, 'required' => false];
  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, 'required' => false];
  55. $this->assertEquals($expected, $res);
  56. }
  57. /**
  58. * test adding field whitelist attrs
  59. *
  60. * @return void
  61. */
  62. public function testAddingFieldsWhitelist() {
  63. $schema = new Schema();
  64. $schema->addField('name', ['derp' => 'derp', 'type' => 'string']);
  65. $expected = ['type' => 'string', 'length' => null, 'required' => false];
  66. $this->assertEquals($expected, $schema->field('name'));
  67. }
  68. /**
  69. * Test removing fields.
  70. *
  71. * @return void
  72. */
  73. public function testRemovingFields() {
  74. $schema = new Schema();
  75. $schema->addField('name', ['type' => 'string']);
  76. $this->assertEquals(['name'], $schema->fields());
  77. $res = $schema->removeField('name');
  78. $this->assertSame($schema, $res, 'Should be chainable');
  79. $this->assertEquals([], $schema->fields());
  80. $this->assertNull($schema->field('name'));
  81. }
  82. /**
  83. * test isRequired
  84. *
  85. * @return void
  86. */
  87. public function testIsRequired() {
  88. $schema = new Schema();
  89. $schema->addField('name', ['type' => 'string'])
  90. ->addField('email', [
  91. 'type' => 'string',
  92. 'required' => true
  93. ]);
  94. $this->assertFalse($schema->isRequired('name'));
  95. $this->assertTrue($schema->isRequired('email'));
  96. $this->assertFalse($schema->isRequired('nope'));
  97. }
  98. /**
  99. * test fieldType
  100. *
  101. * @return void
  102. */
  103. public function testFieldType() {
  104. $schema = new Schema();
  105. $schema->addField('name', 'string')
  106. ->addField('numbery', [
  107. 'type' => 'decimal',
  108. 'required' => true
  109. ]);
  110. $this->assertEquals('string', $schema->fieldType('name'));
  111. $this->assertEquals('decimal', $schema->fieldType('numbery'));
  112. $this->assertNull($schema->fieldType('nope'));
  113. }
  114. }