Skip to content

Commit

Permalink
Add unit tests for apache#6266
Browse files Browse the repository at this point in the history
  • Loading branch information
junichi11 committed Jul 28, 2023
1 parent 4f3cda2 commit d94b06b
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public void visit(FieldAccess fieldAccess) {
if (allFields.isEmpty()) {
assert type != null;
FileObject fileObject = type.getFileObject();
BaseDocument document = fileObject != null ? (BaseDocument) GsfUtilities.getADocument(fileObject, false) : null;
BaseDocument document = fileObject != null ? (BaseDocument) GsfUtilities.getADocument(fileObject, true) : null;
if (document != null && fileObject.canWrite()) {
if (type instanceof ClassScope || type instanceof TraitScope) {
fixes.add(new IntroduceFieldFix(document, fieldAccess, type));
Expand Down Expand Up @@ -353,7 +353,7 @@ public void visit(StaticConstantAccess staticConstantAccess) {
if (allConstants.isEmpty() && allEnumCases.isEmpty()) {
assert type != null;
FileObject fileObject = type.getFileObject();
BaseDocument document = fileObject != null ? (BaseDocument) GsfUtilities.getADocument(fileObject, false) : null;
BaseDocument document = fileObject != null ? (BaseDocument) GsfUtilities.getADocument(fileObject, true) : null;
// if fileObject is null, document is null
if (document != null && fileObject.canWrite()) {
fixes.add(new IntroduceClassConstantFix(document, staticConstantAccess, type));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
class TestClass {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

TestClass::CONSTANT;
TestClass::$staticField;
$test = new TestClass();
$test->field;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TestClass::CONSTA^NT;
-------------------
HINT:Introduce Hint
FIX:Create Constant "CONSTANT" in Class "TestClass" (TestClass.php)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

TestClass::CONSTANT;
TestClass::$staticField;
$test = new TestClass();
$test->field;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$test->fie^ld;
------------
HINT:Introduce Hint
FIX:Create Field "public $field;" in Class "TestClass" (TestClass.php)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

TestClass::CONSTANT;
TestClass::$staticField;
$test = new TestClass();
$test->field;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TestClass::$staticFiel^d;
-----------------------
HINT:Introduce Hint
FIX:Create Field "$staticField" in Class "TestClass" (TestClass.php)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

TestClass::CONSTANT;
TestClass::$staticField;
$test = new TestClass();
$test->field;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

TestClass::CONSTANT;
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,36 @@ public void testGH6258_02Fix() throws Exception {
applyHint("testGH6258_02.php", "$test->generatedMetho^d();", "Create Method");
}

public void testGH6266_Const() throws Exception {
checkHints("TestClass::CONSTA^NT;");
}

public void testGH6266_ConstFix() throws Exception {
// no changes because the const is created into the TestClass
// check that NPE doesn't occur
applyHint("TestClass::CONSTAN^T;", "Create Constant");
}

public void testGH6266_StaticField() throws Exception {
checkHints("TestClass::$staticFiel^d;");
}

public void testGH6266_StaticFieldFix() throws Exception {
// no changes because the field is created into the TestClass
// check that NPE doesn't occur
applyHint("TestClass::$staticFiel^d;", "Create Field");
}

public void testGH6266_Field() throws Exception {
checkHints("$test->fie^ld;");
}

public void testGH6266_FieldFix() throws Exception {
// no changes because the field is created into the TestClass
// check that NPE doesn't occur
applyHint("$test->fiel^d;", "Create Field");
}

private void checkHints(String caretLine) throws Exception {
checkHints(new IntroduceSuggestion(), getTestFileName(), caretLine);
}
Expand Down

0 comments on commit d94b06b

Please sign in to comment.