has anybody encountered the following problem: behat apparently works fine, but it has no predefined steps: the result of
\tests> php .\behat\behat.phar -di
is empty even the file FeatureContext.php has no steps defined.
My behat version is 2.4.0, it has been istalled with the help of PHAR, PHP Version is 5.4.9.
Below are some details of my installation. The tree structure is as follows:
tests> dir
Directory: xxx\tests
Mode Name
d---- behat
d---- features
-a--- bootstrap.php
-a--- phpunit.xml
-a--- WebTestCase.php
behat folder contains the following files:
tests> dir .\behat
Directory: xxx\tests\behat
Mode Name
-a--- behat.phar
-a--- mink.phar
-a--- mink_extension.phar
while feature folder has only bootstrap directory
tests> dir .\features
Directory: xxx\tests\features
Mode Name
d---- bootstrap
which contains the only file FeatureContext.php with the following content
<?php
use Behat\Behat\Context\ClosuredContextInterface,
Behat\Behat\Context\TranslatedContextInterface,
Behat\Behat\Context\BehatContext,
Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode;
class FeatureContext extends BehatContext
{
public function __construct(array $parameters)
{
// Initialize your context here
}
}
Behat itself doesn't come with any predefined steps.
I can see you downloaded the mink extension and I'm guessing you're looking for steps which come with it (they're defined in the MinkContext). You didn't paste your behat.yml so I'm not sure if you actually enabled the extension. For more details follow the official docs.
Note: Steps defined in the MinkContext are a good start but once you finish playing with Behat, you should rather start writing your own steps and use the language of your business domain. Default MinkContext steps hardly ever reflect one's domain language.
Related
I'm trying to configure PhpStorm 2017.2 to use PhpUnit 5 for my PHP 5.6 project.
I've downloaded the phpunit-5.7.21.phar file from the official source and placed it in my PHP 5.6 installation dir.
In PhpStorm Settings >> Languages & Frameworks >> PHP >> Test Frameworks, I've linked to the .phar executable and set the default config file to a phpunit.xml in the project root directory
Here are the contents of phpunit.xml:
.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<testsuites>
<testsuite name="Test suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
I'm trying to structure tests in a tests/unit directory within which my source file project structure would be mirrored as described in the manual. For instance:
// project files:
ClassOne.php
vendor/
ClassTwo.php
Utility.php
// test files
tests/unit/
ClassOneTest.php
vendor/
ClassTwoTest.php
UtilityTest.php
I have two problems though:
First, I don't know how to configure PhpStorm to create tests within tests/unit/ mirroring the sructure with respect to the project root. When I create a test, by default the file is put in the same directory as the project file.
Secondly, I don't know how to get PhpStorm to index the PHPUnit source code. Even though I've linked to phpunit-5.7.21.phar file as shown above, the IDE complains when I create a test:
namespace vendor;
class UtilityTest extends \PHPUnit_Framework_TestCase{}
Undefined class PHPUnit_Framework_TestCase
Update 1
I solved the 2nd problem by adding the directory where I had saved the .phar to the PhpStorm include path, set in Settings >> Languages & Frameworks >> PHP >> Include Path tab. Alternatively, I could just put the .phar file within the project directory and it will be indexed.
I still need help with my first problem.
Update 2
Thanks to Ástþór's answer I figured out how to get PhpStorm to mirror the project structure within a dedicated tests directory. Go to PhpStorm Settings >> Directories and select the base testing directory. The click Test near the top to mark it as a Test Sources Root
The next time you create a test, it will automatically be placed in that directory.
Mark a directory with your source classes as "Source root" and a directory with tests as "Test Sources Root". After that directories will be pre-filled on a test creation (e.g. via ctrl+shift+T on a source class).
Not sure though if that would work fine with your "mirroring" system: I guess you would still have to manually adjust directories for the part of your tests
I installed composer and laravel and installed some packages
all work fine, but now I created my own class under the folder services
I gave it name space of Services like that:
namespace Services;
And the class name is UploadToImgurService
I run the composer command:
composer dump-autoload
And in my controller I wrote:
use Services\UploadToImgurService;
But I get this error:
Class 'Services\UploadToImgurService' not found
What did I did wrong?
Is there anything else that I should do with composer for autoloading the service class?
EDIT
I found a solution
I edited y composer.json file and added to psr-4 the line
"Services\\" : "app/services"
But why It didn't workt before? The line :
"App\\": "app/",
was there, maybe it loaded the class but under the app namespace?
If you are using a case sensitive file system, you will need to have the Services folder with upper case S.
Your idea is right, but let me try to explain how the psr-4 autoloading works.
You can define root namespaces in your composer.json file and map it to any project directory. Inside the defined directories, your classes should get the root namespace. The namespace segments after the root are build by your sub directory structure and the class name is equal to the file name (PSR-4 Autoloading).
E.g
"MyNamespace\\WithSubNamespace\\": "cool/project"
cool/project/MyClass.php -> MyNamespace\WithSubNamespace\MyClass
cool/project/SubDirectory/AnotherClass.php -> MyNamespace\WithSubNamespace\SubDirectory\AnotherClass
In Laravel, the app directory is mapped to the App namespace as default. Optionally, you can change the root namespace with the command php artisan app:name [NewRootNamespaceName], but the autoloader only finds classes inside the app directory.
If you create a new directory outside of "app", you have to add the directory to your psr-4 namespace mapping in the composer.json file.
In your example, you define a new root namespace in the existing app directory, so your issue was that the root namespace was unknown and you solved it by adding the line in your composer.json.
This is possible, because psr-4 provides a huge flexibility. But personally, i would not recommend to use different root namespaces in the same project.
I hope i could help and maybe this is also interesting for you: composer.json PSR-4.
maybe you forgot to add this line of code
require_once('vendor/autoload.php');
Once I was getting the error:
Fatal error: Uncaught Error: Class "..." not found
in /var/www/html/... on line ...
I was requiring autoload file this way:
require 'vendor/autoload.php';
After I changed the line to:
require __DIR__.'/vendor/autoload.php';
it started working...
Weird, since the the vendor folder was in the same directory as the file which was calling it.
BTW, I was using Composer 2 and PHP 8.1.
I'm just getting back into PHP and trying to do things the correct way from the beginning. So I've installed PHPLint 2.1_20151116 and I cannot seem to get it to work with composer's autoload. Is it possible?
For example I am trying to add a test case to Laravel/Envoy, but I cannot get past an error "undeclared parent class TestCase".
The folder structure is:
envoy
├── tests
│ ├── RemoteProcessorTest.php
│ ├── SSHConfigFileTest.php
│ └── TestCase.php
The contents of RemoteProcessorTest.php is:
<?php
namespace Laravel\Envoy;
class RemoteProcessTest extends TestCase
{
}
If I run ./vendor/bin/phpunit then I get the error: No tests found in class "Laravel\Envoy\RemoteProcessTest".. Which isn't a syntax error so it looks like everything is valid. But phplint still complains.
$ cd envoy
$ phpl --php-version 5 --print-path relative --print-column-number --tab-size 4 --no-overall tests/RemoteProcessorTest.php
BEGIN parsing of tests/RemoteProcessorTest.php
1: <?php
2: namespace Laravel\Envoy;
3:
4: class RemoteProcessTest extends TestCase
5: {
{
\_ HERE
==== 5:1: ERROR: undeclared parent class TestCase
6: }
END parsing of tests/RemoteProcessorTest.php
Is there a work around for this?
I may not be able to answer the question directly as the repo in question seems too limited to give you what you seek. I'll leave it to the community to answer the question directly.
Here are some alternatives that hopefully will help.
If you want to syntax check PHP has a built-in linter. Simply:
$ php -l filename.php
or --syntax-check instead of -l.
There is also this on packagist as a viable alternative. https://packagist.org/packages/gamegos/php-code-sniffer
It has a configuration file named phpcs.xml that you can check into your repo and include via composer.
# composer.json
...
require-dev {
"gamegos/php-code-sniffer": "0.4.0"
}
...
The phpcs.xml file has a bootstrap tag that will give it the information to find the Laravel classes among your own.
<?xml version="1.0" encoding="UTF-8"?>
<ruleset>
<rule ref="Gamegos" />
<arg name="bootstrap" value="vendor/autoload.php" />
</ruleset>
After installing with Composer you'll have 3 binaries:
vendor/bin/phpcbf
vendor/bin/phpcs
vendor/bin/phpcs-pre-commit
You can find a lot of customization options on the Github repo. https://github.com/gamegos/php-code-sniffer
I'm new to Shell using CakePHP and I couldn't notice how there are 2 cake console application: one at app/Console/cake and other in lib/Console/cake being the second one the one in CakePHP's core.
So far, I have used lib/Console/cake bake -app /path/to/app to bake some MVC classes, but I have never used app/Console/cake before. Also lib/Console/cake is configured in my environment variables.
I want to know the difference of these 2, when to use one or another and why.
Note: I moved my core folder to a different directory, so naturally, app/Console/cake wont find ShellDispatcher, which line should I modify to set the new path to core library?
Are identical, but you should use the one inside app folder:
cd app/
./Console/cake bake
To change the default folder structure and config you must edit these files:
/app/webroot/index.php
/app/webroot/test.php
and un-Comment this line putting your core folder location:
// /usr/lib/mylocation
define('CAKE_CORE_INCLUDE_PATH', DS . 'usr' . DS . 'lib' . DS . 'mylocation');
Take a look at the manual here:
http://book.cakephp.org/2.0/en/installation/advanced-installation.html#sharing-cakephp-libraries-with-multiple-applications
The difference
The difference between the two executables is that one derives the app location from the current working directory, and the other is application specific. This can be highlighted like so:
Normal usage:
www-data # dev [ /tmp/cakephp ] (master=)
-> app/Console/cake
Welcome to CakePHP v2.4.6 Console
---------------------------------------------------------------
App : app
Path: /tmp/cakephp/app/
---------------------------------------------------------------
Reference app executable from different path:
-> cd anywhere
-> /tmp/cakephp/app/Console/cake
Welcome to CakePHP v2.4.6 Console
---------------------------------------------------------------
App : app
Path: /tmp/cakephp/app/
---------------------------------------------------------------
Note the app and path did not change.
cake in path
If /tmp/cakephp/lib/Cake/Console/ is in the path:
-> cd anywhere
-> cake
Welcome to CakePHP v2.4.6 Console
---------------------------------------------------------------
App : xxx
Path: **anywhere**
---------------------------------------------------------------
Note the app and path vary depending on where you are when executing the command.
If you always specify the -app flag, they will function the same, but you'll find it is problematic to use cake in your path if for example you have multiple applications on the same host using different versions of CakePHP.
Fixing paths
If you moved the cake folder, the files you need to edit are:
app/Console/cake.php ($root variable/include_path)
app/webroot/index.php (ROOT constant)
app/webroot/test.php (ROOT constant)
Only the first will affect cli usage.
I installed behat with mink and selenium2-driver for my Symfony2 project.
Is it possible to use the /app/config/behat.yml instead of the /behat.yml file?
I searched on google but I can't find anything else this command.
php bin/behat --config app/config/behat.yml
But the command isn't working either.
I think there must be a config-path in composer.json.
Yes, you can configure which config file you want to use. Look at this part of the doc.
http://docs.behat.org/guides/7.config.html#paths
What error do you get when running your command?
php bin/behat --config app/config/behat.yml
This error?
[RuntimeException]
Context class not found.
Maybe you have provided a wrong or no `bootstrap` path in your behat.yml:
http://docs.behat.org/guides/7.config.html#paths
If that's the case, I think if might be because you need to specify where to find the features in your behat.yml file.
Now you moved the file to /app/config/behat.yml, the related path from behat.yml to the feature directory has changed, so you should add the following to the file:
default:
paths:
features: ../features/
bootstrap: ../features/bootstrap