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 0c927d4 commit 9714bcb
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 57 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
}
],
"require": {
"php": "^7.3 || ^8.0",
"silverstripe/admin": "^1.6",
"silverstripe/framework": "^4.6",
"silverstripe/framework": "^4.10",
"silverstripe/versioned": "^1.6",
"silverstripe/graphql": "^3.5 || ^4",
"silverstripe/vendor-plugin": "^1"
},
"require-dev": {
"sminnee/phpunit": "^5",
"sminnee/phpunit-mock-objects": "^3.4.5",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3",
"silverstripe/cms": "^4.6"
},
Expand Down
9 changes: 5 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<phpunit bootstrap="vendor/silverstripe/cms/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>tests</directory>
</testsuite>

<testsuites>
<testsuite name="Default">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
Expand Down
2 changes: 1 addition & 1 deletion tests/ArchiveAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ArchiveAdminTest extends SapphireTest
ViewProviderVersionedObject::class,
];

protected function setUp()
protected function setUp(): void
{
parent::setUp();
}
Expand Down
19 changes: 10 additions & 9 deletions tests/Behat/Context/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\VersionedAdmin\Tests\Behat\Context;

use Behat\Mink\Element\NodeElement;
use PHPUnit\Framework\Assert;
use SilverStripe\BehatExtension\Context\SilverStripeContext;

if (!class_exists(SilverStripeContext::class)) {
Expand All @@ -25,7 +26,7 @@ public function iShouldSeeAListOfVersions()
public function iShouldSeeAListOfVersionsInDescendingOrder()
{
$versions = $this->getVersions();
assertNotEmpty($versions, 'I see a list of versions');
Assert::assertNotEmpty($versions, 'I see a list of versions');

$previous = null;

Expand All @@ -43,7 +44,7 @@ public function iShouldSeeAListOfVersionsInDescendingOrder()
*/
public function iClickOnTheFirstVersion()
{
assertNotNull($this->getLatestVersion(), 'I should see a list of versions');
Assert::assertNotNull($this->getLatestVersion(), 'I should see a list of versions');
$this->getLatestVersion()->click();
}

Expand All @@ -61,7 +62,7 @@ public function iClickOnVersion($versionNo)
break;
}
}
assertNotNull($desiredVersion, 'Desired version ' . $versionNo . ' was not found in the page.');
Assert::assertNotNull($desiredVersion, 'Desired version ' . $versionNo . ' was not found in the page.');
$this->clickVersion($desiredVersion);
}

Expand All @@ -71,7 +72,7 @@ public function iClickOnVersion($versionNo)
public function iOpenTheHistoryViewerActionsMenu()
{
$button = $this->getSession()->getPage()->find('css', '.history-viewer__heading .history-viewer__actions .btn');
assertNotNull($button, 'History viewer actions menu not found in the page.');
Assert::assertNotNull($button, 'History viewer actions menu not found in the page.');

$button->click();
}
Expand All @@ -85,7 +86,7 @@ public function theTextShouldBeDeleted($text)
'xpath',
sprintf('//del[contains(normalize-space(string(.)), \'%s\')]', $text)
);
assertNotNull($result, $text . ' was not shown as deleted');
Assert::assertNotNull($result, $text . ' was not shown as deleted');
}

/**
Expand All @@ -97,7 +98,7 @@ public function theTextShouldBeAdded($text)
'xpath',
sprintf('//ins[contains(normalize-space(string(.)), \'%s\')]', $text)
);
assertNotNull($result, $text . ' was not shown as added');
Assert::assertNotNull($result, $text . ' was not shown as added');
}

/**
Expand Down Expand Up @@ -158,7 +159,7 @@ public function iShouldSeeInTheAuthorColumn($text, $versionNumber)
$authorColumn = $version->find('css', '.history-viewer__author');

$exists = strpos($authorColumn->getText(), $text) !== false;
assertTrue($exists, 'Author column contains ' . $text);
Assert::assertTrue($exists, 'Author column contains ' . $text);
}

/**
Expand All @@ -172,7 +173,7 @@ public function iShouldSeeInTheRecordColumn($text, $versionNumber)
$recordColumn = $version->find('css', '.history-viewer__version-state');

$exists = strpos($recordColumn->getText(), $text) !== false;
assertTrue($exists, 'Record column contains ' . $text);
Assert::assertTrue($exists, 'Record column contains ' . $text);
}

/**
Expand All @@ -184,7 +185,7 @@ public function iShouldSeeInTheVersionColumn($text, $versionNumber)
$versionColumn = $version->find('css', '.history-viewer__version-no');

$exists = strpos($versionColumn->getText(), $text) !== false;
assertTrue($exists, 'Version column contains ' . $text);
Assert::assertTrue($exists, 'Version column contains ' . $text);
}

/**
Expand Down
44 changes: 15 additions & 29 deletions tests/Controllers/HistoryViewerControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,18 @@ public function testSchema()
$this->assertSame('application/json', $result->getHeader('Content-Type'));
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessageRegExp /Missing required field/
*/
public function testGetVersionFormThrowsExceptionWhenArgsAreMissing()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/Missing required field/');
$controller = new HistoryViewerController();
$controller->getVersionForm([]);
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessageRegExp /Missing required field/
*/
public function testGetVersionFormThrowsExceptionWhenArgsAreFalsy()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/Missing required field/');
$controller = new HistoryViewerController();
$controller->getVersionForm([
'RecordClass' => 'Page',
Expand All @@ -109,12 +105,10 @@ public function testGetVersionFormThrowsExceptionWhenArgsAreFalsy()
]);
}

/**
* @expectedException \SilverStripe\Control\HTTPResponse_Exception
* @expectedExceptionCode 404
*/
public function testGetVersionFormThrowsExceptionWhenRecordVersionDoesntExist()
{
$this->expectException(\SilverStripe\Control\HTTPResponse_Exception::class);
$this->expectExceptionCode(404);
$controller = new HistoryViewerController();
$controller->getVersionForm([
'RecordClass' => UnviewableVersionedObject::class,
Expand All @@ -123,13 +117,11 @@ public function testGetVersionFormThrowsExceptionWhenRecordVersionDoesntExist()
]);
}

/**
* @expectedException \SilverStripe\Control\HTTPResponse_Exception
* @expectedExceptionCode 403
* @expectedExceptionMessage You don't have the necessary permissions to view Unviewable Versioned Object
*/
public function testGetVersionFormThrowsExceptionWhenCanViewIsFalse()
{
$this->expectException(\SilverStripe\Control\HTTPResponse_Exception::class);
$this->expectExceptionCode(403);
$this->expectExceptionMessage("You don't have the necessary permissions to view Unviewable Versioned Object");
$controller = new HistoryViewerController();
$controller->getVersionForm([
'RecordClass' => UnviewableVersionedObject::class,
Expand All @@ -151,32 +143,26 @@ public function testGetVersionFormReturnsForm()
$this->assertInstanceOf(LeftAndMainFormRequestHandler::class, $result->getRequestHandler());
}

/**
* @expectedException \SilverStripe\Control\HTTPResponse_Exception
* @expectedExceptionCode 400
*/
public function testVersionFormThrowsExceptionWithoutRequest()
{
$this->expectException(\SilverStripe\Control\HTTPResponse_Exception::class);
$this->expectExceptionCode(400);
$controller = new HistoryViewerController();
$controller->versionForm(null);
}

/**
* @expectedException \SilverStripe\Control\HTTPResponse_Exception
* @expectedExceptionCode 400
*/
public function testCompareFormThrowsExceptionWithoutRequest()
{
$this->expectException(\SilverStripe\Control\HTTPResponse_Exception::class);
$this->expectExceptionMessage(400);
$controller = new HistoryViewerController();
$controller->compareForm(null);
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessageRegExp /Missing required field/
*/
public function testVersionFormThrowsExceptionWhenArgsAreFalsy()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/Missing required field/');
$controller = new HistoryViewerController();
$controller->getVersionForm([
'RecordClass' => 'Page',
Expand Down
8 changes: 3 additions & 5 deletions tests/Forms/DataObjectVersionFormFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DataObjectVersionFormFactoryTest extends SapphireTest
*/
protected $controller;

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand All @@ -56,12 +56,10 @@ public function testIsReadonlyFormType()
);
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Missing required context Record
*/
public function testGetFormThrowsExceptionOnMissingRequiredContext()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Missing required context Record');
$this->factory->getForm();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Forms/DiffFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testScalarValuesAreDiffed()
$diffField->setComparisonField($newField);
$diffField->setValue('old');

$this->assertRegExp('/^<ins>new<\/ins> *<del>old<\/del>$/', $diffField->Value());
$this->assertMatchesRegularExpression('/^<ins>new<\/ins> *<del>old<\/del>$/', $diffField->Value());
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/Forms/DiffTransformationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DiffTransformationTest extends SapphireTest
*/
private $testForm;

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down Expand Up @@ -62,8 +62,8 @@ public function testTransform()
$form->loadDataFrom($oldData);

foreach ($form->Fields() as $index => $field) {
$this->assertContains($expected[$index]['before'], $field->Value(), 'Value before is shown');
$this->assertContains($expected[$index]['after'], $field->Value(), 'Value after is shown');
$this->assertStringContainsString($expected[$index]['before'], $field->Value(), 'Value before is shown');
$this->assertStringContainsString($expected[$index]['after'], $field->Value(), 'Value after is shown');
}
}

Expand All @@ -87,8 +87,8 @@ public function testTransformWithCompositeFields()
$form->loadDataFrom($oldData);

foreach (array_values($form->Fields()->dataFields()) as $index => $field) {
$this->assertContains($expected[$index]['before'], $field->Value(), 'Value before is shown');
$this->assertContains($expected[$index]['after'], $field->Value(), 'Value after is shown');
$this->assertStringContainsString($expected[$index]['before'], $field->Value(), 'Value before is shown');
$this->assertStringContainsString($expected[$index]['after'], $field->Value(), 'Value after is shown');
}
}

Expand Down

0 comments on commit 9714bcb

Please sign in to comment.