I have a very simple PHP/Composer application with the following structure:
- src
- content
- test
- Sandbox.php
Sandbox.php has only a static function to print "test" and its namespace is
namespace MyApplication\Content\Test;
My autoload.php has MyApplication an "autoload" property.
"autoload" : {
"psr-4": {"MyApplication\\": "src/"}
},
I run composer install --no-dev in an Windows environment with XAMPP and in a test.php file I do (for the sake of a very simple test):
$autoloadFile = __DIR__ . '/wp-content/plugins/sandbox/vendor/autoload.php';
require $autoloadFile;
echo 'autoload = ' . file_exists($autoloadFile);
echo '<br />';
echo 'class_exists = ' . class_exists('MyApplication\Content\Test\Sandbox');
When I run this test.php file locally, it works perfectly. MyApplication is loading Sandbox class.
However, when I release this to my server which is a Linux based server but running on the same PHP version, Sandbox class is not found.
I made sure my /vendor/ folder is properly uploaded as well.
I'm wondering if the problem is happening because I'm running composer install on a Windows environment while it should be running the same command in my server (which I can't at the moment). Shouldn't the /vendor/ folder upload be enough to make the autoload classes work well?
Path to your file is src/content/test/Sandbox.php and according to PSR-4 it should be src/Content/Test/Sandbox.php - on Windows it does not matter, but on Linux it does.
What I have:
WPKit/Module/AbstractFunctions.php Abstract Class;
wp-content/themes/mytheme/modules/quiz/Functions.php:
use WPKit\Module\AbstractFunctions;
class Functions extends AbstractFunctions { ... }
wp-content/themes/mytheme/tests/quiz/QuizFunctionsTest.php:
require_once dirname(__FILE__) . "/../../modules/quiz/Functions.php";
class QuizFunctionsTest extends TestCase {
public function testGetQuizByID() {
# some code
}
}
When running phpunit QuizFunctionsTest.php, it gives me the following error:
PHP Fatal error: Class '%path%\AbstractFunctions' not found in %path%/modules/quiz/Functions.php on line 18
I tried require_once the missing class but it didn't help. Outside the test class my code works just fine. Any thoughts?
TL;DR: It seems I couldn't get it to work because it was a WordPress setup so what I did was install PHPUnit via wget and use WP-CLI to scaffold theme-tests, then it worked like a charm.
First I installed the PHPUnit from terminal inside my theme directory:
wget -O phpunit https://phar.phpunit.de/phpunit-7.phar
chmod +x phpunit
Next I installed WP-CLI:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
Next setting up the test suite:
wp scaffold theme-tests your-theme-slug
Also make sure you have svn installed.
In my case (and I found some mention it, too) I had to delete /tmp/wordpress and /tmp/wordpress-tests-lib and re-run the previous command twice because the script somehow didn't download everything.
Notice that you'll have a separate WordPress setup in /tmp/wordpress. In my primary WordPress setup I use WPKit library which is stored in WordPress root near wp-content so I had to copy it to /tmp/wordpress, otherwise my PHPUnit tests couldn't find WPKit files which were included in the classes I had to test.
So when you run wp scaffold it creates bin directory in your theme root. Run the following command from your theme root in order to install the test suite:
bin/install-wp-tests.sh <test-database-name> <user> <password> <host> <wordpress-version>
In my case it looked like:
bin/install-wp-tests.sh wp_test root '' localhost latest
Be sure to use empty database because running PHPUnit clears the database every time so you may lose your data.
After all these actions just run PHPUnit and you should be fine:
./phpunit
It runs tests in your-theme/tests directory. By default it ignores the default test file so use it to make your own simple tests to check everything works.
Whew.
Sources:
https://www.kirstencassidy.com/testing-wordpress-plugins-wp-cli-phpunit/
https://phpunit.de/getting-started/phpunit-7.html
https://wp-cli.org/#installing
https://www.smashingmagazine.com/2017/12/automated-testing-wordpress-plugins-phpunit/
Installed PHPUnit via PEAR and copied the latest repository from https://github.com/sebastianbergmann/phpunit/
Wrote a basic test and saved it in the root directory (also tried /Tests/)
<?php
require_once 'PHPUnit/Autoload.php';
class CalculatorTest extends PHPUnit_Framework_TestCase{
public function testAdd(){
$c = New Calculator();
$result = $c->add(5, 10);
$this->assertEquals(15, $result);
}
}
Throws errors about not being able to load require_once(SebastianBergmann/Diff/autoload.php) In the PHPUnit/Autoload.php there is three lines, 69, 70 and 71.
require_once 'SebastianBergmann/Diff/autoload.php';
require_once 'SebastianBergmann/Exporter/autoload.php';
require_once 'SebastianBergmann/Version/autoload.php';
The directory SebastianBergmann doesn't even exist.... why are these lines in here?
What am I doing wrong, did I copy from the wrong place?
Also trying to run /Tests/Runner/BaseTestRunnerTest.php fails with Class 'PHPUnit_Runner_BaseTestRunner' not found
You cannot just clone it and expect to work. Either completely install via PEAR or composer.
As of those particular references - they are satisfied as composer dependencies: see here https://github.com/sebastianbergmann/phpunit/blob/master/composer.json#L32
I am having some trouble configuring doctrine orm on windows 8, php 5.4. I have installed Doctrine using Composer.
I have followed the docs to the letter but when I run any commands, php vendor/bin/doctrine orm:schema-tool:create for example, my command line just outputs
SRC_DIR="`pwd`"
cd "`dirname "$0"`"
cd "../doctrine/orm/bin"
BIN_TARGET="`pwd`/doctrine"
cd "$SRC_DIR"
"$BIN_TARGET" "$#"
I have also tried php vendor/bin/doctrine.php .... but it just prints out the above.
I have followed Doctrine's guide to the letter. Has anyone seen this before and if so, can you suggest anything?
i found a solution
there are also a bin folder in vendor/doctrine/orm/bin/ you can use this one like this
php vendor/doctrine/orm/bin/doctrine orm:schema-tool:create
make sure you have root folder and a cli-config.php file is present in root folder.
below is location where i found a solution
https://groups.google.com/forum/#!msg/doctrine-user/_ph183Kh-5o/_P_coljB-dcJ
this is working fine for me .
I had the same problem. The following solution worked for me:
"vendor/bin/doctrine.bat" orm:schema-tool:create
So, basically you:
use the ".bat" file provided by Doctrine and
enclose the call to that ".bat" file in quotes.
My Environment
Windows 7 Professional (x64)
PHP 5.5.12
Doctrine ORM 2.4.4
Do not write "php..." (it will write the file content)
Just "vendor\bin\doctrine orm:schema-tool:create" do the job (from the project root eg. c:\php\theProject).
Next, you will need a "cli-config.php" in the project root...
For Window version use backward slash "\"
vendor\bin\doctrine orm:schema-tool:create
not forward slash "/"
vendor/bin/doctrine orm:schema-tool:create
You can either install something like git bash or simply use the PHP version of the script:
php vendor\bin\doctrine.php orm:info
Obviously, the php binary directory should be in your PATH environment variable, otherwise, it's something like:
C:\path\to\php.exe vendor\bin\doctrine.php orm:info
Copy doctrine.bat (located at vendor/bin/doctrine.bat) to your project root directory
Create a bootstrap.php file in any path inside your project root directory with the following content:
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
$paths = array("../model");
$isDevMode = false;
$dbParams = array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'user' => 'root',
'password' => '',
'dbname' => 'angular_php',
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);
Create a cli-config.php file in your project root directory with the following content:
<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
// replace with file to your own project bootstrap
require_once 'path/to/file/bootstrap.php';
return ConsoleRunner::createHelperSet($entityManager);
Execute from a command line window (CMD):
c:\path\to\project\root\directory>doctrine --help
It's done!
I found it wasnt returning anything from the doctrine.php.bat. Turns out it was a PHP error in my cli-config.php file
Using Cygwin on Windows with doctrine installed via composer, was having the same problem
resolved by:
vendor/bin/doctrine.bat orm:convert-mapping
if you are still having issues you can run the cli script using php to get the console tools running:
for example
php cli-config.php orm:schema-tool:create
All the answers on this question are either outdated or outright incorrect. In my case, I observed, after installing the the library, the "\vendor\doctrine\orm\" folder was completely empty. The docs ask you to run vendor/bin/doctrine, which in turn attempts to call "vendor\doctrine\orm\bin\doctrine[.php]" (The php file extension is optional). After discovering this, I downloaded the library from the git repository and replaced the composer-installed version.
php vendor/doctrine/orm/bin/doctrine
then works fine.
Also, beware of the common misconception that the cli-config.php file must exist in the root of your project. It's fine to leave it in your config folder
I'm trying to create a very simple PHP CLI application that can be run as a phar file from the command line:
# php myProject.phar
This is what I've tried so far:
My Project
My project is in a directory called MyProject and it has these two files in it:
|-- createPhar.php
`-- bootstrap.php
bootstrap.php
The bootstrap.php file contains this:
<?php
print phpversion() . PHP_EOL;
print 'i am some script' . PHP_EOL;
When I run this script from my Ubuntu command line:
# cd MyProject
# php bootstrap.php
I get the following output:
5.3.2-1ubuntu4.9
i am some script
createPhar.php
The createPhar.php file is meant to turn the project into Phar archive.
It looks like this:
<?php
$phar = new Phar('MyProject.phar');
$phar->addFile('bootstrap.php');
$phar->setStub( $phar->createDefaultStub('bootstrap.php') );
When I run that script...
# php createPhar.php
... a new file called MyProject.phar is created in my project's directory.
|-- bootstrap.php
|-- createPhar.php
`-- MyProject.phar
Now here's the problem
When I run the phar file...
# php MyProject.phar
...I expect to see the same the same output that I got when when I ran the bootstrap.php script.
Instead I see nothing. No output at all. This implies that my bootstrap.php script is not being included by the default stub that was created by $phar->createDefaultStub('bootstrap.php')
I think I am misunderstanding how Phars and their stubs are being created. Could you, please, explain where I have gone wrong.
To answer my own question.
The method outlined in my question, above, is one correct way to create a phar / phar stub.
The reason why it did not work for me and did work for Mario (see his comment below the question), is because I had Suhosin installed and needed to tweak the settings.
Fixed using the technique outlined here:
To fix, put:
suhosin.executor.include.whitelist="phar"
in /etc/php5/cli/php.ini
you could also do it like this:
bootstrap.php;
<?php
function go() {
print phpversion() . PHP_EOL;
print 'i am some script' . PHP_EOL;
}
then:
php -r "require
'phar://Myproject.phar'; go();"
or don't have a function and it will execute whatever commands you have in there, but typically you would have some functions or class files in the phar.