官方指引
很遺憾, phpunit還沒有在ArchLinux的倉庫裡。
所以使用下載安裝的方式。按照官方的指引:
<code>wget https://phar.phpunit.de/phpunit.phar chmod +x phpunit.phar sudo mv phpunit.phar /usr/local/bin/phpunit phpunit --version</code>
結果得到下面的錯誤:
<code>PHP Warning: realpath(): open_basedir restriction in effect. File(/usr/local/bin/phpunit) is not within the allowed path(s): (/srv/http/:/home/:/tmp/:/usr/share/pear/) in /usr/local/bin/phpunit on line 3 PHP Fatal error: Class 'Phar' not found in /usr/local/bin/phpunit on line 714</code>
啟用phar擴充
先解決Fatal error: Class 'Phar' not found
。
<code>ls /usr/lib/php/modules</code>
發現有 phar.so,表示Phar的擴充已經安裝,那麼是不是該擴充沒有Enable呢?
開啟 /etc/php/php.ini
搜尋 phar
,果然發現 extension=phar.so
被註解掉了。去掉該行前面的 ;
,保存php.ini,再運行 phpunit --version
。
<code>PHP Warning: realpath(): open_basedir restriction in effect. File(/usr/local/bin/phpunit) is not within the allowed path(s): (/srv/http/:/home/:/tmp/:/usr/share/pear/) in /usr/local/bin/phpunit on line 3 PHP Warning: Phar::mapPhar(): open_basedir restriction in effect. File(/usr/local/bin/phpunit) is not within the allowed path(s): (/srv/http/:/home/:/tmp/:/usr/share/pear/) in /usr/local/bin/phpunit on line 714</code>
Fatal error
解決了,但警告還在,而且phpunit沒有正常運作。
php對檔案存取的保護機制
google之,發現這裡有解釋: http://www.templatemonster.com/help/open_basedir-restriction-in-effect-filex-is-not-within-the-allowed-paths-y.html
PHP open_basedir protection tweak is a Safe Mode security measure that prevents users from opening files or scripts located outside of their home directory with PHP, unless the. be limited to files under certain directory, and thus prevent php scripts for a particular user from accessing files in unauthorized user's account. When a script tries to open a file with, for example, fopen() or gzopen( the loopenis checked. When the file is outside the specified or permissible directory-tree, PHP will refuse to open it and the following errors may occur: ...
意思是說:php.ini中的open_basedir
是php為保證安全進行檔案存取的設定。如果該選項被賦值,所有的檔案操作將限定在特定的目錄裡,這樣可以防止某個使用者使用php腳本讀取未授權的內容。當你想要透過fopen
或gzopen
開啟一個檔案時,如果該檔案的位置不再被允許的目錄下面,就會出現上述的警告訊息。
從警告訊息發現可以存取的目錄包括/srv/http/:/home/:/tmp/:/usr/share/pear/
,剛好~/bin
即在PATH變數中,也屬於可以被php腳本讀取的目錄,於是
<code>mv /usr/local/bin/phpunit ~/bin</code>
再運行phpunit --version
,得到正確結果:
<code>PHPUnit 4.5.0 by Sebastian Bergmann and contributors.</code>
phpunit安裝成功!
以上就介紹了phpunit安裝出錯的原因及解決方法,包括了方面的內容,希望對PHP教程有興趣的朋友有幫助。