From 8ceda09d7fc8fe4d3933cb12cdefe6feb76e086d Mon Sep 17 00:00:00 2001 From: Julien Devergnies Date: Wed, 10 Mar 2021 11:47:45 +0100 Subject: [PATCH] fixed : compatibility of setUp() with PHP <=7.0 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. --- Tests/Document/ThreadDenormalizerTest.php | 8 ++++++- Tests/EntityManager/ThreadManagerTest.php | 16 ++++++++++++- Tests/Twig/Extension/MessageExtensionTest.php | 24 ++++++++++++++++++- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/Tests/Document/ThreadDenormalizerTest.php b/Tests/Document/ThreadDenormalizerTest.php index e4e43001..0196b206 100644 --- a/Tests/Document/ThreadDenormalizerTest.php +++ b/Tests/Document/ThreadDenormalizerTest.php @@ -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'); @@ -26,6 +30,8 @@ protected function setUp(): void public function testDenormalize() { + $this->setUpBeforeTest(); + $thread = new TestThread(); $user1 = $this->createParticipantMock('u1'); $user2 = $this->createParticipantMock('u2'); diff --git a/Tests/EntityManager/ThreadManagerTest.php b/Tests/EntityManager/ThreadManagerTest.php index 3e237653..2f20b3a2 100644 --- a/Tests/EntityManager/ThreadManagerTest.php +++ b/Tests/EntityManager/ThreadManagerTest.php @@ -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'); @@ -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())); @@ -40,6 +46,8 @@ public function testDoCreatedByAndAt() */ public function testDoCreatedByAndAtWithCreatedBy() { + $this->setUpBeforeTest(); + $thread = $this->createThreadMock(); $thread->expects($this->exactly(0))->method('setCreatedBy'); @@ -59,6 +67,8 @@ public function testDoCreatedByAndAtWithCreatedBy() */ public function testDoCreatedByAndAtWithCreatedAt() { + $this->setUpBeforeTest(); + $thread = $this->createThreadMock(); $thread->expects($this->exactly(1))->method('setCreatedBy'); @@ -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'); @@ -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'); diff --git a/Tests/Twig/Extension/MessageExtensionTest.php b/Tests/Twig/Extension/MessageExtensionTest.php index 61fe82a0..164ad3c1 100644 --- a/Tests/Twig/Extension/MessageExtensionTest.php +++ b/Tests/Twig/Extension/MessageExtensionTest.php @@ -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(); @@ -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)); @@ -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)); @@ -43,6 +51,8 @@ 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)); @@ -50,6 +60,8 @@ public function testCanDeleteThreadWhenHasPermission() 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)); @@ -57,6 +69,8 @@ public function testCanDeleteThreadWhenNoPermission() 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)); @@ -65,12 +79,16 @@ 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()); @@ -78,6 +96,8 @@ public function testGetNbUnread() 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(); @@ -86,6 +106,8 @@ public function testGetNbUnreadStoresCache() public function testGetName() { + $this->setUpBeforeTest(); + $this->assertEquals('fos_message', $this->extension->getName()); }