Skip to content

Commit

Permalink
Throw if mock doesn't exist on instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Jul 29, 2021
1 parent 592ae76 commit 9bd5ccf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/class.c
Expand Up @@ -81,15 +81,15 @@ int uopz_find_mock(zend_string *clazz, zend_object **object, zend_class_entry **
}

if (Z_TYPE_P(found) == IS_STRING) {
*mock = zend_lookup_class(Z_STR_P(found));
*mock = zend_fetch_class_by_name(Z_STR_P(found), NULL, ZEND_FETCH_CLASS_EXCEPTION);
return *mock ? SUCCESS : FAILURE;
} else {
*mock = Z_OBJCE_P(found);
if (object) {
*object = Z_OBJ_P(found);
}
return SUCCESS;
}

return SUCCESS;
} /* }}} */

/* {{{ */
Expand Down
10 changes: 6 additions & 4 deletions src/handlers.c
Expand Up @@ -279,10 +279,12 @@ int uopz_vm_new(UOPZ_OPCODE_HANDLER_ARGS) { /* {{{ */

if (opline->op1_type == IS_CONST) {
if (uopz_find_mock(Z_STR_P(EX_CONSTANT(opline->op1)), &obj, &ce) != SUCCESS) {
ce = zend_fetch_class_by_name(
Z_STR_P(EX_CONSTANT(opline->op1)),
Z_STR_P(EX_CONSTANT(opline->op1) + 1),
ZEND_FETCH_CLASS_DEFAULT | ZEND_FETCH_CLASS_EXCEPTION);
if (!EG(exception)) {
ce = zend_fetch_class_by_name(
Z_STR_P(EX_CONSTANT(opline->op1)),
Z_STR_P(EX_CONSTANT(opline->op1) + 1),
ZEND_FETCH_CLASS_DEFAULT | ZEND_FETCH_CLASS_EXCEPTION);
}

if (ce == NULL) {
ZVAL_UNDEF(EX_VAR(opline->result.var));
Expand Down
9 changes: 9 additions & 0 deletions tests/004.phpt
Expand Up @@ -29,6 +29,14 @@ uopz_unset_mock(Foo::class);
uopz_set_mock(Bar::class, new Foo);

var_dump(new Bar());

uopz_set_mock(Foo::class, DoesntExist::class);
try {
var_dump(new Foo);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECTF--
object(Bar)#%d (0) {
Expand All @@ -37,3 +45,4 @@ int(1)
int(-1)
object(Foo)#%d (0) {
}
Class "DoesntExist" not found

0 comments on commit 9bd5ccf

Please sign in to comment.