PHPUNIT how to get the current class into another file - phpunit

I have a common class A called by anothers php files.
And I want to get the name of current class into the class A.
Here class A :
class A extends PHPUnit_Framework_TestCase
{
............................
public static function setUpBeforeClass()
{
try {
**Here the function to get the class** ??????
self::$selenium = new ObjetConnexion();
self::$tab_castest= array();
self::$nbjeu = -1;
} catch (Testing_Selenium_Exception $e) {
echo $e;
}
}
}
Class B :
class BTest extends A
{
protected function setUp()
{
...
}
public function testConnexion()
{
......
}
}
And the content of the phpunit.xml :
<file>Nouveaux_services/BTest.php</file>
<file>Nouveaux_services/CTest.php</file>
<file>Nouveaux_services/DTest.php</file>
</testsuite>
thanks for your help

You can get the called class with get_called_class()
http://php.net/manual/en/function.get-called-class.php

Related

How to fix Avoid unused parameters PHPMD of protected method in parent class

I have two class :
class EntityVoter
{
protected function canPutToBlockChain($entity, $viewer)
{
return false;
}
}
class VerificationVoter extends EntityVoter
{
public function canPutToBlockChain($entity, $viewer)
{
return $this->canShare($entity, $viewer);
}
}
PHPMD scan EntityVoter class and throw: Avoid unused parameters such as '$entity', '$viewer'.
My solution is creating a interface:
interface EntityVoterInterface
{
function canPutToBlockChain($entity, $viewer);
}
and then add #inhericDoc annotation:
class EntityVoter implements EntityVoterInterface
{
/**
* #inheritDoc
*/
protected function canPutToBlockChain($entity, $viewer)
{
return false;
}
}
Is there any better solution ?

global variable in controller laravel 5.3

How can definition a global variable for use in all function controller
class TestController extends Controller
{
private $x;
public function index()
{
$this->$x ='22';
}
public function send_message()
{
echo $this->$x;
}
}
Write $this->x rather than $this->$x
class TestController extends Controller
{
private $x;
public function index()
{
$this->x ='22';
}
public function send_message()
{
echo $this->x;
}
}
If you want to make global variable in controller, the following code will work definitely:
private $x = 22;
public function index()
{
print_r($this->x);
die();
}

PHPUnit Mocking Wrapper Instances

I have this wrapper:
class APIWrapper {
protected static $clientClass = \Hardcoded_API; // from a library
protected $client;
public function __construct() {
$this->client = new self::$clientClass(array_merge(self::$config, $config));
}
/**
* Does something.
*
* #throws SomeException
*/
public function act() {
$this->client->doSomething();
}
public static function setClientClass($clientClass) {
self::$clientClass = $clientClass;
}
}
Now let's say I am testing functions that make use of this wrapper:
class UnrelatedFunction {
public function callAPI() {
$wrapper = new APIWrapper();
try {
$wrapper->act();
} catch (SomeException $e) {
// ... do other things
}
}
}
class UnrelatedFunctionsTest extends PHPUnit_Framework_TestCase {
public function testUnrelatedFunctionException() {
$unrelatedFunction = new UnrelatedFunction();
$this->setExpectedException('SomeException');
// this call triggers the APIWrapper
$unrelatedFunction->callAPI();
// Some assertions here...
}
}
I need to be able to verify that UnrelatedFunction::callAPI() did its job recovering from the exception. In this case, how can I mock a \Hardcoded_API class, make it throw a SomeException when act() is called, and pass it onto APIWrapper::setClientClass() so that I can test the behaviour?

Call Class in Flex 4

How to call a Class in a Function (Flex 4)?
Example:
public class A {
private function a();
private function b();
....
}
This is my question-->
private function loadMain() {
**call class A in here**
}
Who have any ideas ???
Thank for watching
Is this what you mean?
Class A:
public class A {
public function a();
public function b();
}
Function loadMain:
private function loadMain() {
var instance:A = new A();
instance.a();
instance.b();
}

Symfony2 + Twig: Translate label into a new twig extension

I have implemented a new twig extension and I have some text which had to be translated.
Unfortunately when I use a code label it appears as a sample text.
I mean when twig render this following extension, it displays: 5 entity.years instead of 5 years for example:
class MyExtension extends \Twig_Extension {
public function getFilters()
{
return array(
'myextension' => new \Twig_Filter_Method($this, 'myextension'),
);
}
public function myextension ($myId)
{
// ....
// Some operations concerning $myId...
// ....
if($myId!=0) {
$res = $myId. ' '.'entity.year';
} else {
$res = ($months == 0 ? $days.'entity.days' : $months.'entity.months');
}
return $res;
}
}
Where entity.years, entity.months, entity.days is defined into my translations folder.
Inject the translator service into your extension and use it. For example:
class MyExtension extends \Twig_Extension
{
private $translator;
public function __construct(Translator $translator)
{
$this->translator = $translator;
}
// ...
public function myMethod()
{
return $this->translator->trans('my_string');
}
}

Resources