Skip to content

Commit

Permalink
Fixed RichTextTest so that tests fails if unexpected logs messages ar…
Browse files Browse the repository at this point in the history
…e created (ezsystems#75)
  • Loading branch information
vidarl committed Sep 24, 2018
1 parent 129e65b commit 12bc4f6
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
91 changes: 91 additions & 0 deletions tests/lib/FieldType/Converter/MethodCallCountConstraint.php
@@ -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
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
@@ -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*
@@ -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*
@@ -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=
@@ -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=

0 comments on commit 12bc4f6

Please sign in to comment.