11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
3+ * Copyright 2018 Adobe All rights reserved.
44 * See COPYING.txt for license details.
55 */
66declare (strict_types=1 );
99
1010use Magento \Framework \App \ResourceConnection ;
1111use Magento \Framework \DB \Adapter \SqlVersionProvider ;
12+ use Magento \Framework \Setup \Declaration \Schema \Declaration \ReaderComposite ;
1213use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
1314use Magento \Framework \Setup \Declaration \Schema \Db \DbSchemaReaderInterface ;
1415use Magento \Framework \Setup \Declaration \Schema \Db \SchemaBuilder ;
@@ -330,6 +331,87 @@ public function testBuildUnknownIndexColumn(array $columns, array $references, a
330331 $ this ->model ->build ($ schema );
331332 }
332333
334+ /**
335+ * This test verifies that the system does not crash or throw unexpected errors when attempting to build
336+ * a schema with missing column definitions.
337+ *
338+ * @return void
339+ * @throws \PHPUnit\Framework\MockObject\Exception
340+ */
341+ public function testBuildHandlesMissingColumnsGracefully ()
342+ {
343+ $ data = [
344+ 'table ' => [
345+ 'test_table ' => [
346+ 'name ' => 'test_table_fail ' ,
347+ 'resource ' => 'default ' ,
348+ 'engine ' => 'innodb ' ,
349+ 'comment ' => 'test table ' ,
350+ 'disabled ' => 'true ' ,
351+ ]
352+ ]
353+ ];
354+
355+ $ this ->shardingMock ->expects (self ::once ())
356+ ->method ('getResources ' )
357+ ->willReturn (['default ' ]);
358+
359+ $ this ->dbSchemaReaderMock ->expects (self ::once ())
360+ ->method ('readTables ' )
361+ ->with ('default ' )
362+ ->willReturn (['test_table ' ]);
363+
364+ $ this ->dbSchemaReaderMock ->expects (self ::once ())
365+ ->method ('readColumns ' )
366+ ->with ('test_table ' )
367+ ->willReturn ([]);
368+
369+ $ this ->dbSchemaReaderMock ->expects (self ::once ())
370+ ->method ('readIndexes ' )
371+ ->with ('test_table ' )
372+ ->willReturn ([]);
373+
374+ $ this ->dbSchemaReaderMock ->expects (self ::once ())
375+ ->method ('readReferences ' )
376+ ->willReturn ([]);
377+
378+ $ this ->dbSchemaReaderMock ->expects (self ::once ())
379+ ->method ('readConstraints ' )
380+ ->with ('test_table ' )
381+ ->willReturn ([]);
382+
383+ $ this ->dbSchemaReaderMock ->expects (self ::once ())
384+ ->method ('getTableOptions ' )
385+ ->with ('test_table ' )
386+ ->willReturn ([
387+ 'engine ' => 'innodb ' ,
388+ 'comment ' => '' ,
389+ 'charset ' => 'utf-8 ' ,
390+ 'collation ' => 'utf-8 '
391+ ]);
392+
393+ $ this ->elementFactoryMock ->expects ($ this ->any ())
394+ ->method ('create ' )
395+ ->willReturn ($ this ->createMock (Table::class));
396+
397+ $ readerCompositeMock = $ this ->getMockBuilder (ReaderComposite::class)
398+ ->disableOriginalConstructor ()
399+ ->getMock ();
400+ $ readerCompositeMock ->expects ($ this ->once ())
401+ ->method ('read ' )
402+ ->willReturn ($ data );
403+
404+ $ schemaBuilder = new SchemaBuilder (
405+ $ this ->elementFactoryMock ,
406+ $ this ->dbSchemaReaderMock ,
407+ $ this ->shardingMock ,
408+ $ readerCompositeMock
409+ );
410+
411+ $ schemaBuilder ->build ($ this ->createMock (Schema::class));
412+ $ this ->assertTrue (true , 'System did not crash when columns were missing. ' );
413+ }
414+
333415 /**
334416 * Prepare mocks for test.
335417 *
@@ -338,6 +420,8 @@ public function testBuildUnknownIndexColumn(array $columns, array $references, a
338420 * @param array $constraints
339421 * @param array $indexes
340422 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
423+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
424+ * @SuppressWarnings(PHPMD.NPathComplexity)
341425 */
342426 private function prepareSchemaMocks (array $ columns , array $ references , array $ constraints , array $ indexes )
343427 {
0 commit comments