简体   繁体   English

为什么phpUnit测试不起作用? ZF2

[英]Why phpUnit test does not work? ZF2

I am reading the book "Learn ZF2: Learning By Example" by Slavey Karadzhov. 我正在阅读Slavey Karadzhov的书“学习ZF2:通过示例学习”。 Now I am at pages 103-107. 现在我在第103-107页。 I am trying to run PHPUnit test as described, but nothing happens... Where is the problem and how to fix it? 我正在尝试按所述方式运行PHPUnit测试,但是什么也没发生... 问题出在哪里以及如何解决?

To go into the same situation you would have to: 要进入相同的情况,您必须:

git clone https://github.com/slaff/learnzf2.git .

composer.phar self-update
composer.phar install

git stash
git checkout 'ch-unittest'

This should bring you the same files as I have (=as in the book). 这应该为您带来与我相同的文件(=与书中一样)。

Now, I try to run this command: 现在,我尝试运行以下命令:

ZF2_PATH=`pwd`/vendor/zendframework/zendframework/library \php vendor/bin/phpunit -c module/User/tests/

And the command does not work as expected... I would expect some test result output or so, but all I get is the output witch is similar to the content I have in vendor/bin/phpunit file. 而且该命令无法按预期方式工作...我希望可以得到一些测试结果输出,但是我得到的vendor/bin/phpunit是输出女巫,类似于我在vendor/bin/phpunit文件中的内容。 It is: 它是:

#!/usr/bin/env sh
SRC_DIR="`pwd`"
cd "`dirname "$0"`"
cd "../phpunit/phpunit/composer/bin"
BIN_TARGET="`pwd`/phpunit"
cd "$SRC_DIR"
"$BIN_TARGET" "$@"

To be more precise, the first line (#!/usr/bin/env sh) does not show up in the output. 更准确地说,第一行(#!/ usr / bin / env sh)不会显示在输出中。 The rest do show up. 其余的确实出现。

** **

I am running the code on Windows 8.1 Pro OS, using Git Bash (git version 1.9.4.msysgit.1). 我正在Windows 8.1 Pro OS上使用Git Bash(git版本1.9.4.msysgit.1)运行代码。 php keyword seems to work well as writing php -v works as expected (outputs PHP 5.5.12 (cli) (built: Apr 30 2014 11:20:58) ... ) php关键字似乎可以像编写php -v正常工作(输出PHP 5.5.12 (cli) (built: Apr 30 2014 11:20:58) ...

My own guess would be that Git bash cannot resolve the code it finds in vendor/bin/phpunit so it fails there... But this is the guess only... 我自己的猜测是,Git bash无法解析在vendor / bin / phpunit中找到的代码,因此它在那里失败了……但这只是猜测...

EDIT #1: Just tried on shared hosting with LAMP set. 编辑#1:刚尝试与LAMP集共享托管。 Everything went fine. 一切都很好。 It seems that Zend Framework 2 and Windows are not so friendly at some points (eg at the just mentioned unit testing case). 似乎Zend Framework 2和Windows在某些方面并不那么友好(例如,在刚刚提到的单元测试案例中)。 Looks like the very good book (no sarcasm, it is really the good one) would be better if it would have one more chapter on something like VirtualBox + Vagrant + PuPHPet (or something close to that). 如果再增加一章有关VirtualBox + Vagrant + PuPHPet(或类似的东西)的内容,则看起来不错的书(不要讽刺,这确实是一本好书)看起来会更好。

The line of composer.json file used in the book: phpunit/phpunit" : "3.7.* 本书中使用的composer.json文件所在行: phpunit/phpunit" : "3.7.*

I have changed phpunit/phpunit" : "3.7.* to phpunit/phpunit" : "4.5.* as 4.5 version of PHPUnit is the current stable. 我已将phpunit/phpunit" : "3.7.*更改为phpunit/phpunit" : "4.5.*因为4.5版本的PHPUnit是当前稳定版本。 Then, of course: php composer.phar update Then this one will work nicely: 然后,当然: php composer.phar update然后,这个将很好地工作:

ZF2_PATH=`pwd`/vendor/zendframework/zendframework/library \php vendor/phpunit/phpunit/phpunit -c module/User/tests/

The command above is not the same as in the book. 上面的命令与本书中的命令不同。

Tested on Windows 8.1, Ubuntu Trusty 14.04 LTS x64, the isolated development environment provide here . 隔离的开发环境在Windows 8.1(Ubuntu Trusty 14.04 LTS x64)上进行了测试, 此处提供。 Works well. 效果很好。

There is also a way to make PHPUnit 3.7.* work on all systems (same systems tested by me), but the command must be changed to: 还有一种方法可以使PHPUnit 3.7。*在所有系统(我测试过的相同系统)上运行,但是必须将命令更改为:

ZF2_PATH=`pwd`/vendor/zendframework/zendframework/library \php vendor/phpunit/phpunit/composer/bin/phpunit -c module/User/tests/

The command above is not the same as in the book. 上面的命令与本书中的命令不同。

So... Here we have two ways mentioned to go with PHPUnit tests to make them work. 所以...在这里,我们提到了两种与PHPUnit测试一起使用以使其正常工作的方法。 It is up to us to decide witch of two is better. 由我们决定两个女巫更好。 I would prefer the one with increasing PHPUnit version to 4.5 at the moment. 我希望目前将PHPUnit版本提高到4.5。

Many thanks go to Slavey Karadzhov - the author of the book who made isolated development environment for programming with Zend Framework 2(ZF2) available. 非常感谢Slavey Karadzhov-这本书的作者,他提供了隔离的开发环境以使用Zend Framework 2(ZF2)进行编程。
Also, thanks to @jamaldo, @steeler and @malte whose activity (post, comments) brought me on the right track. 另外,感谢@ jamaldo,@ steeler和@malte的活动(帖子,评论)使我走上了正轨。 The post I am talking about is here: https://stackoverflow.com/questions/23881289/php-vendor-bin-phpunit-is-printing-in-console-a-text 我正在谈论的帖子在这里: https : //stackoverflow.com/questions/23881289/php-vendor-bin-phpunit-is-printing-in-console-a-text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM