@@ -44,69 +44,42 @@ describe("getComposerPhpVer", () => {
4444 }
4545 } ) ;
4646
47- test ( "returns default value when no composer.json is found" , ( ) => {
47+ test ( "returns null when no composer.json is found" , ( ) => {
4848 // Create a directory with no composer.json
4949 const emptyDir = path . join ( tempDir , "empty-dir" ) ;
5050 fs . mkdirSync ( emptyDir , { recursive : true } ) ;
5151
5252 process . chdir ( emptyDir ) ;
5353
54- const defaultVersion = "7.4" ;
55- expect ( getComposerPhpVer ( defaultVersion ) ) . toBe ( defaultVersion ) ;
54+ expect ( getComposerPhpVer ( ) ) . toBe ( null ) ;
5655 } ) ;
5756
5857 test . each ( [
59- [ ">=7.1.0" , "7.1" ] ,
60- [ "^8.0" , "8.0" ] ,
61- [ "~7.4" , "7.4" ] ,
62- [ ">=5.6.0 <8.0.0" , "5.6" ] ,
63- [ "7.3.*" , "7.3" ]
64- ] ) ( "extracts correct version from %s" , ( versionConstraint , expectedVersion ) => {
58+ { ver :">=7.1.0" , expected : "7.1" } ,
59+ { ver :"^8.0" , expected : "8.0" } ,
60+ { ver :"~7.4" , expected : "7.4" } ,
61+ { ver :">=5.6.0 <8.0.0" , expected : "5.6" } ,
62+ { ver :"7.3.*" , expected : "7.3" } ,
63+ { ver :"7.* || 8.*" , expected : "7.0" }
64+ ] ) ( "extracts correct version from $ver ba changing cwd" , ( { ver, expected} ) => {
6565 const composerContent = JSON . stringify (
6666 {
6767 require : {
68- php : versionConstraint ,
68+ php : ver ,
6969 } ,
7070 } ,
7171 null ,
7272 2
7373 ) ;
7474
75- fs . writeFileSync ( tempComposerPath , composerContent ) ;
76-
7775 process . chdir ( tempDir ) ;
78-
79- // Call getComposerPhpVer to test version extraction
80- const result = getComposerPhpVer ( "default" ) ;
81- expect ( result ) . toBe ( expectedVersion ) ;
82- } ) ;
83-
84- test . each ( [
85- [ ">=7.1.0" , "7.1" ] ,
86- [ "^8.0" , "8.0" ] ,
87- [ "~7.4" , "7.4" ] ,
88- [ ">=5.6.0 <8.0.0" , "5.6" ] ,
89- [ "7.3.*" , "7.3" ]
90- ] ) ( "extracts correct version from %s by changing cwd" , ( versionConstraint , expectedVersion ) => {
91- const composerContent = JSON . stringify (
92- {
93- require : {
94- php : versionConstraint ,
95- } ,
96- } ,
97- null ,
98- 2
99- ) ;
100-
10176 fs . writeFileSync ( tempComposerPath , composerContent ) ;
10277
103- process . chdir ( tempDir ) ;
10478
105- const result = getComposerPhpVer ( "default" ) ;
106- expect ( result ) . toBe ( expectedVersion ) ;
79+ expect ( getComposerPhpVer ( ) ) . toBe ( expected ) ;
10780 } ) ;
10881
109- test ( "returns default when composer.json has no PHP requirement" , ( ) => {
82+ test ( "returns null when composer.json has no PHP requirement" , ( ) => {
11083 const composerContent = JSON . stringify (
11184 {
11285 require : {
@@ -122,12 +95,10 @@ describe("getComposerPhpVer", () => {
12295
12396 process . chdir ( tempDir ) ;
12497
125- const defaultVersion = "8.3" ;
126- const result = getComposerPhpVer ( defaultVersion ) ;
127- expect ( result ) . toBe ( defaultVersion ) ;
98+ expect ( getComposerPhpVer ( ) ) . toBe ( null ) ;
12899 } ) ;
129100
130- test ( "returns default when composer.json has invalid PHP requirement" , ( ) => {
101+ test ( "returns null when composer.json has invalid PHP requirement" , ( ) => {
131102 const composerContent = JSON . stringify (
132103 {
133104 require : {
@@ -142,9 +113,7 @@ describe("getComposerPhpVer", () => {
142113
143114 process . chdir ( tempDir ) ;
144115
145- const defaultVersion = "8.3" ;
146- const result = getComposerPhpVer ( defaultVersion ) ;
147- expect ( result ) . toBe ( defaultVersion ) ;
116+ expect ( getComposerPhpVer ( ) ) . toBe ( null ) ;
148117 } ) ;
149118
150119 test ( "finds composer.json in parent directory when in nested child folder" , ( ) => {
@@ -172,8 +141,7 @@ describe("getComposerPhpVer", () => {
172141
173142 process . chdir ( nestedDir3 ) ;
174143
175- const result = getComposerPhpVer ( "default" ) ;
176- expect ( result ) . toBe ( "8.1" ) ;
144+ expect ( getComposerPhpVer ( ) ) . toBe ( "8.1" ) ;
177145 } ) ;
178146
179147 test ( "finds composer.json in intermediate parent directory" , ( ) => {
@@ -202,7 +170,25 @@ describe("getComposerPhpVer", () => {
202170
203171 process . chdir ( nestedDir3 ) ;
204172
205- const result = getComposerPhpVer ( "default" ) ;
206- expect ( result ) . toBe ( "7.2" ) ;
173+ expect ( getComposerPhpVer ( ) ) . toBe ( "7.2" ) ;
174+ } ) ;
175+
176+ test ( "returns null when composer.json is malformed" , ( ) => {
177+ // Create a malformed JSON file (invalid syntax)
178+ const malformedContent = `{
179+ "name": "test/package",
180+ "require": {
181+ "php": "^7.4"
182+ }, // Invalid trailing comma
183+ "extra": {
184+ "key": "value"
185+ }
186+ }` ;
187+
188+ fs . writeFileSync ( tempComposerPath , malformedContent ) ;
189+
190+ process . chdir ( tempDir ) ;
191+
192+ expect ( getComposerPhpVer ( ) ) . toBe ( null ) ;
207193 } ) ;
208194} ) ;
0 commit comments