diff --git a/core/lib/Drupal/Component/Diff/DiffEngine.php b/core/lib/Drupal/Component/Diff/DiffEngine.php index 0194379..fd6ce4b 100644 --- a/core/lib/Drupal/Component/Diff/DiffEngine.php +++ b/core/lib/Drupal/Component/Diff/DiffEngine.php @@ -8,6 +8,8 @@ * You may copy this code freely under the conditions of the GPL. */ +namespace Drupal\Component\Diff; + use Drupal\Component\Utility\Settings; use Drupal\Component\Utility\String; use Drupal\Component\Utility\Unicode; @@ -570,7 +572,7 @@ function _shift_boundaries($lines, &$changed, $other_changed) { * @subpackage DifferenceEngine */ class Diff { - var $edits; + public $edits; /** * Constructor. @@ -580,7 +582,7 @@ class Diff { * (Typically these are lines from a file.) * @param $to_lines array An array of strings. */ - function Diff($from_lines, $to_lines) { + public function __construct($from_lines, $to_lines) { $eng = new _DiffEngine; $this->edits = $eng->diff($from_lines, $to_lines); //$this->_check($from_lines, $to_lines); diff --git a/core/tests/Drupal/Tests/Component/Diff/DiffEngineTest.php b/core/tests/Drupal/Tests/Component/Diff/DiffEngineTest.php index 97c1f57..91185e9 100644 --- a/core/tests/Drupal/Tests/Component/Diff/DiffEngineTest.php +++ b/core/tests/Drupal/Tests/Component/Diff/DiffEngineTest.php @@ -5,75 +5,83 @@ * Contains \Drupal\Tests\Component\DiffEngineTest. */ -namespace Drupal\Tests\Component; +namespace Drupal\Tests\Component\Diff; +// Since this file holds more than a few classes, we can't rely on +// autoloading the classes in its namespace. +require_once(__DIR__ . '/../../../../../../core/lib/Drupal/Component/Diff/DiffEngine.php'); + +use Drupal\Component\Diff\Diff; +use Drupal\Component\Diff\_DiffOp_Copy; +use Drupal\Component\Diff\_DiffOp_Add; use Drupal\Tests\UnitTestCase; /** * Tests the DiffEngine class. + * + * @group Drupal + * @group Component + * @group Diff */ class DiffEngineTest extends UnitTestCase { - /** - * Lines array to use for testing. - * - * @var array - */ - protected $lines1; - - /** - * Lines array to use for testing. - * - * @var array - */ - protected $lines2; - - public static function getInfo() { - return array( - 'name' => 'DiffEngine', - 'description' => 'Tests the DiffEngine class.', - 'group' => 'System', - ); - } - - function setUp() { - parent::setUp(); - - $this->lines1 = array( - 'Hello World', - 'I am an original line' - ); - - $this->lines2 = array( - 'Hello World', - 'I am a new line', - 'I am an original line' - ); - } - - /** - * Tests Diff::Diff(). - */ - public function testDiff() { - define('DRUPAL_ROOT', __DIR__ .'/../../../../../../'); - require_once DRUPAL_ROOT . '/core/lib/Drupal/Component/Diff/DiffEngine.php'; - - $diff = new \Diff($this->lines1, $this->lines2); - - $expected = array( - new \_DiffOp_Copy(array('Hello World')), - new \_DiffOp_Add(array('I am a new line')), - new \_DiffOp_Copy(array('I am an original line')) - ); - - $this->assertCount(count($expected), $diff->edits); - - foreach($diff->edits as $key => $diffObject) { - $this->assertInstanceOf(get_class($expected[$key]), $diffObject); - $this->assertSame($expected[$key]->orig, $diffObject->orig); - $this->assertSame($expected[$key]->closing, $diffObject->closing); - } - + /** + * {@inheritdoc} + */ + public static function getInfo() { + return array( + 'name' => 'DiffEngine', + 'description' => 'Tests the DiffEngine class.', + 'group' => 'System', + ); + } + + /** + * Data provider for testDiff(). + * + * @return + * array Test data. + */ + public function providerTestDiff() { + return array( + array( + // $expected + array( + new _DiffOp_Copy(array('Hello World')), + new _DiffOp_Add(array('I am a new line')), + new _DiffOp_Copy(array('I am an original line')) + ), + // $old + array( + 'Hello World', + 'I am an original line' + ), + // $new + array( + 'Hello World', + 'I am a new line', + 'I am an original line' + ), + ), + ); + } + + /** + * Tests Diff::Diff(). + * + * A basic test of the Diff class. + * + * @dataProvider providerTestDiff + */ + public function testDiff($expected, $old, $new) { + $diff = new Diff($old, $new); + $this->assertCount(count($expected), $diff->edits); + + foreach ($diff->edits as $key => $diffObject) { + $this->assertInstanceOf(get_class($expected[$key]), $diffObject); + $this->assertSame($expected[$key]->orig, $diffObject->orig); + $this->assertSame($expected[$key]->closing, $diffObject->closing); } + } }