我创建了这个对象数组:
$ad_1 = new AdUnit(array('id' => '1', 'name' => 'Ad_1', 'description' => 'great ad', 'code' => 'alpha', 'widget_id' => '123'));
$ad_2 = new AdUnit(array('id' => '2', 'name' => 'Ad_2', 'description' => 'good ad', 'code' => 'delta', 'widget_id' => '456'));
$ad_3 = new AdUnit(array('id' => '3', 'name' => 'Ad_3', 'description' => 'bad ad', 'code' => 'sigma', 'widget_id' => '789'));
$adUnitArr = array($ad_1, $ad_2, $ad_3);
Run Code Online (Sandbox Code Playgroud)
我想检查一个函数中存在的随机广告是否存在于数组中.获取广告的代码如下所示:
$fixture = new AdGroup();
$fixture->setAds($adUnitArr);
$randad = $fixture->getRandomAd();
Run Code Online (Sandbox Code Playgroud)
现在我想检查数组是否包含我收到的随机广告,我能够做到这样:
$this->assertEquals(in_array($randad, $adUnitArr), 1); //check if ad in …Run Code Online (Sandbox Code Playgroud) 我正在尝试模拟PDO对象在使用phpunit编写一些测试时使用,但我发现它非常复杂,并且找不到太多关于它的文档.我创建了这个xml结构:
<dataset>
<table name="providers">
<column>id</column>
<column>name</column>
<column>description</column>
<row>
<value>1</value>
<value>provdier_1</value>
<value>phpunit first provider</value>
</row>
</table>
</dataset>
Run Code Online (Sandbox Code Playgroud)
现在我想查询providers表并获取数据,但我不知道如何做到这一点.
我开始嘲笑PDO对象,但我不明白我应该如何使用它以及如何在getConnection()方法中使用它.我的第一次尝试,我猜它离正确的方式很远,因为我在这里很迷失,看起来像这样:
class AdProvidersTest extends PHPUnit_Extensions_Database_TestCase
{
public function getConnection()
{
$dsn = 'mydb';
$user = '';
$password = '';
$pdo = $this->getMockBuilder('PDOMock')
->getMock();
return $this->createDefaultDBConnection($pdo, 'adserverTesting');
}
public function getDataSet()
{
return $this->createXMLDataSet('adserverTesting.xml');
}
}
Run Code Online (Sandbox Code Playgroud)
如何使连接与'adserverTesting.xml'文件交互,如何使用以下行查询它:
$ds = new PHPUnit_Extensions_Database_DataSet_QueryDataSet($this->getConnection());
$ds->addTable('adserverTesting', 'SELECT * FROM providers');
Run Code Online (Sandbox Code Playgroud) 我有这个功能,我想测试看起来像这样:
class Logger {
function error($msg){
if (is_string($msg)){
error_log($msg);
die($msg);
} elseif (is_object($msg)){
error_log($msg.' '.$msg->getTraceAsString());
die('exception');
} else {
var_dump($msg);
die('error');
}
}
Run Code Online (Sandbox Code Playgroud)
我想测试这个功能而不记录$msg.有没有办法确定error_log没有记录是否有效?我尝试使用,setExpectedException但我无法捕获错误,它一直在记录.
我有项目的文件夹dummyProject,里面有两个文件夹,src和test,和composer.json文件。问题是当我运行在文件夹中创建的文件夹的composer install命令时。我如何告诉作曲家在里面创建文件夹?谢谢..vendordummyProjectvendorsrc
像这样模拟PDO对象之后:
class AdProvidersTest extends PHPUnit_Framework_TestCase
{
public function dataProvider()
{
$providers = array (
array (1, '1st', 'desc_1', 101),
array (2, '2nd', 'desc_2', 202),
array (3, '3rd', 'desc_3', 303)
);
return $providers;
}
/**
* @dataProvider dataProvider
*/
public function testAdProviders_getConnection($id, $name, $desc, $account_id)
{
$data = array (
array (
'id' => $id,
'name' => $name,
'desc' => $desc,
'account_id' => $account_id,
)
);
$stmt = $this->getMock('PDOStatement', array ('execute','fetchAll'));
$stmt->expects($this->any())
->method('execute')
->will($this->returnValue(true));
$stmt->expects($this->any())
->method('fetchAll')
->will($this->returnValue($data));
$pdo = $this->getMock('PDO', array('prepare'), …Run Code Online (Sandbox Code Playgroud) 每当我尝试时,都会sudo apt-get update收到以下错误:
W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: http://packages.linuxmint.com rafaela Release: The following signatures were invalid: BADSIG 3EE67F3D0FF405B2 Clement Lefebvre (Linux Mint Package Repository v1) <root@linuxmint.com>
W: GPG error: http://extra.linuxmint.com rafaela Release: The following signatures were invalid: BADSIG 3EE67F3D0FF405B2 Clement Lefebvre (Linux Mint Package Repository v1) <root@linuxmint.com>
W: Failed to fetch http://packages.linuxmint.com/dists/rafaela/Release
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty/Release Unable to find …Run Code Online (Sandbox Code Playgroud) 我遇到了一段代码,其中PDO::FETCH_CLASS没有将类的名称作为参数传递,而是这样:
function getUsers() {
$DBH = $this->getDbh();
$query = "SELECT * FROM users";
try {
$STH = $DBH->query($query);
$users = $STH->fetchAll(PDO::FETCH_CLASS);
} catch(PDOException $e) {
}
return $users;
}
Run Code Online (Sandbox Code Playgroud)
现在,在官方文件中 它说:
如果fetch_style包含PDO :: FETCH_CLASSTYPE(例如PDO :: FETCH_CLASS | PDO :: FETCH_CLASSTYPE),则类的名称由第一列的值确定.
所以我不确定我究竟是什么意思,也不知道它是如何运作的.假设我有users一张记录表
那怎么能PDO::FETCH_CLASS知道什么是班级名字?我可以用类名添加列到我的记录中,FETCH_CLASS从数据库中获取类名吗?记录结构的例子将受到高度赞赏...... thx
*我正在测试这个类而不是作者,所以更改类是无关紧要的
我正在使用node.js迈出第一步,显然我尝试做的第一件事就是从模块中导出一些数据,所以我尝试了这个简单的例子:
dummy.js:
var user = "rally";
module.exports = {
user:user
};
Run Code Online (Sandbox Code Playgroud)
而不是从这样的不同文件中要求它:
var dummy = require('./dummy.js');
console.log(dummy.user); // rally
Run Code Online (Sandbox Code Playgroud)
到目前为止一切顺利,每件事都有效,但现在我潜入了代码,在模块的开头有这个定义:
module.exports = function(passport,config, mongoose) {}
Run Code Online (Sandbox Code Playgroud)
我不明白它的含义是什么,我怎么能用它.只是为了试图理解我在这个抽象函数中定义了一些变量但是无法从任何其他文件中获取它们的值.任何想法如何从像这样定义的模块中导出变量..?所以例如我可以要求这个模块并获取"Dummy"变量并在不同的文件中使用它
module.exports = function(passport,config, mongoose) {
var dummy = "Dummy";
}
Run Code Online (Sandbox Code Playgroud) 我测试的目的是检查函数返回错误404,或者更准确,功能创建呼叫服务器(我使用狂饮),我希望服务器返回404错误,但我无法找到任何@expectedException这有道理因为它是一个错误,但有谁知道如何用phpunit测试这个案例??? 谢谢
如果它可能有帮助,这就是函数的样子(最后一次尝试):
public function testApi_deleteExecuted()
{
$path = '/adserver/src/public/api/rule/288';
$client = new Client(['base_uri' => 'http://10.0.0.38']);
$response = $client->get($path);
$data = json_decode($response->getBody());
$code = $response->getStatusCode();
$this->assertEquals($code, 404);
}
Run Code Online (Sandbox Code Playgroud) 我试图为第一个测试函数内的变量分配一个值,然后在类内的其他测试函数中使用它。
现在在我的代码中,第二个函数由于此错误而失败:
1) ApiAdTest::testApiAd_postedAdCreated
GuzzleHttp\Exception\ClientException: Client error: 404
Run Code Online (Sandbox Code Playgroud)
我不知道为什么。代码如下所示:
class ApiAdTest extends PHPUnit_Framework_TestCase
{
protected $adId;
private static $base_url = 'http://10.0.0.38/adserver/src/public/';
private static $path = 'api/ad/';
//start of expected flow
public function testApiAd_postAd()
{
$client = new Client(['base_uri' => self::$base_url]);
$response = $client->post(self::$path, ['form_params' => [
'name' => 'bellow content - guzzle testing'
]]);
$data = json_decode($response->getBody());
$this->adId = $data->id;
$code = $response->getStatusCode();
$this->assertEquals($code, 200);
}
public function testApiAd_postedAdCreated()
{
$client = new Client(['base_uri' => self::$base_url]);
$response = $client->get(self::$path.$this->adId);
$code …Run Code Online (Sandbox Code Playgroud) php ×8
phpunit ×5
pdo ×3
mysql ×2
apt-get ×1
composer-php ×1
javascript ×1
linux ×1
mocking ×1
node.js ×1
unit-testing ×1