ColumnSchemaAwareTypeIntegrationTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. declare(strict_types=1);
  3. namespace Cake\Test\TestCase\ORM;
  4. use Cake\Database\DriverInterface;
  5. use Cake\Database\TypeFactory;
  6. use Cake\TestSuite\TestCase;
  7. use TestApp\Database\Type\ColumnSchemaAwareType;
  8. class ColumnSchemaAwareTypeIntegrationTest extends TestCase
  9. {
  10. protected array $fixtures = [
  11. //'core.ColumnSchemaAwareTypeValues',
  12. ];
  13. /**
  14. * @var \Cake\Database\TypeInterface|null
  15. */
  16. public $textType;
  17. public function setUp(): void
  18. {
  19. parent::setUp();
  20. $this->textType = TypeFactory::build('text');
  21. TypeFactory::map('text', ColumnSchemaAwareType::class);
  22. // For SQLServer.
  23. TypeFactory::map('nvarchar', ColumnSchemaAwareType::class);
  24. $this->markTestSkipped('This test requires non-auto-fixtures');
  25. }
  26. public function tearDown(): void
  27. {
  28. parent::tearDown();
  29. TypeFactory::set('text', $this->textType);
  30. $map = TypeFactory::getMap();
  31. unset($map['nvarchar']);
  32. TypeFactory::setMap($map);
  33. }
  34. public function testCustomTypesCanBeUsedInFixtures(): void
  35. {
  36. $table = $this->getTableLocator()->get('ColumnSchemaAwareTypeValues');
  37. $expected = [
  38. 'this text has been processed via a custom type',
  39. 'this text also has been processed via a custom type',
  40. ];
  41. $result = $table->find()->orderAsc('id')->all()->extract('val')->toArray();
  42. $this->assertSame($expected, $result);
  43. }
  44. public function testCustomTypeCanProcessColumnInfo(): void
  45. {
  46. $column = $this->getTableLocator()->get('ColumnSchemaAwareTypeValues')->getSchema()->getColumn('val');
  47. $this->assertSame('text', $column['type']);
  48. $this->assertSame(255, $column['length']);
  49. $this->assertSame('Custom schema aware type comment', $column['comment']);
  50. }
  51. public function testCustomTypeReceivesAllColumnDefinitionKeys(): void
  52. {
  53. $table = $this->getTableLocator()->get('ColumnSchemaAwareTypeValues');
  54. $type = $this
  55. ->getMockBuilder(ColumnSchemaAwareType::class)
  56. ->setConstructorArgs(['char'])
  57. ->onlyMethods(['convertColumnDefinition'])
  58. ->getMock();
  59. $type
  60. ->expects($this->once())
  61. ->method('convertColumnDefinition')
  62. ->willReturnCallback(function (array $definition, DriverInterface $driver) {
  63. $this->assertEquals(
  64. [
  65. 'length',
  66. 'precision',
  67. 'scale',
  68. ],
  69. array_keys($definition)
  70. );
  71. return null;
  72. });
  73. TypeFactory::set('text', $type);
  74. TypeFactory::set('nvarchar', $type);
  75. $table->getSchema()->getColumn('val');
  76. }
  77. }