PHPUnit - implementing abstract method in child - php

I have a set of read-only tests and a couple of ones that modify data (insert/update/delete). I'd like to back up my tables, so each test class would have a list of associated tables that they'll modify. This is just test data.
I thus thought of this:
abstract class DataAlteringTestBase extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
echo "backing up tables: " . $this->GetAlteredTableNames();
}
public abstract function GetAlteredTaleNames();
}
One of the subclasses:
class DataAlteringTest extends DataAlteringTestBase
{
function GetAlteredTaleNames()
{
return array("some_table");
}
public function testDummyStuffChild()
{
$this->assertTrue(true);
}
}
The problem is, I think, that PHPUnit tries to get the method implementation from the abstract class, rather than its children.
Call to undefined method DataAlteringTest::GetAlteredTableNames() -
the implementation ...\tests\DataAlteringTestBase.php:6 - the abstract
class
How to fix it? or is there something wrong with the idea of implementing this in PHP/PHPUnit in the first place?

You have a few typos - you wrote GetAlteredTaleNames() in some places and GetAlteredTableNames() in other places.

Related

How can I have an optional abstract method to avoid empty functions?

I currently have an abstract class which i am extending to other controllers. I have a abstract function within the abstract class which takes the value and places it in the __construct.
abstract class Controller extends BaseController {
abstract public function something();
public function __construct(Request $request) {
if (!is_null($this->something())){
$this->global_constructor_usse = $this->something();
}
}
}
My problem is that, on controllers that don't require this abstract function, I am having to place in the empty function.
class ControllerExample extends Controller {
public function something(){
return 'somethinghere';
}
}
Is there anyway to making the abstract function optional, or have a default value?
class EmptyControllerExample extends Controller {
public function something(){}
}
It is not possible to have a abstract method optional, as it is implied in PHP that all abstract methods must have an implementation.
There are legit use cases for optional abstract methods, yes: event handlers, metadata describers, etc. Unfortunately, you'll need to use regular, non-abstract methods with an empty body, and indicate in PHPDoc that they will do nothing unless extended.
Be wary, though: this can very quickly turn into code smell by diffusing a class responsability with their children. If you're dealing with generic events, you can look into Laravel's own event system, or the Observer pattern instead.
Abstract functions in a parent class, should only be used if its required by your application to implement the following method in all controllers who inherits from it, clearly it is not the case.
In this case i would make a trait. Here you create a trait which can be implemented by the classes who needs it. Notice the use keyword usage, use somethingTrait;
trait SomethingTrait
{
public function something()
{
echo "something called";
}
}
class Controller
{
use SomethingTrait;
public function run()
{
$this->something();
}
}
phpfiddle link
Another aproach could be doing a class inheritance structure, if the controllers you want to implement the methods has something in common. Where you would implement your special method in CrmController, where you still would be able to create shared methods in the abstract controller.
AbstractController
|
CrmController
|
CompanyController
For your question, 'Is there anyway to making the abstract function optional or have a default value?' No, and you are down the wrong path if you are trying to make abstract function optional. Hope my suggestions can help.

Multi-level inheritance in PHP classes

Lets say I have this code:
class A {
...
}
class B extends A {
...
}
class C extends B {
...
}
$c = new C();
$c->getMethodOrPropertyFromB()->getMethodOrPropertyFromA();
Other than a bad architecture or bad design will this have any impact on PHP / Webserver (Apache/Nginx) performance when the script executes?
If this is not recommended to have such multi-level in PHP classes, can you explain why?
Note: in addition to the answers I've got I will let this here which is helpful as well
My initial thought was that this may not be good for inheritance but after testing it seems okay. However, there are other means of accomplishing this you could be aware of.
Abstract classes or Interfaces may make sense.
Abstract classes are just like other classes but they cannot be instantiated. There are also abstract methods which must be implemented by concrete classes.
abstract class A {
//You can also have abstract methods
abstract public function doFoo();
abstract public function doBar($when);
//Also implemented method which when
//called unless overridden will use this logic
public function sayHi(){
echo "hi";
}
}
Now this class can choose to implement the abstract methods or not also adding any further logic needed by it.
abstract class B extends A {
public function doFoo(){
//Some code
}
abstract public function doFooBar();
public function sayBye(){
echo "bye";
}
}
This is a concrete class and all abstract methods must be implemented here if not already ones that are implemented can be overridden yet again.
class C extends B {
public function doFoo(){
//Some different code
}
public function doBar($when){
//Some code
}
public function doFooBar(){
//Some code
}
//do not need sayHi() and sayBye() but they will be available.
}
Interface in a simple and crude way is a bag of methods. You are simply telling the developer if you are going to use this implement these. These methods are not declared as abstract but cannot be implemented in the interface.
interface iA {
public function doFoo();
public function doBar();
}
An interface can be extended by other interface which is just adding more methods to the interface
interface iB extends iA {
public function doFooBar();
}
interface iC {
public function doAnything();
}
And implemented by classes
class A implements iA{
public function doFoo(){
//Some Code
}
public function doBar(){
//Some Code
}
}
class B implements iB{
public function doFoo(){
//Some Code
}
public function doBar(){
//Some Code
}
public function doFooBar(){
//Some Code
}
}
The added advantage of interfaces is that a class or abstract can implement more then one
abstract class C implements iA, iC {
public function doFoo(){
//Some Code
}
}
class D extends C {
//get doFoo() from C implementation and must implement the remaining...
public function doBar(){
//Some Code
}
public function doAnything(){
//Some Code
}
}
This seems perfectly fine. PHP only support single inheritance - so you can only inherit from one class.
If you need more functionality in a class but cannot get the functality in a parent class you can also consider using traits. Traits try to solve the single inheritance problem - even if it's not a problem per se.
If you properly build your classes you get a nice chain of inheritance which does not have any bad influences onto Apache/Nginx.

How to access class members in traits (or get a similar behaviour)?

This is a follow-up to my previous question about resolving the diamond issue in php.
As I state in that question, I resolve my problem by using traits and passing the instance of the class to the method of the trait. Such as:
trait SecurityTrait
{
public function beforeExecuteRouteTrait($controller, Dispatcher $dispatcher)
{
// Do something that makes use of methods/members of the controller
}
}
class AppController extends Controller
{
use SecurityTrait;
public function beforeExecuteRoute(Dispatcher $dispatcher)
{
return $this->beforeExecuteRouteTrait($this, $dispatcher);
}
}
However, I am still uncomfortable with this as I don't think this is how traits are really supposed to be used. In my reading I haven't found any way in which to access class members in traits (make $this inside a trait refer to the class using it). Is this possible? Or is there another way to implement a similar behaviour?
After reading some of the answers...
Previously I thought I had received errors when using $this->... inside the trait and this led me to believe the trait could not access anything to do with the underlying class. After reading the answers I tried altering my code to use $this->... inside a trait again and it works - which means a typo several weeks ago has given me far too much headache...
The example given previously now looks like this
trait SecurityTrait
{
public function beforeExecuteRoute(Dispatcher $dispatcher)
{
// Do something that makes use of methods/members of the controller
}
}
class AppController extends Controller
{
use SecurityTrait;
}
Much cleaner and more easily understandable but provides the same functionality.
If you use a trait inside a class then that trait has full access to all class's members and vice versa - you can call private trait methods from the class itself.
Think of traits as code that literally gets copy/pasted into the class body.
For example:
trait Helper
{
public function getName()
{
return $this->name;
}
private function getClassName()
{
return get_class($this);
}
}
class Example
{
use Helper;
private $name = 'example';
public function callPrivateMethod()
{
// call a private method on a trait
return $this->getClassName();
}
}
$e = new Example();
print $e->getName(); // results in "example"
print $e->callPrivateMethod(); // results in "Example"
In my view referencing classes in traits is not the best way to use them but there's nothing stopping anyone from doing it.
No, that's exactly what Traits are for. Your class already extends a class so you can't inherit the methods and variables of any other classes.
Think of a Trait like copy/paste for code execution. When a class includes a Trait, it's just as if you had written all that code into the class itself.

Is it compulsory to declare abstract class for abstract method?

I have a below program
<?php
abstract class foo
{
abstract public function callme();
public function testing()
{
return $this->callme();
}
}
class bar extends foo
{
public function callme()
{
return "hello";
}
}
$objBar = new bar();
echo $objBar->testing();
?>
I defined abstract class foo. Is it compulsory to write abstract before class ? Because if i removed abstract i am getting fatal error.
Yes, if it contains abstract methods.
By declaring a method as abstract you are saying that in order to use this class, extending classes must implement the abstract method.
Your foo class cannot be instantiated unless callme is implemented, hence it must be declared abstract.
These concepts are perhaps better explained with a real world example than your standard abstract class Vehicle, class Car extends Vehicle tutorials.
Let's say we have a reporting system that does some querying on the database.
We find that all reports must be implemented in a standard way to share code and help with future maintenance.
So we define:
abstract class Report
{
}
For the sake of argument, all of our reports require a database connection.
abstract class Report
{
/** #var PDO */
protected $dbh;
public function __construct (PDO $dbh)
{
$this->dbh = $dbh;
}
/**
* #return array
*/
abstract public function getData();
}
Here we have also decided that all of our reports must implement a public getData method that returns an array.
This means:
We can ensure all our reports have a database connection
We can instantiate and then run each report in the same way
The abstract class definition has enforced the way we consume this code and makes sure that every type of report, regardless of which developer on your team wrote it, conforms to the convention we have decided.
Other code is then able to select a report from user input, run it, and do something with the result of getData (such as writing it to a CSV file) knowing that it will be an array.

PHPUnit: Testdependencies with test from parentclass

So, I've got a little problem with my Unittests. I wrote some basisclasses for different Testcases and I want to uses some prepared test-methods.
i.e.
class ModelTestCase extends PHPUnit_Framework_TestCase {
public function testCreateInstance() { ... }
}
class UserModelTest extends ModelTestCase {
/**
* (at)depends testCreateInstance
*/
public funcion testWhatever($model) { ...}
}
Is there any trick to use it as I want or must I really write every test in every class?
It all depends on what you really want to do, your code sample is way too vague to tell that.
One option for you is to create your own setup() method in ModelTestCase (dont forget to call parent::setUp()) and do some initialization in there.
If you want to test only the derived model tests, but not the base class itself, you can declare it as abstract:
abstract class ModelTestCase extends PHPUnit_Framework_TestCase {
public function testCreateInstance() { ... }
}
This worked for me.

Categories