Merge pull request #8717 from tractorcow/pulls/4.2/test-assert-field

BUG Fix issue with assertListEquals() ignoring field getters
This commit is contained in:
Robbie Averill 2019-01-17 21:49:08 +01:00 committed by GitHub
commit c58a75eed1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 1 deletions

View File

@ -64,7 +64,7 @@ class ViewableDataContains extends PHPUnit_Framework_Constraint implements TestO
$success = true;
foreach ($this->match as $fieldName => $value) {
if ($other->getField($fieldName) != $value) {
if ($other->$fieldName != $value) {
$success = false;
break;
}

View File

@ -4,6 +4,7 @@ namespace SilverStripe\Dev\Tests;
use SilverStripe\Dev\Constraint\ViewableDataContains;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\Tests\ViewableDataContainsTest\TestObject;
use SilverStripe\Security\Member;
use SilverStripe\View\ArrayData;
@ -100,4 +101,14 @@ class ViewableDataContainsTest extends SapphireTest
$this->assertFalse($constraint->evaluate($item, '', true));
}
public function testFieldAccess()
{
$data = new TestObject(['name' => 'Damian']);
$constraint = new ViewableDataContains(['name' => 'Damian', 'Something' => 'something']);
$this->assertTrue($constraint->evaluate($data, '', true));
$constraint = new ViewableDataContains(['name' => 'Damian', 'Something' => 'notthing']);
$this->assertFalse($constraint->evaluate($data, '', true));
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace SilverStripe\Dev\Tests\ViewableDataContainsTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\View\ViewableData;
class TestObject extends ViewableData implements TestOnly
{
protected $data = null;
public function __construct($data)
{
$this->data = $data;
}
public function hasField($name)
{
return isset($this->data[$name]);
}
public function getField($name)
{
return isset($this->data[$name]) ?: null;
}
public function getSomething()
{
return 'something';
}
}