Skip to content

Commit

Permalink
API phpunit9 support
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Oct 27, 2021
1 parent ba2e931 commit 9ee6858
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 92 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
}
],
"require": {
"php": ">=5.6",
"sminnee/phpunit": "^5.7",
"php": ">=7.3",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3",
"behat/behat": "^3.2",
"behat/mink": "^1.7",
"behat/mink-extension": "^2.1",
"silverstripe/mink-facebook-web-driver": "^1",
"symfony/dom-crawler": "^3 || ^4",
"silverstripe/testsession": "^2.2",
"silverstripe/framework": "^4",
"silverstripe/framework": "^4.10",
"symfony/finder": "^3.2 || ^4"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ not exactly one record found in the relation, and hence fail that step for Behat
public function thereShouldBeAnAbuseReportForWithReason($id, $reason)
{
$page = $this->fixtureFactory->get('Page', $id);
assertEquals(1, $page->PageAbuseReports()->filter('Reason', $reason)->Count());
Assert::assertEquals(1, $page->PageAbuseReports()->filter('Reason', $reason)->Count());
}
```

Expand Down
8 changes: 5 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>tests/php</directory>
</testsuite>
<testsuites>
<testsuite name="Default">
<directory>tests/php</directory>
</testsuite>
</testsuites>
</phpunit>
63 changes: 32 additions & 31 deletions src/Context/BasicContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Facebook\WebDriver\WebDriverAlert;
use Facebook\WebDriver\WebDriverExpectedCondition;
use InvalidArgumentException;
use PHPUnit\Framework\Assert;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Filesystem;
use SilverStripe\BehatExtension\Utility\StepHelper;
Expand Down Expand Up @@ -366,7 +367,7 @@ public function takeScreenshot(StepScope $event)
public function stepPageCantBeFound()
{
$page = $this->getSession()->getPage();
assertTrue(
Assert::assertTrue(
// Content from ErrorPage default record
$page->hasContent('Page not found')
// Generic ModelAsController message
Expand Down Expand Up @@ -425,9 +426,9 @@ public function iShouldSeeAButton($negative, $text)
{
$button = $this->findNamedButton($text);
if (trim($negative)) {
assertNull($button, sprintf('%s button found', $text));
Assert::assertNull($button, sprintf('%s button found', $text));
} else {
assertNotNull($button, sprintf('%s button not found', $text));
Assert::assertNotNull($button, sprintf('%s button not found', $text));
}
}

Expand All @@ -438,7 +439,7 @@ public function iShouldSeeAButton($negative, $text)
public function stepIPressTheButton($text)
{
$button = $this->findNamedButton($text);
assertNotNull($button, "{$text} button not found");
Assert::assertNotNull($button, "{$text} button not found");
$button->click();
}

Expand All @@ -456,7 +457,7 @@ public function stepIPressTheButtons($text)
}
}

assertNotNull($button, "{$text} button not found");
Assert::assertNotNull($button, "{$text} button not found");
$button->click();
}

Expand Down Expand Up @@ -495,7 +496,7 @@ public function iClickOnTheElement($selector)
{
$page = $this->getMainContext()->getSession()->getPage();
$element = $page->find('css', $selector);
assertNotNull($element, sprintf('Element %s not found', $selector));
Assert::assertNotNull($element, sprintf('Element %s not found', $selector));
$element->click();
}

Expand Down Expand Up @@ -525,9 +526,9 @@ public function iClickInTheElement($clickType, $text, $selector)
);
$page = $this->getSession()->getPage();
$parentElement = $page->find('css', $selector);
assertNotNull($parentElement, sprintf('"%s" element not found', $selector));
Assert::assertNotNull($parentElement, sprintf('"%s" element not found', $selector));
$element = $parentElement->find('xpath', sprintf('//*[count(*)=0 and contains(.,"%s")]', $text));
assertNotNull($element, sprintf('"%s" not found', $text));
Assert::assertNotNull($element, sprintf('"%s" not found', $text));
$clickTypeFn = $clickTypeMap[$clickType];
$element->$clickTypeFn();
}
Expand Down Expand Up @@ -569,7 +570,7 @@ public function iClickInTheElementDismissingTheDialog($clickType, $text, $select
public function iSeeTheDialogText($expected)
{
$text = $this->getExpectedAlert()->getText();
assertContains($expected, $text);
Assert::assertStringContainsString($expected, $text);
}

/**
Expand Down Expand Up @@ -841,13 +842,13 @@ public function stepFieldShouldBeDisabled($name, $type, $negate)
));
}

assertNotNull($element, sprintf("Element '%s' not found", $name));
Assert::assertNotNull($element, sprintf("Element '%s' not found", $name));

$disabledAttribute = $element->getAttribute('disabled');
if (trim($negate)) {
assertNull($disabledAttribute, sprintf("Failed asserting element '%s' is not disabled", $name));
Assert::assertNull($disabledAttribute, sprintf("Failed asserting element '%s' is not disabled", $name));
} else {
assertNotNull($disabledAttribute, sprintf("Failed asserting element '%s' is disabled", $name));
Assert::assertNotNull($disabledAttribute, sprintf("Failed asserting element '%s' is disabled", $name));
}
}

Expand All @@ -864,11 +865,11 @@ public function stepFieldShouldBeEnabled($field)
{
$page = $this->getSession()->getPage();
$fieldElement = $page->findField($field);
assertNotNull($fieldElement, sprintf("Field '%s' not found", $field));
Assert::assertNotNull($fieldElement, sprintf("Field '%s' not found", $field));

$disabledAttribute = $fieldElement->getAttribute('disabled');

assertNull($disabledAttribute, sprintf("Failed asserting field '%s' is enabled", $field));
Assert::assertNull($disabledAttribute, sprintf("Failed asserting field '%s' is enabled", $field));
}

/**
Expand All @@ -887,7 +888,7 @@ public function iFollowInTheRegion($link, $region)
{
$context = $this->getMainContext();
$regionObj = $context->getRegionObj($region);
assertNotNull($regionObj);
Assert::assertNotNull($regionObj);

$linkObj = $regionObj->findLink($link);
if (empty($linkObj)) {
Expand All @@ -913,7 +914,7 @@ public function iFillinTheRegion($field, $value, $region)
{
$context = $this->getMainContext();
$regionObj = $context->getRegionObj($region);
assertNotNull($regionObj, "Region Object is null");
Assert::assertNotNull($regionObj, "Region Object is null");

$fieldObj = $regionObj->findField($field);
if (empty($fieldObj)) {
Expand Down Expand Up @@ -943,7 +944,7 @@ public function iSeeTextInRegion($negate, $text, $region)
{
$context = $this->getMainContext();
$regionObj = $context->getRegionObj($region);
assertNotNull($regionObj);
Assert::assertNotNull($regionObj);

$actual = $regionObj->getText();
$actual = preg_replace('/\s+/u', ' ', $actual);
Expand Down Expand Up @@ -987,7 +988,7 @@ public function iSelectTheRadioButton($radioLabel)
'radio',
$this->getMainContext()->getXpathEscaper()->escapeLiteral($radioLabel)
]);
assertNotNull($radioButton);
Assert::assertNotNull($radioButton);
$session->getDriver()->click($radioButton->getXPath());
}

Expand All @@ -1001,7 +1002,7 @@ public function theTableShouldContain($selector, $text)
$table = $this->getTable($selector);

$element = $table->find('named', array('content', "'$text'"));
assertNotNull($element, sprintf('Element containing `%s` not found in `%s` table', $text, $selector));
Assert::assertNotNull($element, sprintf('Element containing `%s` not found in `%s` table', $text, $selector));
}

/**
Expand All @@ -1014,7 +1015,7 @@ public function theTableShouldNotContain($selector, $text)
$table = $this->getTable($selector);

$element = $table->find('named', array('content', "'$text'"));
assertNull($element, sprintf('Element containing `%s` not found in `%s` table', $text, $selector));
Assert::assertNull($element, sprintf('Element containing `%s` not found in `%s` table', $text, $selector));
}

/**
Expand All @@ -1027,7 +1028,7 @@ public function iClickOnInTheTable($text, $selector)
$table = $this->getTable($selector);

$element = $table->find('xpath', sprintf('//*[count(*)=0 and contains(.,"%s")]', $text));
assertNotNull($element, sprintf('Element containing `%s` not found', $text));
Assert::assertNotNull($element, sprintf('Element containing `%s` not found', $text));
$element->click();
}

Expand Down Expand Up @@ -1065,7 +1066,7 @@ protected function getTable($selector)
// Some tables don't have a visible title, so look for a fieldset with data-name instead
$candidates += $page->findAll('xpath', "//fieldset[@data-name=$selector]//table");

assertTrue((bool)$candidates, 'Could not find any table elements');
Assert::assertTrue((bool)$candidates, 'Could not find any table elements');

$table = null;
/** @var NodeElement $candidate */
Expand All @@ -1075,7 +1076,7 @@ protected function getTable($selector)
}
}

assertTrue((bool)$table, 'Found table elements, but none are visible');
Assert::assertTrue((bool)$table, 'Found table elements, but none are visible');

return $table;
}
Expand All @@ -1092,19 +1093,19 @@ protected function getTable($selector)
public function theTextBeforeAfter($textBefore, $order, $textAfter, $element)
{
$ele = $this->getSession()->getPage()->find('css', $element);
assertNotNull($ele, sprintf('%s not found', $element));
Assert::assertNotNull($ele, sprintf('%s not found', $element));

// Check both of the texts exist in the element
$text = $ele->getText();
assertTrue(strpos($text, $textBefore) !== 'FALSE', sprintf('%s not found in the element %s', $textBefore, $element));
assertTrue(strpos($text, $textAfter) !== 'FALSE', sprintf('%s not found in the element %s', $textAfter, $element));
Assert::assertTrue(strpos($text, $textBefore) !== 'FALSE', sprintf('%s not found in the element %s', $textBefore, $element));
Assert::assertTrue(strpos($text, $textAfter) !== 'FALSE', sprintf('%s not found in the element %s', $textAfter, $element));

/// Use strpos to get the position of the first occurrence of the two texts (case-sensitive)
// and compare them with the given order (before or after)
if ($order === 'before') {
assertTrue(strpos($text, $textBefore) < strpos($text, $textAfter));
Assert::assertTrue(strpos($text, $textBefore) < strpos($text, $textAfter));
} else {
assertTrue(strpos($text, $textBefore) > strpos($text, $textAfter));
Assert::assertTrue(strpos($text, $textBefore) > strpos($text, $textAfter));
}
}

Expand Down Expand Up @@ -1213,7 +1214,7 @@ public function iScrollToField($locator, $type)
{
$page = $this->getSession()->getPage();
$el = $page->find('named', array($type, "'$locator'"));
assertNotNull($el, sprintf('%s element not found', $locator));
Assert::assertNotNull($el, sprintf('%s element not found', $locator));

$id = $el->getAttribute('id');
if (empty($id)) {
Expand All @@ -1236,7 +1237,7 @@ public function iScrollToField($locator, $type)
public function iScrollToElement($locator)
{
$el = $this->getSession()->getPage()->find('css', $locator);
assertNotNull($el, sprintf('The element "%s" is not found', $locator));
Assert::assertNotNull($el, sprintf('The element "%s" is not found', $locator));

$id = $el->getAttribute('id');
if (empty($id)) {
Expand Down Expand Up @@ -1305,7 +1306,7 @@ public function iShouldSeeTheElement($selector)
return document.querySelector("$sel");
JS;
$element = $this->getSession()->evaluateScript($js);
assertNotNull($element, sprintf('Element %s not found', $selector));
Assert::assertNotNull($element, sprintf('Element %s not found', $selector));
}

/**
Expand Down
Loading

0 comments on commit 9ee6858

Please sign in to comment.