Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed RichTextTest so that tests fails if unexpected logs messages are created #75

Merged
merged 1 commit into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions tests/lib/FieldType/Converter/MethodCallCountConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* This file is part of the eZ Platform XmlText Field Type package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzPlatformXmlTextFieldType\Tests\FieldType\Converter;

use PHPUnit_Framework_MockObject_Invocation;
use PHPUnit_Framework_MockObject_Matcher_InvokedRecorder;
use PHPUnit_Framework_ExpectationFailedException;

/**
* Class MethodCallCountConstraint.
*
* When generating mocks in PHPUnit, you cannot validate that the mock methods are called the correct
* number of times.
*
* However, with this constrain you may.
* To ensure foobarMethod() is not called more than twice:
* $mock->expects(new MethodCallCountConstraint(2))->method('foobarMethod);
*
* Based on workaround proposed on https://github.com/sebastianbergmann/phpunit-mock-objects/issues/65
*/
class MethodCallCountConstraint extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
{
/**
* @var int
*/
protected $expectedCount;

/**
* @param int $expectedCount
*/
public function __construct($expectedCount)
{
$this->expectedCount = $expectedCount;
}

/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
* @return mixed|void
* @throws PHPUnit_Framework_ExpectationFailedException
*/
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
{
parent::invoked($invocation);

$count = $this->getInvocationCount();

if ($count > $this->expectedCount) {
$message = 'Call to ' . $invocation->toString() . ' unexpected';

throw new PHPUnit_Framework_ExpectationFailedException($message);
}
}

/**
* Returns a string representation of the object.
*
* @return string
*/
public function toString()
{
return 'MethodCallCountConstraint';
}

/**
* Verifies that the current expectation is valid. If everything is OK the
* code should just return, if not it must throw an exception.
*
* @throws PHPUnit_Framework_ExpectationFailedException
*/
public function verify()
{
$count = $this->getInvocationCount();
if ($count != $this->expectedCount) {
throw new PHPUnit_Framework_ExpectationFailedException(
sprintf(
'Methods of class was expected to be called %d times, ' .
'actually called %d times.',

$this->expectedCount,
$count
)
);
}
}
}
10 changes: 10 additions & 0 deletions tests/lib/FieldType/Converter/RichTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@

class RichTextTest extends TestCase
{
/**
* @var \EzSystems\EzPlatformXmlTextFieldType\Tests\FieldType\Converter\MethodCallCountConstraint
*/
protected $methodCallCountConstraint = null;

/**
* @param string $xml
* @param bool $isPath
Expand Down Expand Up @@ -173,6 +178,7 @@ private function createApiRepositoryStub()
private function createLoggerStub($logFilePath)
{
$loggerStub = $this->createMock(NullLogger::class);
$callLogCount = 0;

if ($logFilePath !== null) {
$log = file_get_contents($logFilePath);
Expand All @@ -199,8 +205,11 @@ private function createLoggerStub($logFilePath)
->method('log')
->with($logLevel, $logMessage);
}
++$callLogCount;
}
}
$this->methodCallCountConstraint = new MethodCallCountConstraint($callLogCount);
$loggerStub->expects($this->methodCallCountConstraint)->method('log');

return $loggerStub;
}
Expand All @@ -221,6 +230,7 @@ public function testConvert($inputFilePath, $outputFilePath, $logFilePath)
$richText->setImageContentTypes([27]);

$result = $richText->convert($inputDocument, true, true);
$this->methodCallCountConstraint->verify();

if ($result === false && !file_exists($outputFilePath)) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
warning:Duplicated id in original ezxmltext for contentobject_attribute.id=[unknown], automatically generated new id : myembed_id --> duplicated_id_myembed_id_idm*
warning:Duplicated id in original ezxmltext for contentobject_attribute.id=[unknown], automatically generated new id : myembed_id --> duplicated_id_myembed_id_idm*
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
warning:Duplicated id in original ezxmltext for contentobject_attribute.id=[unknown], automatically generated new id : anchor --> duplicated_id_anchor_idm*
warning:Duplicated id in original ezxmltext for contentobject_attribute.id=[unknown], automatically generated new id : anchor --> duplicated_id_anchor_idm*
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
notice:Found embed(s) inside header tag. Embed(s) where moved outside header where contentobject_attribute.id=
notice:Found embed(s) inside header tag. Embed(s) where moved outside header where contentobject_attribute.id=
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
error:Only literal tag with class="html" supported at the moment. Tag with different class removed where contentobject_attribute.id=
error:Only literal tag with class="html" supported at the moment. Tag with different class removed where contentobject_attribute.id=
error:Only literal tag with class="html" supported at the moment. Tag with different class removed where contentobject_attribute.id=
error:Only literal tag with class="html" supported at the moment. Tag with different class removed where contentobject_attribute.id=