Skip to content

Commit

Permalink
Temporary workaround for protobuf extension issue (#199)
Browse files Browse the repository at this point in the history
* Temporary workaround for protobuf extension issue

protocolbuffers/protobuf#4761

* Moved GPBMetadata to metadata directory
  • Loading branch information
Takashi Matsuo authored Jul 18, 2018
1 parent a5d099c commit 02d5470
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 1 deletion.
28 changes: 28 additions & 0 deletions metadata/Google/ApiCore/Tests/Unit/Example.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions src/ApiCore/Testing/MessageAwareArrayComparator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\ApiCore\Testing;

use SebastianBergmann\Comparator\ArrayComparator;

class MessageAwareArrayComparator extends ArrayComparator
{
public function __construct()
{
parent::__construct();
$this->exporter = new MessageAwareExporter();
}
}
43 changes: 43 additions & 0 deletions src/ApiCore/Testing/MessageAwareExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\ApiCore\Testing;

use Google\Protobuf\Internal\Message;
use SebastianBergmann\Exporter\Exporter;

class MessageAwareExporter extends Exporter
{
/**
* Exports a value into a single-line string
*
* @return string
*
* @see SebastianBergmann\Exporter\Exporter::export
*/
public function shortenedExport($value)
{
if (\is_object($value) && $value instanceof Message) {
return \sprintf(
'%s Object (%s)',
\get_class($value),
\spl_object_hash($value)
);
}
return parent::shortenedExport($value);
}
}
56 changes: 56 additions & 0 deletions src/ApiCore/Testing/ProtobufGPBEmptyComparator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\ApiCore\Testing;

use Google\Protobuf\GPBEmpty;
use SebastianBergmann\Comparator\Comparator;

class ProtobufGPBEmptyComparator extends Comparator
{
/**
* 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 GPBEmpty && $actual instanceof GPBEmpty;
}

/**
* 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)
{
// No need to do anything here.
}
}
76 changes: 76 additions & 0 deletions src/ApiCore/Testing/ProtobufMessageComparator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\ApiCore\Testing;

use Google\Protobuf\Internal\Message;
use SebastianBergmann\Comparator\Comparator;
use SebastianBergmann\Comparator\ComparisonFailure;
use SebastianBergmann\Exporter\Exporter;

class ProtobufMessageComparator extends Comparator
{
/** @var Exporter */
protected $exporter;

public function __construct()
{
parent::__construct();
$this->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'
);
}
}
}
31 changes: 31 additions & 0 deletions tests/ApiCore/Tests/Unit/MyMessage.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions tests/ApiCore/Tests/Unit/ProtobufBandaidTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\ApiCore\Tests\Unit;

use Google\ApiCore\Testing\GeneratedTest;

class ProtobufBandaidTest extends GeneratedTest
{
/**
* @dataProvider protobufMessageProvider
*/
public function testCompare($expected, $actual)
{
$this->assertEquals($expected, $actual);
}

public function protobufMessageProvider()
{
$msg1 = new MyMessage();
$msg2 = new Mymessage();
return [
[$msg1, $msg2],
[[$msg1, $msg2], [$msg1, $msg2]]
];
}
}
11 changes: 10 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,18 @@
* (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';

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());

0 comments on commit 02d5470

Please sign in to comment.