Getting started with Enhance PHP

I want to include a testing platform in the project I am creating and have come across Enhance PHP , which I like, but I have some difficulties finding relevant information online, because “php enhancement” is such a frequently used phrase.

Has anyone worked with this framework that could point me to some useful reference? Have you worked with the unit test framework, which, in your opinion, is amazingly better?

Thanks in advance.

In response to Gotzofter, this class is being tested:

<?php include_once('EnhanceTestFramework.php'); class ExampleClass { private $OtherClass; function __construct($mock = null) { if ($mock == null) $this->OtherClass = new OtherExampleClass(); else $this->OtherClass = $mock; } public function doSomething() { return $this->OtherClass->getSomething(1, 'Arg2'); } } class OtherExampleClass { public function getSomething() { return "Something"; } } class ExampleClassTests extends \Enhance\TestFixture { public function setUp() { } public function tearDown() { } public function verifyWithAMock() { $mock = \Enhance\MockFactory::createMock('OtherExampleClass'); $mock->addExpectation( \Enhance\Expect::method('getSomething') ->with(1, 'Arg2') ->returns('Something') ->times(1) ); $target = new ExampleClass($mock); $result = $target->doSomething(); \Enhance\Assert::areIdentical("Something", $result); $mock->verifyExpectations(); } } \Enhance\Core::runTests(); 

Take a look at my constructor for ExampleClass.

Due to the php-site website embedding the $ mock object by calling the new ExampleClass ($ mock), I have to change my ExampleClass constructor to handle $ mock as an input parameter.

Do I have to handle this for all classes that I want to unit test with a framework?

Thanks.

+6
source share
3 answers

It:

 function __construct() { $this->OtherClass = new OtherExampleClass; } 

Must be:

 function __construct($otherClass) { $this->OtherClass = $otherClass; } 

Your layout is never entered at this point in your test:

  $target = new ExampleClass($mock); 
+3
source

One thing that I would recommend no matter what test structure you use is a hint type against the expected class or interface.

 <?php class ExampleClass { private $OtherClass; // OtherClass instance public function __construct(OtherClass $OtherClass=null) { // ... } } 

I am not an expert, but I see no problem with each class invoking a new one if the instance is not provided for a specific dependency. Of course, you can also use an approach in which you use configuration methods to configure dependencies.

 <?php class class ExampleClass { private $OtherClass; // OtherClass instance public function setOtherClass(OtherClass $OtherClass) { $this->OtherClass = $OtherClass; } } 

It’s not enough that the ExampleClass in the sample code doesn’t even define the doSomething method from ExampleDependencyClassTests , but if I understand correctly, it looks like Improving PHP does not force you to adopt a certain dependency injection style. You can write the test class as you wish, for example, if you applied the setter method approach mentioned above, you could change the example code layout to

 <?php class ExampleDependencyClassTests extends \Enhance\TestFixture { public function verifyWithAMock() { $mock = \Enhance\MockFactory::createMock('ExampleDependencyClass'); $mock->addExpectation( \Enhance\Expect::method('getSomething') ->with(1, 'Arg2') ->returns('Something') ->times(1) ); $target = new ExampleClass(); $target->setExampleDependencyClass($mock); $result = $target->doSomething(); $mock->verifyExpectations(); } } 

Of course, it would be wise to make appropriate changes to the ExampleClass !

 <?php class ExampleClass { private $ExampleDependencyClass; public function addTwoNumbers($a, $b) { return $a + $b; } public function setExampleDependencyClass( ExampleDependencyClass $ExampleDependecyClass ) { $this->ExampleDependecyClass = $ExampleDependecyClass; } public function doSomething($someArg) { return 'Something'; } } 

I have been working with PHPUnit quite a bit, and frankly, you will have to face the same problems with Mocks. My 2 cents, try to simulate your tests without Mocks, if possible;)

+1
source

There is a NetTuts guide called Testing your PHP code with Enhance PHP , which will definitely help you get started.

And there is a Quick Guide to PHP Extension .

0
source

Source: https://habr.com/ru/post/903145/


All Articles