diff --git a/src/Services/ThemeHelper.php b/src/Services/ThemeHelper.php index 0353cfaa..d0a5dbb3 100644 --- a/src/Services/ThemeHelper.php +++ b/src/Services/ThemeHelper.php @@ -208,10 +208,11 @@ public function sectionsWithAppBlock(array $templateMainSections): array $asset = $assetResponse['body']['asset']->toArray(); preg_match('/\{\%\s+schema\s+\%\}([\s\S]*?)\{\%\s+endschema\s+\%\}/m', $asset['value'], $matches); - $schema = json_decode($matches[1], true); - - if ($schema && isset($schema['blocks'])) { - $acceptsAppBlock = in_array('@app', array_column($schema['blocks'], 'type')); + if (!empty($matches) && isset($matches[1])) { + $schema = json_decode($matches[1], true); + if ($schema && isset($schema['blocks'])) { + $acceptsAppBlock = in_array('@app', array_column($schema['blocks'], 'type')); + } } return $acceptsAppBlock ? $file : null; diff --git a/tests/Services/ThemeHelperTest.php b/tests/Services/ThemeHelperTest.php index 55e1e045..618ab9d1 100644 --- a/tests/Services/ThemeHelperTest.php +++ b/tests/Services/ThemeHelperTest.php @@ -127,4 +127,32 @@ public function testSectionsWithAppBlock(): void $this->assertNotEmpty($sectionsWithApp); } + + public function testSectionsWithAppBlockHandlesMissingSchema(): void + { + // Response stubbing + $this->setApiStub(); + ApiStub::stubResponses([ + 'get_themes', + 'get_theme_assets', + 'get_theme_asset_no_schema', + ]); + + // Define config to include a template + $this->app['config']->set( + 'shopify-app.theme_support.templates', + [ + 'asset', + ] + ); + + // Run extraction + $this->helper->extractStoreMainTheme($this->shop->getId()); + $jsonFiles = $this->helper->templateJSONFiles(); + $mainSections = $this->helper->mainSections($jsonFiles); + $sectionsWithApp = $this->helper->sectionsWithAppBlock($mainSections); + + // Assert no sections are returned and no error occurs + $this->assertEmpty($sectionsWithApp); + } } diff --git a/tests/fixtures/get_theme_asset_no_schema.json b/tests/fixtures/get_theme_asset_no_schema.json new file mode 100644 index 00000000..8f93191a --- /dev/null +++ b/tests/fixtures/get_theme_asset_no_schema.json @@ -0,0 +1,6 @@ +{ + "asset": { + "key": "sections/no-schema.liquid", + "value": "" + } +}