Skip to content

Commit

Permalink
fixed : compatibility of setUp() with PHP <=7.0
Browse files Browse the repository at this point in the history
The old version of PHP did not support `void` as a type hint. Newer version of PHPUnit requires `void` as a type hint for setUp(). We are in an impasse using this method.
  • Loading branch information
encreinformatique authored and pkruithof committed Nov 28, 2022
1 parent e18ff11 commit 8ceda09
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
8 changes: 7 additions & 1 deletion Tests/Document/ThreadDenormalizerTest.php
Expand Up @@ -12,7 +12,11 @@ class ThreadDenormalizerTest extends TestCase
{
protected $dates;

protected function setUp(): void
/**
* This method should be setUp(): void
* For compatibility reasons with old versions of PHP, we cannot use neither setUp(): void nor setUp().
*/
protected function setUpBeforeTest()
{
$this->markTestIncomplete('Broken, needs to be fixed');

Expand All @@ -26,6 +30,8 @@ protected function setUp(): void

public function testDenormalize()
{
$this->setUpBeforeTest();

$thread = new TestThread();
$user1 = $this->createParticipantMock('u1');
$user2 = $this->createParticipantMock('u2');
Expand Down
16 changes: 15 additions & 1 deletion Tests/EntityManager/ThreadManagerTest.php
Expand Up @@ -16,7 +16,11 @@ class ThreadManagerTest extends TestCase
protected $user;
protected $date;

public function setUp(): void
/**
* This method should be setUp(): void
* For compatibility reasons with old versions of PHP, we cannot use neither setUp(): void nor setUp().
*/
public function setUpBeforeTest()
{
$this->user = $this->createParticipantMock('4711');
$this->date = new \DateTime('2013-12-25');
Expand All @@ -27,6 +31,8 @@ public function setUp(): void
*/
public function testDoCreatedByAndAt()
{
$this->setUpBeforeTest();

$thread = $this->createThreadMock();
$thread->expects($this->exactly(1))->method('getFirstMessage')
->will($this->returnValue($this->createMessageMock()));
Expand All @@ -40,6 +46,8 @@ public function testDoCreatedByAndAt()
*/
public function testDoCreatedByAndAtWithCreatedBy()
{
$this->setUpBeforeTest();

$thread = $this->createThreadMock();

$thread->expects($this->exactly(0))->method('setCreatedBy');
Expand All @@ -59,6 +67,8 @@ public function testDoCreatedByAndAtWithCreatedBy()
*/
public function testDoCreatedByAndAtWithCreatedAt()
{
$this->setUpBeforeTest();

$thread = $this->createThreadMock();

$thread->expects($this->exactly(1))->method('setCreatedBy');
Expand All @@ -78,6 +88,8 @@ public function testDoCreatedByAndAtWithCreatedAt()
*/
public function testDoCreatedByAndAtWithCreatedAtAndBy()
{
$this->setUpBeforeTest();

$thread = $this->createThreadMock();
$thread->expects($this->exactly(0))->method('setCreatedBy');
$thread->expects($this->exactly(0))->method('setCreatedAt');
Expand All @@ -99,6 +111,8 @@ public function testDoCreatedByAndAtWithCreatedAtAndBy()
*/
public function testDoCreatedByAndNoMessage()
{
$this->setUpBeforeTest();

$thread = $this->createThreadMock();
$thread->expects($this->exactly(0))->method('setCreatedBy');
$thread->expects($this->exactly(0))->method('setCreatedAt');
Expand Down
24 changes: 23 additions & 1 deletion Tests/Twig/Extension/MessageExtensionTest.php
Expand Up @@ -16,7 +16,11 @@ class MessageExtensionTest extends TestCase
private $authorizer;
private $participant;

public function setUp(): void
/**
* This method should be setUp(): void
* For compatibility reasons with old versions of PHP, we cannot use neither setUp(): void nor setUp().
*/
public function setUpBeforeTest()
{
$this->participantProvider = $this->getMockBuilder('FOS\MessageBundle\Security\ParticipantProviderInterface')->getMock();
$this->provider = $this->getMockBuilder('FOS\MessageBundle\Provider\ProviderInterface')->getMock();
Expand All @@ -27,6 +31,8 @@ public function setUp(): void

public function testIsReadReturnsTrueWhenRead()
{
$this->setUpBeforeTest();

$this->participantProvider->expects($this->once())->method('getAuthenticatedParticipant')->will($this->returnValue($this->participant));
$readAble = $this->getMockBuilder('FOS\MessageBundle\Model\ReadableInterface')->getMock();
$readAble->expects($this->once())->method('isReadByParticipant')->with($this->participant)->will($this->returnValue(true));
Expand All @@ -35,6 +41,8 @@ public function testIsReadReturnsTrueWhenRead()

public function testIsReadReturnsFalseWhenNotRead()
{
$this->setUpBeforeTest();

$this->participantProvider->expects($this->once())->method('getAuthenticatedParticipant')->will($this->returnValue($this->participant));
$readAble = $this->getMockBuilder('FOS\MessageBundle\Model\ReadableInterface')->getMock();
$readAble->expects($this->once())->method('isReadByParticipant')->with($this->participant)->will($this->returnValue(false));
Expand All @@ -43,20 +51,26 @@ public function testIsReadReturnsFalseWhenNotRead()

public function testCanDeleteThreadWhenHasPermission()
{
$this->setUpBeforeTest();

$thread = $this->getThreadMock();
$this->authorizer->expects($this->once())->method('canDeleteThread')->with($thread)->will($this->returnValue(true));
$this->assertTrue($this->extension->canDeleteThread($thread));
}

public function testCanDeleteThreadWhenNoPermission()
{
$this->setUpBeforeTest();

$thread = $this->getThreadMock();
$this->authorizer->expects($this->once())->method('canDeleteThread')->with($thread)->will($this->returnValue(false));
$this->assertFalse($this->extension->canDeleteThread($thread));
}

public function testIsThreadDeletedByParticipantWhenDeleted()
{
$this->setUpBeforeTest();

$thread = $this->getThreadMock();
$this->participantProvider->expects($this->once())->method('getAuthenticatedParticipant')->will($this->returnValue($this->participant));
$thread->expects($this->once())->method('isDeletedByParticipant')->with($this->participant)->will($this->returnValue(true));
Expand All @@ -65,19 +79,25 @@ public function testIsThreadDeletedByParticipantWhenDeleted()

public function testGetNbUnreadCacheStartsEmpty()
{
$this->setUpBeforeTest();

$this->assertAttributeEmpty('nbUnreadMessagesCache', $this->extension);
$this->extension->getNbUnread();
}

public function testGetNbUnread()
{
$this->setUpBeforeTest();

$this->assertAttributeEmpty('nbUnreadMessagesCache', $this->extension);
$this->provider->expects($this->once())->method('getNbUnreadMessages')->will($this->returnValue(3));
$this->assertEquals(3, $this->extension->getNbUnread());
}

public function testGetNbUnreadStoresCache()
{
$this->setUpBeforeTest();

$this->provider->expects($this->once())->method('getNbUnreadMessages')->will($this->returnValue(3));
//we call it twice but expect to only get one call
$this->extension->getNbUnread();
Expand All @@ -86,6 +106,8 @@ public function testGetNbUnreadStoresCache()

public function testGetName()
{
$this->setUpBeforeTest();

$this->assertEquals('fos_message', $this->extension->getName());
}

Expand Down

0 comments on commit 8ceda09

Please sign in to comment.