Skip to content

Commit 36c0b21

Browse files
committed
added test cases for Git2\Config::store and delete.
1 parent 4fe6eff commit 36c0b21

File tree

3 files changed

+177
-9
lines changed

3 files changed

+177
-9
lines changed

Diff for: config.c

+111-9
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_config_get, 0,0,1)
5555
ZEND_ARG_INFO(0, get)
5656
ZEND_END_ARG_INFO()
5757

58+
ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_config_store, 0,0,2)
59+
ZEND_ARG_INFO(0, key)
60+
ZEND_ARG_INFO(0, value)
61+
ZEND_END_ARG_INFO()
62+
63+
ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_config_delete, 0,0,1)
64+
ZEND_ARG_INFO(0, key)
65+
ZEND_END_ARG_INFO()
66+
5867
typedef struct{
5968
zval *result;
6069
git_config *config;
@@ -151,6 +160,32 @@ static int php_git2_config_foreach(const char *var_name, const char *value, void
151160
return GIT_SUCCESS;
152161
}
153162

163+
static int php_git2_config_reload(zval *object, unsigned short dtor TSRMLS_DC)
164+
{
165+
zval *entry;
166+
php_git2_config_foreach_t payload;
167+
php_git2_config *m_config;
168+
int error = 0;
169+
170+
m_config = PHP_GIT2_GET_OBJECT(php_git2_config, object);
171+
entry = zend_read_property(git2_config_class_entry, object,"configs",sizeof("configs")-1, 1 TSRMLS_CC);
172+
if (entry != NULL) {
173+
zval_ptr_dtor(&entry);
174+
entry = NULL;
175+
}
176+
177+
MAKE_STD_ZVAL(entry);
178+
array_init(entry);
179+
180+
payload.config = m_config->config;
181+
payload.result = entry;
182+
error = git_config_foreach(m_config->config,&php_git2_config_foreach,&payload);
183+
add_property_zval(object, "configs", entry);
184+
if (dtor == 1) {
185+
zval_ptr_dtor(&entry);
186+
}
187+
}
188+
154189
/*
155190
{{{ proto: Git2\Config::__construct(string $path)
156191
*/
@@ -174,15 +209,11 @@ PHP_METHOD(git2_config, __construct)
174209
m_config = PHP_GIT2_GET_OBJECT(php_git2_config, getThis());
175210
m_config->config = config;
176211

177-
/* @todo: think solution */
178-
MAKE_STD_ZVAL(config_array);
179-
array_init(config_array);
180-
181-
payload.config = config;
182-
payload.result = config_array;
183-
error = git_config_foreach(config,&php_git2_config_foreach,&payload);
184-
add_property_zval(getThis(), "configs", config_array);
185-
zval_ptr_dtor(&config_array);
212+
php_git2_config_reload(getThis(), 1 TSRMLS_CC);
213+
/**
214+
* @todo: support global config
215+
* php_array_merge(HashTable *dest, HashTable *src, int recursive TSRMLS_DC);
216+
*/
186217
}
187218
/* }}} */
188219

@@ -213,9 +244,80 @@ PHP_METHOD(git2_config, get)
213244
/* }}} */
214245

215246

247+
/*
248+
{{{ proto: Git2\Config::store(string $key, mixed $value)
249+
*/
250+
PHP_METHOD(git2_config, store)
251+
{
252+
char *key;
253+
int error, key_len = 0;
254+
zval *result, *value, *entry;
255+
php_git2_config *m_config;
256+
php_git2_config_foreach_t payload;
257+
258+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
259+
"sz", &key, &key_len, &value) == FAILURE) {
260+
return;
261+
}
262+
263+
if (key_len < 1) {
264+
RETURN_FALSE;
265+
}
266+
267+
m_config = PHP_GIT2_GET_OBJECT(php_git2_config, getThis());
268+
269+
switch (Z_TYPE_P(value)) {
270+
case IS_STRING:
271+
error = git_config_set_string(m_config->config, key, Z_STRVAL_P(value));
272+
break;
273+
case IS_BOOL:
274+
error = git_config_set_bool(m_config->config, key, Z_BVAL_P(value));
275+
break;
276+
case IS_LONG:
277+
error = git_config_set_int32(m_config->config, key, Z_LVAL_P(value));
278+
break;
279+
default:
280+
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC,
281+
"Git2\\Config::store must pass string or bool or long value");
282+
RETURN_FALSE;
283+
}
284+
285+
php_git2_config_reload(getThis(),0 TSRMLS_CC);
286+
}
287+
/* }}} */
288+
289+
290+
/*
291+
{{{ proto: Git2\Config::delete(string $key)
292+
*/
293+
PHP_METHOD(git2_config, delete)
294+
{
295+
char *key;
296+
int error, key_len = 0;
297+
zval *result, *entry;
298+
php_git2_config *m_config;
299+
php_git2_config_foreach_t payload;
300+
301+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
302+
"s", &key, &key_len) == FAILURE) {
303+
return;
304+
}
305+
306+
if (key_len < 1) {
307+
RETURN_FALSE;
308+
}
309+
310+
m_config = PHP_GIT2_GET_OBJECT(php_git2_config, getThis());
311+
git_config_delete(m_config->config, key);
312+
php_git2_config_reload(getThis(), 0 TSRMLS_CC);
313+
}
314+
/* }}} */
315+
216316
static zend_function_entry php_git2_config_methods[] = {
217317
PHP_ME(git2_config, __construct, arginfo_git2_config___construct, ZEND_ACC_PUBLIC)
218318
PHP_ME(git2_config, get, arginfo_git2_config_get, ZEND_ACC_PUBLIC)
319+
PHP_ME(git2_config, store, arginfo_git2_config_store, ZEND_ACC_PUBLIC)
320+
PHP_ME(git2_config, delete, arginfo_git2_config_delete, ZEND_ACC_PUBLIC)
219321
{NULL,NULL,NULL}
220322
};
221323

Diff for: tests/00a-03-config_store.phpt

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
Check for Git2\Config::get
3+
--SKIPIF--
4+
<?php if (!extension_loaded("git2")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$path = __DIR__ . '/fixtures/testrepo.git/config';
8+
$data = file_get_contents($path);
9+
$path = __DIR__ . '/fixtures/testrepo.git/config.test';
10+
file_put_contents($path,$data);
11+
12+
$config = new Git2\Config($path);
13+
echo $config->get("core.bare") . PHP_EOL;
14+
echo $config->store("core.bare",false);
15+
echo $config->get("core.bare") . PHP_EOL;
16+
echo $config->store("core.bare",true);
17+
echo $config->get("core.bare") . PHP_EOL;
18+
echo $config->store("core.bare",false);
19+
echo $config->get("core.bare") . PHP_EOL;
20+
echo $config->store("core.bare",1);
21+
echo $config->get("core.bare") . PHP_EOL;
22+
echo $config->store("core.bare",0);
23+
echo $config->get("core.bare") . PHP_EOL;
24+
echo $config->store("core.bare","1");
25+
echo $config->get("core.bare") . PHP_EOL;
26+
echo $config->store("core.bare","0");
27+
echo $config->get("core.bare") . PHP_EOL;
28+
try {
29+
$config->store("core.bare",array());
30+
echo "FAIL\n";
31+
} catch (\Exception $e) {
32+
echo "OK\n";
33+
}
34+
unlink($path);
35+
--EXPECT--
36+
true
37+
false
38+
true
39+
false
40+
1
41+
0
42+
1
43+
0
44+
OK

Diff for: tests/00a-04-config_delete.phpt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Check for Git2\Config::get
3+
--SKIPIF--
4+
<?php if (!extension_loaded("git2")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$path = __DIR__ . '/fixtures/testrepo.git/config';
8+
$data = file_get_contents($path);
9+
$path = __DIR__ . '/fixtures/testrepo.git/config.test';
10+
file_put_contents($path,$data);
11+
12+
$config = new Git2\Config($path);
13+
echo $config->get("core.bare") . PHP_EOL;
14+
$config->delete("core.bare");
15+
var_dump($config->get("core.bare"));
16+
//for now, recursive delete does not support.
17+
//$config->delete("core");
18+
//var_dump($config->get("core"));
19+
unlink($path);
20+
--EXPECT--
21+
true
22+
NULL

0 commit comments

Comments
 (0)