如何在 cakePHP 和 Simpletest 中执行复杂的搜索并测试它们
我有一个非常复杂的数据结构,比如 10 个没有连接表的表。我的应用程序需要能够在大多数表中执行搜索。
为此,我将搜索字段的内容转换为条件数组。键是模型名称,值是搜索条件,即
$conditions = array(
'Artist' => array(
'OR' => array(
'Artist.name LIKE' => '%barl%',
'Pseudonym.name LIKE' => '%barl%'
)
),
'Content' => array('Content.subject' => 'architecture'),
'Editor' => array('Editor.name LIKE' => '%Gal%'),
etc....
)
该数组传递给可搜索的模型,每个模型都采用相关的条件。
$this->find('all', array('conditions' => $conditions['Artist']))
到目前为止一切都很好,至少我认为。现在我开始测试模型,我发现自己在不同的模型测试用例中一遍又一遍地复制同一个数组,这让我很烦恼。
有没有办法让每个测试用例都可以访问这个数组?也许数组不是最好的解决方案,我应该建立一个搜索模型?
有什么建议吗?
I have a very complex data structure, something like 10 tables without the join tables. My application needs to be able to perform search in most of the tables.
To do this, I though to turn the content of the search fields into an array of conditions. The key is the model name, the value is the search conditions, i.e.
$conditions = array(
'Artist' => array(
'OR' => array(
'Artist.name LIKE' => '%barl%',
'Pseudonym.name LIKE' => '%barl%'
)
),
'Content' => array('Content.subject' => 'architecture'),
'Editor' => array('Editor.name LIKE' => '%Gal%'),
etc....
)
This array gets passed to the models that are searchable and each model takes the condition that is relevant.
$this->find('all', array('conditions' => $conditions['Artist']))
So far so good, at least I think. Now I started to test the models and I found myself copying over and over that same array in the different model test cases, and that bothers me.
Is there a way to have this array accessible to every test cases? Maybe the array is not the best solution and I should make a search model?
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将数组作为属性放在 app_model.php 中,如 $commonSearchConditions 之类的内容,并从应该继承 AppModel 的模型内部访问它们。
根据您具体执行的操作,如果每个模型中的搜索不同,我将在每个模型测试中都有一个测试用例。如果没有,您可能想使用您在测试内部创建的测试模型创建一个单独的测试,以仅测试您想要执行的搜索内容。如果不了解更多,很难说清楚。
Put the array as property in the app_model.php as something like $commonSearchConditions and access them from inside your models which should inherit the AppModel.
Depending on what exactly you do, if the searches differ in every model, I would have a test-case in every models test. If not you might want to create a separate test with a test model you create inside of the test for testing just the search stuff you want to do. Hard to tell without knowing more.