|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | +package com.magento.idea.magento2plugin.reference; |
| 6 | + |
| 7 | +import com.intellij.psi.PsiElement; |
| 8 | +import com.intellij.psi.PsiFile; |
| 9 | +import com.intellij.psi.PsiReference; |
| 10 | +import com.intellij.psi.xml.XmlAttributeValue; |
| 11 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 12 | +import com.magento.idea.magento2plugin.inspections.BaseInspectionsTestCase; |
| 13 | +import com.magento.idea.magento2plugin.magento.packages.File; |
| 14 | + |
| 15 | +abstract public class BaseReferenceTestCase extends BaseInspectionsTestCase { |
| 16 | + |
| 17 | + private static final String testDataFolderPath = "testData" + File.separator + "reference" + File.separator; |
| 18 | + |
| 19 | + @Override |
| 20 | + protected void setUp() throws Exception { |
| 21 | + super.setUp(); |
| 22 | + myFixture.setTestDataPath(testDataFolderPath); |
| 23 | + } |
| 24 | + |
| 25 | + protected void assertHasReferenceToXmlAttributeValue(String reference) { |
| 26 | + PsiElement element = getElementFromCaret(); |
| 27 | + assertEquals(reference, ((XmlAttributeValue) element.getReferences()[0].resolve()).getValue()); |
| 28 | + } |
| 29 | + |
| 30 | + protected void assertHasReferenceToFile(String reference) { |
| 31 | + String referenceNotFound = "Failed that element contains reference to the file `%s`"; |
| 32 | + |
| 33 | + PsiElement element = getElementFromCaret(); |
| 34 | + for (PsiReference psiReference: element.getReferences()) { |
| 35 | + PsiElement resolved = psiReference.resolve(); |
| 36 | + if (!(resolved instanceof PsiFile)) { |
| 37 | + continue; |
| 38 | + } |
| 39 | + if (((PsiFile) resolved).getVirtualFile().getPath().endsWith(reference)) { |
| 40 | + return; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + fail(String.format(referenceNotFound, reference)); |
| 45 | + } |
| 46 | + |
| 47 | + protected void assertHasReferencePhpClass(String phpClassFqn) { |
| 48 | + PsiElement element = getElementFromCaret(); |
| 49 | + PsiReference[] references = element.getReferences(); |
| 50 | + assertEquals( |
| 51 | + phpClassFqn, |
| 52 | + ((PhpClass) references[references.length -1].resolve()) |
| 53 | + .getPresentableFQN() |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + protected void assertEmptyReference() { |
| 58 | + PsiElement element = getElementFromCaret(); |
| 59 | + assertEmpty(element.getReferences()); |
| 60 | + } |
| 61 | + |
| 62 | + private PsiElement getElementFromCaret() { |
| 63 | + return myFixture.getFile().findElementAt(myFixture.getCaretOffset()).getParent(); |
| 64 | + } |
| 65 | +} |
0 commit comments