diff --git a/src/ApiCore/Testing/MessageAwareArrayComparator.php b/src/ApiCore/Testing/MessageAwareArrayComparator.php new file mode 100644 index 000000000..090137f6d --- /dev/null +++ b/src/ApiCore/Testing/MessageAwareArrayComparator.php @@ -0,0 +1,30 @@ +exporter = new MessageAwareExporter(); + } +} diff --git a/src/ApiCore/Testing/MessageAwareExporter.php b/src/ApiCore/Testing/MessageAwareExporter.php new file mode 100644 index 000000000..0878250d7 --- /dev/null +++ b/src/ApiCore/Testing/MessageAwareExporter.php @@ -0,0 +1,45 @@ +exporter = new MessageAwareExporter(); + } + + /** + * Returns whether the comparator can compare two values. + * + * @param mixed $expected The first value to compare + * @param mixed $actual The second value to compare + * @return boolean + */ + public function accepts($expected, $actual) + { + return $expected instanceof Message && $actual instanceof Message; + } + + /** + * Asserts that two values are equal. + * + * @param Message $expected The first value to compare + * @param Message $actual The second value to compare + * @param float|int $delta The allowed numerical distance between two values to + * consider them equal + * @param bool $canonicalize If set to TRUE, arrays are sorted before + * comparison + * @param bool $ignoreCase If set to TRUE, upper- and lowercasing is + * ignored when comparing string values + * @throws \PHPUnit_Framework_ComparisonFailure Thrown when the comparison + * fails. Contains information about the + * specific errors that lead to the failure. + */ + public function assertEquals($expected, $actual, $delta = 0, $canonicalize = FALSE, $ignoreCase = FALSE) + { + if ($expected->serializeToString() !== $actual->serializeToString()) { + throw new ComparisonFailure( + $expected, + $actual, + $this->exporter->shortenedExport($expected), + $this->exporter->shortenedExport($actual), + false, + 'Given 2 Message objects are not the same' + ); + } + } +} diff --git a/tests/ApiCore/Tests/Unit/MyMessage.php b/tests/ApiCore/Tests/Unit/MyMessage.php new file mode 100644 index 000000000..9f8712230 --- /dev/null +++ b/tests/ApiCore/Tests/Unit/MyMessage.php @@ -0,0 +1,31 @@ +google.apicore.tests.unit.MyMessage + */ +class MyMessage extends \Google\Protobuf\Internal\Message +{ + + /** + * Constructor. + * + * @param array $data { + * Optional. Data for populating the Message object. + * + * } + */ + public function __construct($data = NULL) { + \GPBMetadata\Google\ApiCore\Tests\Unit\Example::initOnce(); + parent::__construct($data); + } + +} + diff --git a/tests/ApiCore/Tests/Unit/ProtobufBandaidTest.php b/tests/ApiCore/Tests/Unit/ProtobufBandaidTest.php new file mode 100644 index 000000000..7b8f955e6 --- /dev/null +++ b/tests/ApiCore/Tests/Unit/ProtobufBandaidTest.php @@ -0,0 +1,41 @@ +assertEquals($expected, $actual); + } + + public function protobufMessageProvider() + { + $msg1 = new MyMessage(); + $msg2 = new Mymessage(); + return [ + [$msg1, $msg2], + [[$msg1, $msg2], [$msg1, $msg2]] + ]; + } +} diff --git a/tests/ApiCore/Tests/Unit/testdata/src/GPBMetadata/Google/ApiCore/Tests/Unit/Example.php b/tests/ApiCore/Tests/Unit/testdata/src/GPBMetadata/Google/ApiCore/Tests/Unit/Example.php new file mode 100644 index 000000000..d958e2b52 --- /dev/null +++ b/tests/ApiCore/Tests/Unit/testdata/src/GPBMetadata/Google/ApiCore/Tests/Unit/Example.php @@ -0,0 +1,28 @@ +internalAddGeneratedFile(hex2bin( + "0a85010a0d6578616d706c652e70726f746f1219676f6f676c652e617069" . + "636f72652e74657374732e756e6974220b0a094d794d6573736167654244" . + "ca0219476f6f676c655c417069436f72655c54657374735c556e6974e202" . + "254750424d657461646174615c476f6f676c655c417069436f72655c5465" . + "7374735c556e6974620670726f746f33" + )); + + static::$is_initialized = true; + } +} + diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 69dca88fa..50570f954 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -29,9 +29,21 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -require_once __DIR__ . '/../vendor/autoload.php'; +$loader = require_once __DIR__ . '/../vendor/autoload.php'; +$loader->add( + 'GPBMetadata\Google\ApiCore\Tests\Unit', + __DIR__ . '/ApiCore/Tests/Unit/testdata/src/' +); + +use Google\ApiCore\Testing\MessageAwareArrayComparator; +use Google\ApiCore\Testing\ProtobufMessageComparator; +use Google\ApiCore\Testing\ProtobufGPBEmptyComparator; date_default_timezone_set('UTC'); ini_set('error_reporting', E_ALL); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); + +\SebastianBergmann\Comparator\Factory::getInstance()->register(new MessageAwareArrayComparator()); +\SebastianBergmann\Comparator\Factory::getInstance()->register(new ProtobufMessageComparator()); +\SebastianBergmann\Comparator\Factory::getInstance()->register(new ProtobufGPBEmptyComparator());