如何使用Zend Framework和PHPUnit成功创建项目? [英] How do I successfully create a project with Zend Framework and PHPUnit?

查看:59
本文介绍了如何使用Zend Framework和PHPUnit成功创建项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我要说的是,我已将此内容发布到多个论坛,甚至试图在ZF IRC频道上获得帮助.我已经连续一个星期谷歌搜索,但仍然没有结果.过去,我在该站点上阅读过很多Q和A,所以我想我要开一个帐户,然后尝试问您.

Let me just start by saying that I've posted this to multiple forums and even tried to get help on the ZF IRC channel. I've been Googling for a straight week and still no results. I've read a lot of Q's and A's on this site in the past, so I figured I'd make an account and try asking you.

(是的,我已经搜索过以前提出的问题,但是没有一个答案对我有帮助.)

(Yes, I've searched previously asked questions, but none of the answers helped me.)

我正在尝试学习如何将Zend Framework用于我加入的新项目.出于兼容性原因,他们使用的是Zend 1(而不是较新的Zend 2).我已经找到并遵循了许多在线和实体书教程,但是一次又一次地得到相同的结果.

I'm trying to learn how to use Zend Framework for a new project that I've joined. For compatibility reasons they are using Zend 1 (and not the newer Zend 2). I have found and followed a number of online and physical book tutorials but I've the same results over and over again.

这就是所有教程所给出的说明.我转到framework.zend.com并下载了ZF 1.12的完整版本.

So here goes (this are the instructions that all the tutorials give). I went to framework.zend.com and downloaded the full version of ZF 1.12.

  1. 我解压缩了内容
  2. 我将库文件夹移动到了一个不会被修改的安全目录
  3. 我将bin文件夹的内容移到了与我的PHP可执行文件相同的目录中
  4. 我将php.ini文件中的include_path更改为包含库目录
  5. 我更新了Windows PATH变量,以确保它包含PHP可执行文件的路径
  6. 我参加了

  1. I unzipped the contents
  2. I moved the library folder to a safe directory where it won't be modified
  3. I moved the contents of the bin folder to same directory as my PHP executable
  4. I changed the include_path in my php.ini file to include the library directory
  5. I updated my Windows PATH variable to make sure it included the path to the PHP executable
  6. I ran

zf --help

zf --help

此命令按预期工作.我还成功运行了zf show版本(Zend Framework版本1.12.7).

This command worked as intended. I also successfully ran zf show version (Zend Framework Version 1.12.7).

我运行了命令

zf创建项目myproject

zf create project myproject

这样做之后,我收到以下错误消息:

Upon doing this, I receiving the following error message:

Fatal error: Class 'PHPUnit_Framework_TestCase' not found in D:\Zend\library\Zend\Test\PHPUnit\ControllerTestCase.php on line 48

有问题的那一行是扩展PHPUnit_Framework_TestCase的类声明. 我不知道PHPUnit_Framework_TestCase的定义位置.它不在我从Zend下载的单个ZIP文件中的文件或目录的任何中.我什至在所有文件和文件夹上运行grep,都在搜索字符串"class PHPUnit_Framework_TestCase",但未显示任何结果.

That particular line in question is a class declaration that extends PHPUnit_Framework_TestCase. I don't know where PHPUnit_Framework_TestCase is defined. It is not in any of the files or directories that came in the single ZIP file that I downloaded from Zend. I even ran grep on all files and folders searching for the string "class PHPUnit_Framework_TestCase" but it printed no results.

有人建议我没有安装PHPUnit(现在对我来说很明显).令我感到困扰的部分是,我阅读的教程中完全没有提到任何有关在事前安装PHPUnit或如何安装它或Zend对它有什么依赖性的内容.其中许多都是初学者教程,它们假定您仅具有PHP的基本知识,而没有人提到PHPUnit令我感到非常震惊.如果PHPUnit如此重要,我会认为我从Zend下载的文件会包含它.我想不是.

Some have suggested that I don't have PHPUnit installed (which is obvious to me now). The part that bugs me is that absolutely none of the tutorials that I read mention anything about installing PHPUnit before hand or how to install it or what dependencies Zend has on it. Many of these were beginner tutorials that assumed you only had a basic knowledge of PHP, and it's pretty shocking to me that none even mentioned PHPUnit. If PHPUnit was that important I would think that the file I downloaded from Zend would have included it. I guess not.

因此,我再次上线并获得了PHPUnit的PHAR文件,但是现在怎么办?我尝试将其放在多个不同的目录中,但仍然遇到相同的错误.我不应该使用PHAR文件吗?我应该使用实际文件吗?

So I went online again and got the PHAR file for PHPUnit, but now what? I tried putting it in multiple different directories but I still get the same error. Am I not supposed to use a PHAR file? Should I be using the actual files instead?

要让ZF识别PHPUnit,解决此错误并创建我的第一个ZF项目,我该怎么做?

其他信息: Windows 7,XAMPP服务器(在本地主机上运行),PHP 5.5.6

Additional Info: Windows 7, XAMPP Server (running on localhost), PHP 5.5.6

推荐答案

假定您已安装PHPUnit,并且它位于您的include路径中(请注意,ZF1仅正式支持PHPUnit 3.4.x,并且绝对不支持上述任何内容. PHPUnit 3.5.x,因此,如果您使用的是XAMPP,则可能必须按照 commit 导致的,PHPUnit的require调用已被剥离赞成使用自动装带器.但是,基于CLI的ZF工具没有设置自动加载器,因此找不到PHPUnit,因为它根本不是require d!要解决此问题,您可以将这些行返回到Zend/Test/PHPUnit/ControllerTestCase.php

Assuming you have PHPUnit installed and it is on your include path (Bearing in mind that ZF1 only officially supports PHPUnit 3.4.x and definitely doesn't support anything above PHPUnit 3.5.x, so if you're using XAMPP you may have to downgrade PHPUnit as described here). The problem is most probably due to this commit, where the require calls for PHPUnit were stripped out in favour of using an autoloader. The ZF tool over CLI doesn't set up an autoloader though, so PHPUnit is not found because it is simply not required! To fix you can return these lines to the start of Zend/Test/PHPUnit/ControllerTestCase.php

/** @see PHPUnit_Runner_Version */
require_once 'PHPUnit/Runner/Version.php';

/**
* Depending on version, include the proper PHPUnit support
* @see PHPUnit_Autoload
*/
require_once (version_compare(PHPUnit_Runner_Version::id(), '3.5.0', '>=')) ? 'PHPUnit/Autoload.php' : 'PHPUnit/Framework.php';

值得注意的是,即使出现您提到的错误,ZF工具在建立项目时仍应正常工作,它不会产生单元测试操作(您必须自己进行操作).如果您不想降级您的XAMPP PHPUnit版本,您应该能够使用composer在本地将正确的版本添加到您的项目中,如

It's worth noting that even with the error you mention, ZF tool should still work correctly when setting up a project, it just won't produce unit test actions (you'll have to make them yourself). If you don't want to downgrade your XAMPP PHPUnit version you should be able to add the correct version locally to your project using composer as described here.

2015年1月更新: 由于ZF1自1.12.7起至少支持4.1版本的PHPUnit,因此不再需要为XAMPP降级PHPUnit(我没有在4.1以上进行测试).这很有用,因为PHPUnit已已完全删除自2014年12月起已弃用的Pear存储库,这意味着您无论如何都无法下载3.7之前的版本! (当前XAMPP随PHPUnit 3.6一起提供).这些天,尽管值得在Vagrant中使用XAMPP for Vagrant并通过Composer在全球范围内安装PHPUnit 4.1.

Update Jan 2015: Downgrading PHPUnit for XAMPP is no longer necessary as ZF1 has supported at least version 4.1 of PHPUnit since 1.12.7 (I've not tested above 4.1). This is helpful as PHPUnit has completely removed their deprecated Pear repository as of December 2014, which means you can't download versions older than 3.7 anymore anyway! (Currently XAMPP ships with PHPUnit 3.6). These days though it's probably worth chucking XAMPP for Vagrant and globally installing PHPUnit 4.1 via Composer during Vagrant provisioning.

这篇关于如何使用Zend Framework和PHPUnit成功创建项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆