API phpunit9 support

This commit is contained in:
Steve Boyd 2021-10-27 17:14:44 +13:00
parent ba2e93131e
commit 9ee6858f91
8 changed files with 95 additions and 92 deletions

View File

@ -21,8 +21,8 @@
}
],
"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",
@ -30,7 +30,7 @@
"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": {

View File

@ -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());
}
```

View File

@ -1,6 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="Default">
<directory>tests/php</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@ -17,6 +17,7 @@ use Facebook\WebDriver\WebDriver;
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;
@ -366,7 +367,7 @@ JS;
public function stepPageCantBeFound()
{
$page = $this->getSession()->getPage();
assertTrue(
Assert::assertTrue(
// Content from ErrorPage default record
$page->hasContent('Page not found')
// Generic ModelAsController message
@ -425,9 +426,9 @@ JS;
{
$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));
}
}
@ -438,7 +439,7 @@ JS;
public function stepIPressTheButton($text)
{
$button = $this->findNamedButton($text);
assertNotNull($button, "{$text} button not found");
Assert::assertNotNull($button, "{$text} button not found");
$button->click();
}
@ -456,7 +457,7 @@ JS;
}
}
assertNotNull($button, "{$text} button not found");
Assert::assertNotNull($button, "{$text} button not found");
$button->click();
}
@ -495,7 +496,7 @@ JS;
{
$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();
}
@ -525,9 +526,9 @@ JS;
);
$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();
}
@ -569,7 +570,7 @@ JS;
public function iSeeTheDialogText($expected)
{
$text = $this->getExpectedAlert()->getText();
assertContains($expected, $text);
Assert::assertStringContainsString($expected, $text);
}
/**
@ -841,13 +842,13 @@ JS;
));
}
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));
}
}
@ -864,11 +865,11 @@ JS;
{
$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));
}
/**
@ -887,7 +888,7 @@ JS;
{
$context = $this->getMainContext();
$regionObj = $context->getRegionObj($region);
assertNotNull($regionObj);
Assert::assertNotNull($regionObj);
$linkObj = $regionObj->findLink($link);
if (empty($linkObj)) {
@ -913,7 +914,7 @@ JS;
{
$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)) {
@ -943,7 +944,7 @@ JS;
{
$context = $this->getMainContext();
$regionObj = $context->getRegionObj($region);
assertNotNull($regionObj);
Assert::assertNotNull($regionObj);
$actual = $regionObj->getText();
$actual = preg_replace('/\s+/u', ' ', $actual);
@ -987,7 +988,7 @@ JS;
'radio',
$this->getMainContext()->getXpathEscaper()->escapeLiteral($radioLabel)
]);
assertNotNull($radioButton);
Assert::assertNotNull($radioButton);
$session->getDriver()->click($radioButton->getXPath());
}
@ -1001,7 +1002,7 @@ JS;
$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));
}
/**
@ -1014,7 +1015,7 @@ JS;
$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));
}
/**
@ -1027,7 +1028,7 @@ JS;
$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();
}
@ -1065,7 +1066,7 @@ JS;
// 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 */
@ -1075,7 +1076,7 @@ JS;
}
}
assertTrue((bool)$table, 'Found table elements, but none are visible');
Assert::assertTrue((bool)$table, 'Found table elements, but none are visible');
return $table;
}
@ -1092,19 +1093,19 @@ JS;
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));
}
}
@ -1213,7 +1214,7 @@ JS;
{
$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)) {
@ -1236,7 +1237,7 @@ JS;
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)) {
@ -1305,7 +1306,7 @@ JS;
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));
}
/**

View File

@ -6,6 +6,7 @@ use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Session;
use PHPUnit\Framework\Assert;
use SilverStripe\BehatExtension\Utility\TestMailer;
use SilverStripe\Control\Email\Email;
use SilverStripe\Control\Email\Mailer;
@ -65,9 +66,9 @@ class EmailContext implements Context
$from = ($direction == 'from') ? $email : null;
$match = $this->mailer->findEmail($to, $from);
if (trim($negate)) {
assertNull($match);
Assert::assertNull($match);
} else {
assertNotNull($match);
Assert::assertNotNull($match);
}
$this->lastMatchedEmail = $match;
}
@ -89,7 +90,7 @@ class EmailContext implements Context
return $email->Subject;
}, $allMails)) . '"' : null;
if (trim($negate)) {
assertNull($match);
Assert::assertNull($match);
} else {
$msg = sprintf(
'Could not find email %s "%s" titled "%s".',
@ -100,7 +101,7 @@ class EmailContext implements Context
if ($allTitles) {
$msg .= ' Existing emails: ' . $allTitles;
}
assertNotNull($match, $msg);
Assert::assertNotNull($match, $msg);
}
$this->lastMatchedEmail = $match;
}
@ -129,9 +130,9 @@ class EmailContext implements Context
}
if (trim($negate)) {
assertNotContains($content, $emailContent);
Assert::assertNotContains($content, $emailContent);
} else {
assertContains($content, $emailContent);
Assert::assertContains($content, $emailContent);
}
}
@ -155,7 +156,7 @@ class EmailContext implements Context
$emailPlainText = strip_tags($emailContent);
$emailPlainText = preg_replace("/\h+/", " ", $emailPlainText);
assertContains($content, $emailPlainText);
Assert::assertContains($content, $emailPlainText);
}
/**
@ -169,13 +170,13 @@ class EmailContext implements Context
$to = ($direction == 'to') ? $email : null;
$from = ($direction == 'from') ? $email : null;
$match = $this->mailer->findEmail($to, $from);
assertNotNull($match);
Assert::assertNotNull($match);
$crawler = new Crawler($match->Content);
$linkEl = $crawler->selectLink($linkSelector);
assertNotNull($linkEl);
Assert::assertNotNull($linkEl);
$link = $linkEl->attr('href');
assertNotNull($link);
Assert::assertNotNull($link);
$this->getMainContext()->visit($link);
}
@ -192,13 +193,13 @@ class EmailContext implements Context
$to = ($direction == 'to') ? $email : null;
$from = ($direction == 'from') ? $email : null;
$match = $this->mailer->findEmail($to, $from, $title);
assertNotNull($match);
Assert::assertNotNull($match);
$crawler = new Crawler($match->Content);
$linkEl = $crawler->selectLink($linkSelector);
assertNotNull($linkEl);
Assert::assertNotNull($linkEl);
$link = $linkEl->attr('href');
assertNotNull($link);
Assert::assertNotNull($link);
$this->getMainContext()->visit($link);
}
@ -218,9 +219,9 @@ class EmailContext implements Context
$match = $this->lastMatchedEmail;
$crawler = new Crawler($match->Content);
$linkEl = $crawler->selectLink($linkSelector);
assertNotNull($linkEl);
Assert::assertNotNull($linkEl);
$link = $linkEl->attr('href');
assertNotNull($link);
Assert::assertNotNull($link);
$this->getMainContext()->visit($link);
}
@ -264,11 +265,11 @@ class EmailContext implements Context
// For "should not contain"
if (trim($negate)) {
foreach ($rows as $row) {
assertNotContains($row[0], $emailContent);
Assert::assertNotContains($row[0], $emailContent);
}
} else {
foreach ($rows as $row) {
assertContains($row[0], $emailContent);
Assert::assertContains($row[0], $emailContent);
}
}
}
@ -282,13 +283,13 @@ class EmailContext implements Context
{
$match = $this->mailer->findEmail(null, null, $subject);
if (trim($negate)) {
assertNull($match);
Assert::assertNull($match);
} else {
$msg = sprintf(
'Could not find email titled "%s".',
$subject
);
assertNotNull($match, $msg);
Assert::assertNotNull($match, $msg);
}
$this->lastMatchedEmail = $match;
}
@ -306,9 +307,9 @@ class EmailContext implements Context
$match = $this->lastMatchedEmail;
if (trim($negate)) {
assertNotContains($from, $match->From);
Assert::assertNotContains($from, $match->From);
} else {
assertContains($from, $match->From);
Assert::assertContains($from, $match->From);
}
}
@ -325,9 +326,9 @@ class EmailContext implements Context
$match = $this->lastMatchedEmail;
if (trim($negate)) {
assertNotContains($to, $match->To);
Assert::assertNotContains($to, $match->To);
} else {
assertContains($to, $match->To);
Assert::assertContains($to, $match->To);
}
}
@ -358,7 +359,7 @@ class EmailContext implements Context
break;
}
}
assertNotNull($href);
Assert::assertNotNull($href);
$this->getMainContext()->visit($href);
}

View File

@ -9,6 +9,7 @@ use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Exception;
use InvalidArgumentException;
use PHPUnit\Framework\Assert;
use SilverStripe\Assets\Folder;
use SilverStripe\Assets\Storage\AssetStore;
use SilverStripe\Core\ClassInfo;
@ -650,7 +651,7 @@ class FixtureContext implements Context
public function iAddAnExtensionToTheClass($extension, $class)
{
// Validate the extension
assertTrue(
Assert::assertTrue(
class_exists($extension) && is_subclass_of($extension, Extension::class),
'Given extension does not extend Extension'
);
@ -696,9 +697,9 @@ YAML;
{
$project = ModuleManifest::config()->get('project') ?: 'mysite';
$mysite = ModuleLoader::getModule($project);
assertNotNull($mysite, 'Project exists');
Assert::assertNotNull($mysite, 'Project exists');
$destPath = $mysite->getResource("_config/{$filename}")->getPath();
assertFileNotExists($destPath, "Config file {$filename} hasn't aleady been loaded");
Assert::assertFileDoesNotExist($destPath, "Config file {$filename} hasn't aleady been loaded");
return $destPath;
}
@ -712,7 +713,7 @@ YAML;
*/
public function stepThereShouldBeAFileOrFolder($type, $path)
{
assertFileExists($this->joinPaths(BASE_PATH, $path));
Assert::assertFileExists($this->joinPaths(BASE_PATH, $path));
}
/**
@ -727,7 +728,7 @@ YAML;
public function stepThereShouldBeAFileWithTuple($filename, $hash)
{
$exists = $this->getAssetStore()->exists($filename, $hash);
assertTrue((bool)$exists, "A file exists with filename $filename and hash $hash");
Assert::assertTrue((bool)$exists, "A file exists with filename $filename and hash $hash");
}
/**

View File

@ -4,6 +4,7 @@ namespace SilverStripe\BehatExtension\Context;
use Behat\Behat\Context\Context;
use Behat\Mink\Element\NodeElement;
use PHPUnit\Framework\Assert;
use SilverStripe\Security\Authenticator;
use SilverStripe\Security\Group;
use SilverStripe\Security\Member;
@ -32,7 +33,7 @@ class LoginContext implements Context
if (0 == strpos($this->getMainContext()->getSession()->getCurrentUrl(), $loginUrl)) {
$this->stepILogInWith('admin', 'password');
assertStringStartsWith($adminUrl, $this->getMainContext()->getSession()->getCurrentUrl());
Assert::assertStringStartsWith($adminUrl, $this->getMainContext()->getSession()->getCurrentUrl());
}
}
@ -63,13 +64,13 @@ class LoginContext implements Context
$page = $this->getMainContext()->getSession()->getPage();
$form = $page->findById('LogoutForm_Form');
assertNotNull($form, 'Logout form not found');
Assert::assertNotNull($form, 'Logout form not found');
$submitButton = $form->find('css', '[type=submit]');
$securityID = $form->find('css', '[name=SecurityID]');
assertNotNull($submitButton, 'Submit button on logout form not found');
assertNotNull($securityID, 'CSRF token not found');
Assert::assertNotNull($submitButton, 'Submit button on logout form not found');
Assert::assertNotNull($securityID, 'CSRF token not found');
$submitButton->press();
}
@ -86,7 +87,7 @@ class LoginContext implements Context
$this->getMainContext()->getSession()->visit($loginUrl);
$page = $this->getMainContext()->getSession()->getPage();
$form = $page->findById('MemberLoginForm_LoginForm');
assertNotNull($form, 'Login form not found');
Assert::assertNotNull($form, 'Login form not found');
// Try to find visible forms again on login page.
$visibleForm = null;
@ -94,17 +95,17 @@ class LoginContext implements Context
if ($form->isVisible() && $form->find('css', '[name=Email]')) {
$visibleForm = $form;
}
assertNotNull($visibleForm, 'Could not find login email field');
Assert::assertNotNull($visibleForm, 'Could not find login email field');
$emailField = $visibleForm->find('css', '[name=Email]');
$passwordField = $visibleForm->find('css', '[name=Password]');
$submitButton = $visibleForm->find('css', '[type=submit]');
$securityID = $visibleForm->find('css', '[name=SecurityID]');
assertNotNull($emailField, 'Email field on login form not found');
assertNotNull($passwordField, 'Password field on login form not found');
assertNotNull($submitButton, 'Submit button on login form not found');
assertNotNull($securityID, 'CSRF token not found');
Assert::assertNotNull($emailField, 'Email field on login form not found');
Assert::assertNotNull($passwordField, 'Password field on login form not found');
Assert::assertNotNull($submitButton, 'Submit button on login form not found');
Assert::assertNotNull($securityID, 'CSRF token not found');
$emailField->setValue($email);
$passwordField->setValue($password);
@ -119,7 +120,7 @@ class LoginContext implements Context
$page = $this->getMainContext()->getSession()->getPage();
$message = $page->find('css', '.message.error');
$error = $message ? $message->getText() : null;
assertNull($message, 'Could not log in with user ' . $email . '. Error: "' . $error. '""');
Assert::assertNull($message, 'Could not log in with user ' . $email . '. Error: "' . $error. '""');
}
/**
@ -129,7 +130,7 @@ class LoginContext implements Context
{
$page = $this->getMainContext()->getSession()->getPage();
$loginForm = $page->find('css', '#MemberLoginForm_LoginForm');
assertNotNull($loginForm, 'I should see a log-in form');
Assert::assertNotNull($loginForm, 'I should see a log-in form');
}
/**
@ -139,7 +140,7 @@ class LoginContext implements Context
{
$page = $this->getMainContext()->getSession()->getPage();
$logoutForm = $page->find('css', '#LogoutForm_Form');
assertNotNull($logoutForm, 'I should see a log-out form');
Assert::assertNotNull($logoutForm, 'I should see a log-out form');
}
/**
@ -150,7 +151,7 @@ class LoginContext implements Context
{
$page = $this->getMainContext()->getSession()->getPage();
$message = $page->find('css', sprintf('.message.%s', $type));
assertNotNull($message, sprintf('%s message not found.', $type));
Assert::assertNotNull($message, sprintf('%s message not found.', $type));
}
/**
@ -163,10 +164,10 @@ class LoginContext implements Context
{
/** @var Member $member */
$member = Member::get()->filter('Email', $id)->First();
assertNotNull($member);
Assert::assertNotNull($member);
$authenticators = Security::singleton()->getApplicableAuthenticators(Authenticator::CHECK_PASSWORD);
foreach ($authenticators as $authenticator) {
assertTrue($authenticator->checkPassword($member, $password)->isValid());
Assert::assertTrue($authenticator->checkPassword($member, $password)->isValid());
}
}

View File

@ -8,6 +8,7 @@ use Behat\Mink\Session;
use Behat\Mink\Mink;
use Behat\Mink\Driver\DriverInterface;
use Behat\Mink\Element\Element;
use PHPUnit\Framework\MockObject\MockObject;
use SilverStripe\BehatExtension\Tests\SilverStripeContextTest\FeatureContext;
use SilverStripe\Dev\SapphireTest;
@ -16,22 +17,18 @@ class SilverStripeContextTest extends SapphireTest
protected $backupGlobals = false;
/**
* @expectedException \LogicException
* @expectedExceptionMessage Cannot find 'region_map' in the behat.yml
*/
public function testGetRegionObjThrowsExceptionOnUnknownSelector()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage("Cannot find 'region_map' in the behat.yml");
$context = $this->getContextMock();
$context->getRegionObj('.unknown');
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Cannot find the specified region in the behat.yml
*/
public function testGetRegionObjThrowsExceptionOnUnknownRegion()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage("Cannot find the specified region in the behat.yml");
$context = $this->getContextMock();
$context->setRegionMap(array('MyRegion' => '.my-region'));
$context->getRegionObj('.unknown');
@ -92,7 +89,7 @@ class SilverStripeContextTest extends SapphireTest
}
/**
* @return Element|\PHPUnit_Framework_MockObject_MockObject
* @return Element|MockObject
*/
protected function getElementMock()
{