2023-01-15 22:31:19 +01:00
< ? php
/**
* @ copyright Copyright ( C ) 2010 - 2023 , the Friendica project
*
* @ license GNU AGPL version 3 or any later version
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*
*/
namespace Friendica\Test\src\Core\Hooks\Model ;
use Dice\Dice ;
2023-07-17 00:10:15 +02:00
use Friendica\Core\Hooks\Exceptions\HookInstanceException ;
use Friendica\Core\Hooks\Exceptions\HookRegisterArgumentException ;
2023-07-02 23:56:56 +02:00
use Friendica\Core\Hooks\Model\DiceInstanceManager ;
2023-07-17 00:10:15 +02:00
use Friendica\Core\Hooks\Util\HookFileManager ;
2023-01-15 22:31:19 +01:00
use Friendica\Test\MockedTest ;
use Friendica\Test\Util\Hooks\InstanceMocks\FakeInstance ;
use Friendica\Test\Util\Hooks\InstanceMocks\FakeInstanceDecorator ;
use Friendica\Test\Util\Hooks\InstanceMocks\IAmADecoratedInterface ;
2023-07-17 00:10:15 +02:00
use Mockery\MockInterface ;
2023-01-15 22:31:19 +01:00
class InstanceManagerTest extends MockedTest
{
2023-07-17 00:10:15 +02:00
/** @var HookFileManager|MockInterface */
protected $hookFileManager ;
2023-01-15 22:31:19 +01:00
2023-07-17 00:10:15 +02:00
protected function setUp () : void
{
parent :: setUp ();
2023-01-15 22:31:19 +01:00
2023-07-17 00:10:15 +02:00
$this -> hookFileManager = \Mockery :: mock ( HookFileManager :: class );
$this -> hookFileManager -> shouldReceive ( 'setupHooks' ) -> withAnyArgs ();
2023-01-15 22:31:19 +01:00
}
protected function tearDown () : void
{
FakeInstanceDecorator :: $countInstance = 0 ;
parent :: tearDown ();
}
2023-07-17 00:10:15 +02:00
public function testEqualButNotSameInstance ()
{
$instance = new DiceInstanceManager ( new Dice (), $this -> hookFileManager );
$instance -> registerStrategy ( IAmADecoratedInterface :: class , FakeInstance :: class , 'fake' );
$getInstanceA = $instance -> create ( IAmADecoratedInterface :: class , 'fake' );
$getInstanceB = $instance -> create ( IAmADecoratedInterface :: class , 'fake' );
self :: assertEquals ( $getInstanceA , $getInstanceB );
self :: assertNotSame ( $getInstanceA , $getInstanceB );
}
2023-01-15 22:31:19 +01:00
public function dataTests () : array
{
return [
2023-01-16 22:25:26 +01:00
'only_a' => [
2023-01-15 22:31:19 +01:00
'aString' => 'test' ,
],
2023-01-16 22:25:26 +01:00
'a_b' => [
2023-01-15 22:31:19 +01:00
'aString' => 'test' ,
2023-01-16 22:25:26 +01:00
'cBool' => false ,
2023-01-15 22:31:19 +01:00
'bString' => 'test23' ,
],
2023-01-16 22:25:26 +01:00
'a_c' => [
2023-01-15 22:31:19 +01:00
'aString' => 'test' ,
'cBool' => false ,
'bString' => null ,
],
2023-01-16 22:25:26 +01:00
'a_b_c' => [
2023-01-15 22:31:19 +01:00
'aString' => 'test' ,
'cBool' => false ,
'bString' => 'test23' ,
],
2023-01-16 22:25:26 +01:00
'null' => [],
2023-01-15 22:31:19 +01:00
];
}
/**
* @ dataProvider dataTests
*/
2023-07-17 00:10:15 +02:00
public function testInstanceWithArgs ( string $aString = null , bool $cBool = null , string $bString = null )
2023-01-15 22:31:19 +01:00
{
2023-07-17 00:10:15 +02:00
$instance = new DiceInstanceManager ( new Dice (), $this -> hookFileManager );
2023-01-15 22:31:19 +01:00
$args = [];
if ( isset ( $aString )) {
$args [] = $aString ;
}
if ( isset ( $bString )) {
$args [] = $bString ;
}
if ( isset ( $cBool )) {
$args [] = $cBool ;
}
2023-07-17 00:10:15 +02:00
$instance -> registerStrategy ( IAmADecoratedInterface :: class , FakeInstance :: class , 'fake' );
2023-01-15 22:31:19 +01:00
/** @var IAmADecoratedInterface $getInstanceA */
2023-07-17 00:10:15 +02:00
$getInstanceA = $instance -> create ( IAmADecoratedInterface :: class , 'fake' , $args );
2023-01-15 22:31:19 +01:00
/** @var IAmADecoratedInterface $getInstanceB */
2023-07-17 00:10:15 +02:00
$getInstanceB = $instance -> create ( IAmADecoratedInterface :: class , 'fake' , $args );
2023-01-15 22:31:19 +01:00
self :: assertEquals ( $getInstanceA , $getInstanceB );
self :: assertNotSame ( $getInstanceA , $getInstanceB );
self :: assertEquals ( $aString , $getInstanceA -> getAText ());
self :: assertEquals ( $aString , $getInstanceB -> getAText ());
self :: assertEquals ( $bString , $getInstanceA -> getBText ());
self :: assertEquals ( $bString , $getInstanceB -> getBText ());
self :: assertEquals ( $cBool , $getInstanceA -> getCBool ());
self :: assertEquals ( $cBool , $getInstanceB -> getCBool ());
}
/**
* @ dataProvider dataTests
*/
2023-07-17 00:10:15 +02:00
public function testInstanceWithTwoStrategies ( string $aString = null , bool $cBool = null , string $bString = null )
2023-01-15 22:31:19 +01:00
{
2023-07-17 00:10:15 +02:00
$instance = new DiceInstanceManager ( new Dice (), $this -> hookFileManager );
2023-01-15 22:31:19 +01:00
$args = [];
if ( isset ( $aString )) {
$args [] = $aString ;
}
2023-07-17 00:10:15 +02:00
if ( isset ( $bString )) {
$args [] = $bString ;
}
2023-01-15 22:31:19 +01:00
if ( isset ( $cBool )) {
$args [] = $cBool ;
}
2023-07-17 00:10:15 +02:00
$instance -> registerStrategy ( IAmADecoratedInterface :: class , FakeInstance :: class , 'fake' );
$instance -> registerStrategy ( IAmADecoratedInterface :: class , FakeInstance :: class , 'fake23' );
2023-01-15 22:31:19 +01:00
/** @var IAmADecoratedInterface $getInstanceA */
2023-07-17 00:10:15 +02:00
$getInstanceA = $instance -> create ( IAmADecoratedInterface :: class , 'fake' , $args );
2023-01-15 22:31:19 +01:00
/** @var IAmADecoratedInterface $getInstanceB */
2023-07-17 00:10:15 +02:00
$getInstanceB = $instance -> create ( IAmADecoratedInterface :: class , 'fake23' , $args );
2023-01-15 22:31:19 +01:00
self :: assertEquals ( $getInstanceA , $getInstanceB );
self :: assertNotSame ( $getInstanceA , $getInstanceB );
self :: assertEquals ( $aString , $getInstanceA -> getAText ());
self :: assertEquals ( $aString , $getInstanceB -> getAText ());
self :: assertEquals ( $bString , $getInstanceA -> getBText ());
self :: assertEquals ( $bString , $getInstanceB -> getBText ());
self :: assertEquals ( $cBool , $getInstanceA -> getCBool ());
self :: assertEquals ( $cBool , $getInstanceB -> getCBool ());
}
/**
2023-07-17 00:10:15 +02:00
* Test the exception in case the interface was already registered
2023-01-15 22:31:19 +01:00
*/
2023-07-17 00:10:15 +02:00
public function testDoubleRegister ()
2023-01-15 22:31:19 +01:00
{
2023-07-17 00:10:15 +02:00
self :: expectException ( HookRegisterArgumentException :: class );
self :: expectExceptionMessage ( sprintf ( 'A class with the name %s is already set for the interface %s' , 'fake' , IAmADecoratedInterface :: class ));
2023-01-15 22:31:19 +01:00
2023-07-17 00:10:15 +02:00
$instance = new DiceInstanceManager ( new Dice (), $this -> hookFileManager );
$instance -> registerStrategy ( IAmADecoratedInterface :: class , FakeInstance :: class , 'fake' );
$instance -> registerStrategy ( IAmADecoratedInterface :: class , FakeInstance :: class , 'fake' );
}
2023-01-15 22:31:19 +01:00
2023-07-17 00:10:15 +02:00
/**
* Test the exception in case the name of the instance isn ' t registered
*/
public function testWrongInstanceName ()
{
self :: expectException ( HookInstanceException :: class );
self :: expectExceptionMessage ( sprintf ( 'The class with the name %s isn\'t registered for the class or interface %s' , 'fake' , IAmADecoratedInterface :: class ));
2023-01-15 22:31:19 +01:00
2023-07-17 00:10:15 +02:00
$instance = new DiceInstanceManager ( new Dice (), $this -> hookFileManager );
$instance -> create ( IAmADecoratedInterface :: class , 'fake' );
2023-01-15 22:31:19 +01:00
}
/**
2023-07-17 00:10:15 +02:00
* Test in case there are already some rules
*
2023-01-15 22:31:19 +01:00
* @ dataProvider dataTests
*/
2023-07-17 00:10:15 +02:00
public function testWithGivenRules ( string $aString = null , bool $cBool = null , string $bString = null )
2023-01-15 22:31:19 +01:00
{
$args = [];
if ( isset ( $aString )) {
$args [] = $aString ;
}
2023-07-17 00:10:15 +02:00
if ( isset ( $bString )) {
$args [] = $bString ;
2023-01-15 22:31:19 +01:00
}
2023-07-17 00:10:15 +02:00
$dice = ( new Dice ()) -> addRules ([
FakeInstance :: class => [
'constructParams' => $args ,
],
]);
2023-01-15 22:31:19 +01:00
$args = [];
if ( isset ( $cBool )) {
$args [] = $cBool ;
}
2023-07-17 00:10:15 +02:00
$instance = new DiceInstanceManager ( $dice , $this -> hookFileManager );
2023-01-15 22:31:19 +01:00
2023-07-17 00:10:15 +02:00
$instance -> registerStrategy ( IAmADecoratedInterface :: class , FakeInstance :: class , 'fake' );
2023-01-15 22:31:19 +01:00
/** @var IAmADecoratedInterface $getInstanceA */
2023-07-17 00:10:15 +02:00
$getInstanceA = $instance -> create ( IAmADecoratedInterface :: class , 'fake' , $args );
2023-01-15 22:31:19 +01:00
/** @var IAmADecoratedInterface $getInstanceB */
2023-07-17 00:10:15 +02:00
$getInstanceB = $instance -> create ( IAmADecoratedInterface :: class , 'fake' , $args );
2023-01-15 22:31:19 +01:00
self :: assertEquals ( $getInstanceA , $getInstanceB );
self :: assertNotSame ( $getInstanceA , $getInstanceB );
2023-07-17 00:10:15 +02:00
self :: assertEquals ( $aString , $getInstanceA -> getAText ());
self :: assertEquals ( $aString , $getInstanceB -> getAText ());
self :: assertEquals ( $bString , $getInstanceA -> getBText ());
self :: assertEquals ( $bString , $getInstanceB -> getBText ());
self :: assertEquals ( $cBool , $getInstanceA -> getCBool ());
self :: assertEquals ( $cBool , $getInstanceB -> getCBool ());
2023-01-15 22:31:19 +01:00
}
}