I have a scenario that I wanted to 'derequire' or just the require_once needs to be on that class only.
I learned that in PHPUnit the test files are gonna be included. And because of that, the required_once is overriding the class that I needed. I needed to use the original class. Is it possible?
PHPUnit: include class after mocking it
Related
I am having difficulty getting psysh to instantiate classes.
I am trying to use PSR-4 namespaces, and have registered a psr-4 autoload in composer like this:
"autoload": {
"psr-4": {
"System\\": "phpclasses/"
}
},
There is a class in phpclasses\Test.php, class name Test with a static method called hello().
I open a command shell, start psysh, and psysh appears to be working normally.
If I try to run Test::hello(); it will fail, unless I call it like this first: echo System\Test::hello();
This actually fails with the message:
PHP Fatal error: Class 'System\Test' not found in eval()'d code on line 1
but then I can successfully run: echo Test::hello();
echo System\Test::hello(); will never work
I tried Use System; and use System\Test; has no beneficial effect.
Every class I use, I have to go through this routine, which is kind of a drag because some of the classes uses static methods, and each of those will only work if each class has gone through that fail first routine.
Basically the same technique must be used for static or non-static methods.
I am running psysh in a command shell in windows 10, xampp (php 5.6), composer (current) installed.
Any suggestions for what I am doing wrong or need to do differently?
The trouble here is that you're not following PSR-4. With the config you provided, it's expecting to find classes in the System namespace inside your phpclasses folder. So, for example, the file Test.php would have the class System\Test.
To just fix it, either change the prefix in your autoload settings to "", or add namespace System; to your Test.php file. If you want to understand why it's acting like it is, you have to understand a bit about how autoloading works in PHP:
PHP lets you register an autoloader to find classes which haven't been encountered yet. The autoloader is handed a class name, and given a chance to find it. Usually they work by mapping class names to files in some way. When they're asked for an unknown class, they translate the class name to a file name, and try to require the file.
PSR-4 is a standard for setting up such an autoloader, and Composer comes with a PSR-4 compliant autoloader for free. For it to work right, you have to lay out your classes and namespaces like PSR-4 expects. If you don't, you can run into strange issues like you're encountering.
When you first tried calling Test::hello(), the class wasn't defined. Your PSR-4 autoloader translated that to a file name, but per your config, there's nowhere defined for non-namespaced classes to live, so it couldn't find a file to load, and it ended up loading nothing. After the autoloader had a chance, PHP still didn't know about that class, so it threw an error.
When you tried calling System\Test::hello(), your PSR-4 autoloader looked it up in the config and translated it to a filename (phpclasses/Test.php), which did exist this time, so it loaded that file. PHP then tried calling the method, but it didn't know about that class, so it threw an error.
The third time, it had already loaded your file and discovered the non-namespaced Test class. So when you tried calling it again, it didn't even bother with the autoloader, and just executed your method.
I have following problem with setting up my phpunit tests. The project I'm working on has different class naming convetion, for example - class name is cMailing and the name of that file is class_mailing.php. How to automate phpunit lookup for that class in my tests?
Use bootstrap file for autoloding your classes.
http://phpunit.de/manual/3.7/en/textui.html
http://jes.st/2011/phpunit-bootstrap-and-autoloading-classes/
Isn't PHPUnit supposed to require the class itself using the naming conventiong "ClassTest"?
I have a php file "Router.php" with a class Router
My PHPUnit testing class is defined as following:
class RouterTest extends PHPUnit_Framework_TestCase
However it raises a fatal error for file not found "Router.php".
If I require it manually everything is ok.
What's wrong with it?
There is nothing wrong with it. How would PHPUnit know where your classes are in relation from the tests?
For your tests, you need to either require the class under test yourself or create an autoloader that will require the class for you. PHPUnit has no idea where it should be getting the files from. It has an autoloader for its internal files but that is it.
I have few PHP classes and files that I want to include in Joomla so that I can call those classes. I tried doing require_once config.php to include a PHP file which also includes all the classes that I would want to use in Joomla.
But each time I execute the page I receive the error below:
Fatal error: Class 'SomeClassName' not found
is there any other way to include external PHP classes or files in Joomla?
Thanks in advance!
Please use Joomla autoloader. Is better.
<?php
// Register an adhoc class.
JLoader::register('AdhocClass', '/the/path/adhoc.php');
// Register a custom class to override as core class.
// This must be done before the core class is loaded.
JLoader::register('JDatabase', '/custom/path/database_driver.php', true);
Edit:
Load classes with autoload instead of require/include have a better performance, because PHP will only read (require access to the disk) and compile (require memory and CPU usage) if you really use your class.
To do the same with require/include you have to be sure to only use if will really use the class.
Source:
http://developer.joomla.org/manual/ch01s04.html
require_once should work just fine within Joomla. Make sure the class you want to use is really loaded within your file, and the file is properly referenced in the require_once. Something is going wrong there and it has nothing to do with Joomla itself :-)
I'm just starting to try out phpunit on some existing code. The naming convention we use is that the MyClass class should be in MyClass.class.php. PHPUnit seems to require that the file should be called MyClass.php.
Is there any way around this?
I noticed it while trying to generate a skeleton test class:
phpunit --skeleton-test MyClass.class
PHPUnit 3.3.4 by Sebastian Bergmann.
Could not find class "MyClass.class" in "/home/jd/skeleton/classes/MyClass.class.php".
Fatal error: Call to a member function getOutClassName() on a non-object in /usr/share/php/PHPUnit/TextUI/Command.php on line 470
Its not a requirement, its just assumptive. You can write your own test-cases.
Skeleton just makes a mock-up one "the easy way" that makes dummy functions for all your classes dummy functions.
Also,
phpunit --skeleton-test MyClass MyClass.class.php
Might do what you want.
Usage: phpunit [switches] UnitTest [UnitTest.php]
phpunit [switches] <directory>
--skeleton-class Generate Unit class for UnitTest in UnitTest.php.
--skeleton-test Generate UnitTest class for Unit in Unit.php.
so by that reasoning, if you don't tell it what file the class is in it will try guess, if you do tell it what file the class is in, it wont guess.
I ran up against a similar problem when the class a skeleton is being generated for is namespaced. I'm using phpunit 3.5.6 from the command-line. The phpUnit documentation example didn't work for me and I couldn't find the answer online so I figured I share it here. Suppose you have class Foo inside directory /my/dir and it's declared in a single file Foo.php so the full path to the class file is /my/dir/Foo.php
Also suppose that Foo is declared as follows:
<?php
namespace my\space;
class Foo
{
...
}
In order to generate a test skeleton I made sure I was in the directory where Foo.php resides and then I ran the following command:
user[/my/dir]$ phpunit --skeleton-test my\\dir\\Foo Foo.php
That got my skeleton to generate. The key was escaping the backslash as the documentation of phpUnit mentions namespaces but only shows one backslash. Hope this helps someone.