Symfony的单元测试方法总结
Symfony中使用了一种类似Perl的Test::More的单元测试库lime,语法上的确比PHPUnit2简单许多。
1. diag($msg) :简单的输出$msg的内容,不做任何测试。
2. ok($test, $msg) :如果$test为真则通过测试。
3. is($value1, $value2, $msg):如果$value1等于$value2则通过测试。
4. isnt($value1, $value2, $msg):如果$value1不等于$value2则通过测试。
5. like($string, $regx, $msg):如果$string匹配正则表达式$regx则通过测试。
6. unlike($string, $regx, $msg):如果$string不匹配正则表达式$regx则通过测试。
7. cmpok($value1, $operator, $value2, $msg) :如果$value1和$value2进行制定运算后结果为零则通过测试。
8. isa_ok($variable, $type, $msg):检查$variable是不是$type的类型。
9. isa_ok($object, $class, $msg) :检查$object是不是$class的类定义类型。
10. can_ok($object, $method, $msg) :检查$object是否有$method的方法。
11. is_deeply($array1, $array2, $msg):检查两个数组是否相同。
12. include_ok($file, $msg) :检查$file是否可以包含到。
13. fail() :永远返回失败,在异常中使用。
14. pass(): 永远返回成功,在异常中使用。
15. skip($msg, $nb_tests) :如果$nb_tests是否为真,则跳过此测试。
16. todo():测试代码还未完成。
Symfony中自定义404页面
在Symfony中的404页面定义在$sf_symfony_data_dir/modules/default/ 目录中,可能为了统一界面的考虑,想自己定义属于你的风格的404页面,其实只要自己在你的应用程序中添加了default模块(module)即可,这个模块会覆盖原来symfony的系统定义。
步骤如下:
- 在命令行输入php symfony init-module your_app_name default,其中your_app_name是你的应用程序名称。
- 在action.php加入executeError404方法
- 在template目录中加入error404Success.php文件,定义你的404页面的显示内容。
如果不想使用default模块名或error404响应事件,也可以修改settings.yml的两个设定值:
- error_404_module
- error_404_ action
symfony 1.0.10 版本发布
修改日志:
- r6622: Problem with ProjectExport and directory structure change (#2606)
- r6614: fixed magic_quotes_gpc() handling in sfRouting (#1801)
- r6610: fixed non-sf exception handling (#2064)
- r6607: fixed incorrect use of sfConfigCache:checkConfig(…, true) (#2123)
- r6469: fixed sfOutputEscaperObjectDecorator::toString() as PHP 5.2.5 does not allow exception in toString() methods (#2630)
- r6398: fixed default log file permissions (#2145)
- r6378: fixed typo in sfDebugConnection (#2524)
- r6375: changed implementation of sfCultureInfo::simplify (#1821)
- r6371: fixed connecting to a database using unix socket (#2539)
- r6358: fixed sfProcessCache eaccelerator delete also cached scripts
- r6337: reverted changes for default time and date format
接着就是期待1.1版本何时发布了。
PHP Framework之我见
今天看了篇IBM Devloper work对PHP Framework的介绍的文章,介绍了现在3个主流的Framework,其中的两个Zend Framework和Symfony我已经使用过,还有那个CakePHP只知道大名鼎鼎,但听说学习起来非常的复杂,所以还未能有幸使用。
symfony生成验证码
使用symfony非常的容易,只需你的PHP安装了GD库即可。
具体实例可参考Symfony的官方说明:http://www.symfony-project.com/snippets/snippet/32
如何在Symfony中发送Email
Symfony可以使用两种方式来发送电子邮件:
1.通过sfMail的方式
public function executePasswordRequest()
{
// determine customer from the request 'id' parameter
$customer = CustomerPeer::retrieveByPk($this->getRequestParameter('id'));
// definition of the required parameters
$mail->setSender('webmaster@my-company.com', 'My Company webmaster');
$mail->setFrom('webmaster@my-company.com', 'My Company webmaster');
$mail->addReplyTo('webmaster_copy@my-company.com');
$mail->addAddress($customer->getEmail());
$mail->setSubject('Your password request');
$mail->setBody('
Dear customer,
You are so absentminded. Next time, try to remember your password:
'.$customer->getPassword().'
Regards,
The My Company webmaster');
// send the email
$mail->send();
}
2.或者通过MVC的方式来实现,把发送Email的工作模拟成一个普通的页面请求。
public function executePasswordRequest(){
// send the email
$raw_email = $this->sendEmail('mail', 'sendPassword');
// log the email
$this->logMessage($raw_email, 'debug');
}
这种方法就是通过调用sendEmail来实现发送Email,好处当然是MVC,可以把代码和显示分开,就不用再Controller中写一堆HTML语句。sendEMail其实是一个类似于forward的函数,唯一的区别就是sendEmail会在执行完它的代码时返回,而forward会在执行完成后终止代码。两个参数分别是Module Name和Action Name。
Symfony ORM常用函数总结
在这里的总结中我们都已Symfony书中所讲的article作为Table所对应的ORM对象(ORM Class)
1.保存
$article->save();
2.删除
$article->delete();
3.通过主键Primary Key来获取资料
$article = ArticlePeer::retrieveByPk(7);
4.使用Select语句来获取资料
$c = new Criteria(); $articles = ArticlePeer::doSelect($c);
Symfony ORM栏位类型定义
Symfony framework
Symfony framework 是一套类似于Ruby on Rails的PHP版本的Framework。
1.支持使用脚本来生成框架结构。
2.Ajax的整合。
3.ORM的支持。
个人感觉比Zend Framework功能强一些。
同时相关的文档资料比较详细,和Joomla的文档相比真的是天壤之别。可能Joomla比较倾向于使用,而不是做开发工作。