简体   繁体   English

如何使用PHAR在MAMP中配置PHPUnit

[英]How to configure PHPUnit in MAMP with PHAR

I wanna test my php codes and I have decided to user PHPUnit for my test. 我想测试我的PHP代码,我决定使用PHPUnit进行测试。

I have followed steps with official documentation 我已经按照官方文档的步骤

 $ wget https://phar.phpunit.de/phpunit.phar
 $ chmod +x phpunit.phar
 $ sudo mv phpunit.phar /usr/local/bin/phpunit
 $ phpunit --version
 PHPUnit x.y.z by Sebastian Bergmann and contributors.

But I am using MAMP with my MacOS X. 但是我在MacOS X上使用MAMP。

So I am not sure how to implement PHAR files in MAMP. 所以我不确定如何在MAMP中实现PHAR文件。

Normally, the documentation tells to use this comment in terminal: 通常,文档告诉在终端中使用此注释:

sudo mv phpunit.phar /usr/local/bin/phpunit

And my PHP location is: 我的PHP位置是:

/Applications/MAMP/bin/php/php5.2.17/bin/

I've tried to run this comment: 我试过运行这个评论:

sudo mv phpunit.phar /Applications/MAMP/bin/php/php5.2.17/bin/

I don't know what should I do at this step. 我不知道在这一步应该怎么做。 Please take a look because it does not work. 请看一下,因为它不起作用。

Here are the steps I used to successfully get PHPUnit working in MAMP. 以下是我用于成功使PHPUnit在MAMP中工作的步骤。 These instructions are pieced together from various places. 这些说明是从不同的地方拼凑而成的。 I hope that having it all in one place helps someone else. 我希望在一个地方拥有一切可以帮助别人。 Happy testing! 快乐的测试!

Use MAMP's PHP in the Terminal 在终端中使用MAMP的PHP

Adapted from How to override the path of PHP to use the MAMP path? 改编自如何覆盖PHP的路径以使用MAMP路径?

Edit or create ~/.bash_profile with the lines below 使用以下行编辑或创建~/.bash_profile

# Use MAMP's latest version of PHP
MAMP_LATEST_PHP=`ls /Applications/MAMP/bin/php/ | sort -n | tail -1`
export PATH=/Applications/MAMP/bin/php/${MAMP_LATEST_PHP}/bin:$PATH

Place these lines after any other lines exporting $PATH - this assures that your MAMP PHP is found first in the path. 将这些行放在导出$ PATH的任何其他行之后 - 这可以确保在路径中首先找到您的MAMP PHP。 Note that these lines try to find the highest numbered version of PHP in your MAMP installation. 请注意,这些行尝试在MAMP安装中找到PHP编号最高的版本。 Feel free to adjust this to a specific one that you have, if desired. 如果需要,请随意根据您的具体情况进行调整。

You can tell you did it right when you get a MAMP path from which php in your terminal. 你可以告诉你这样做是正确的,当你从MAMP路径which php在你的终端。 You should get something like this: 你应该得到这样的东西:

/Applications/MAMP/bin/php/php7.0.0/bin/php

Install PHPUnit 安装PHPUnit

Mostly, this is downloading the PHP archive (PHAR) from the PHPUnit website. 大多数情况下,这是从PHPUnit网站下载PHP存档(PHAR)。 There are ways to do this from the command line that I couldn't get to work. 有一些方法可以从命令行执行此操作,但我无法使用它。 So, I used a web browser. 所以,我使用了一个网络浏览器。

  1. Download the most recent PHPUnit PHAR from https://phar.phpunit.de https://phar.phpunit.de下载最新的PHPUnit PHAR
  2. Move it to /usr/local/bin 将其移至/usr/local/bin
  3. cd /usr/local/bin
  4. Make it executable with chmod +x phpunit-5.3.2.phar (adjust according to actual name) 使用chmod +x phpunit-5.3.2.phar使其可执行(根据实际名称调整)
  5. Make a symbolic link with ln -s phpunit-5.3.2.phar ./phpunit (adjust according to actual name) ln -s phpunit-5.3.2.phar ./phpunit建立符号链接(根据实际名称调整)
  6. Check the version with phpunit -—version phpunit -—version检查版本

You should get something like this: 你应该得到这样的东西:

PHPUnit 5.3.2 by Sebastian Bergmann and contributors.

Building a symbolic link in step 5 permits you to use phpunit instead of having to type phpunit-5.3.2.phar instead. 在步骤5中构建符号链接允许您使用phpunit而不必键入phpunit-5.3.2.phar It also allows you to update PHPUnit without having to change what you type, assuming of course that you create a new symbolic link when you update. 它还允许您更新PHPUnit而无需更改键入的内容,当然假设您在更新时创建了新的符号链接。

Write a Test 写一个测试

This isn't an exhaustive section. 这不是一个详尽的部分。 There are far better tutorials on writing tests. 有关编写测试的更好的教程。 Instead, this is merely some notes from my experience on rules that tripped me up, though I'm sure everyone else knows them: 相反,这只是我对绊倒我的规则经验的一些注释,尽管我确信其他人都知道它们:

  1. Your test class name must end with Test: class SomeTest extends PHPUnit_Framework_TestCase 您的测试类名称必须以Test结尾: class SomeTest extends PHPUnit_Framework_TestCase
  2. Your test class file name must end with Test.php and match the contained class: SomeTest.php 您的测试类文件名必须以Test.php结尾,并匹配包含的类: SomeTest.php
  3. Method names in your test class that are to be run as tests must start with test: public function testSomething() 测试类中要作为测试运行的方法名称必须以test开头: public function testSomething()

Run a Test 运行测试

By this time, it should be as easy as: 到这个时候,它应该像以下一样简单:

phpunit SomeTest

If everything goes well, PHPUnit will run your test and give you the results. 如果一切顺利,PHPUnit将运行您的测试并为您提供结果。

Add a Handy Alias 添加一个方便的别名

Assuming that it all works (Yay!) try this alias in your ~/.bash_profile 假设一切正常(耶!)在你的~/.bash_profile尝试这个别名

# Use colors when running phpunit
alias phpunit='phpunit --colors'

/usr/local/bin/ is just recommended as a convention, because it's always in the $PATH on Unix systems, you can use any other location that's in your path, or leave it anywhere else and specify the absolute path when using it. /usr/local/bin/仅作为约定推荐,因为它始终位于Unix系统上的$ PATH中,您可以使用路径中的任何其他位置,或将其保留在其他位置并指定使用它时的绝对路径。

With what you have, you should be able to run: 有了你所拥有的,你应该能够运行:

/Applications/MAMP/bin/php/php5.2.17/bin/phpunit.phar --version

Note the .phar suffix, because you did not rename the file while moving, as you'd have with sudo mv phpunit.phar /usr/local/bin/phpunit 注意.phar后缀,因为你没有在移动时重命名文件,就像你有sudo mv phpunit.phar /usr/local/bin/phpunit

I tried. 我试过了。 You can use this command. 您可以使用此命令。 It worked for me. 它对我有用。

sudo mv phpunit.phar /Applications/MAMP/bin/php/php5.2.17/bin/phpunit

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

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