getFunctionMock('Zotlabs\Lib', 'openssl_pkey_new') ->expects($this->any()) ->willReturn('openssl_pkey_result'); // // Since we have stubbed `openssl_pkey_new` we also need to mock the // call to `openssl_pkey_export` that is used to get the private key // from the result of `openssl_pkey_new`. // // As this function returns the value in a output param, we need to // replace it with a callback function to simulate it. // $openssl_pkey_export = $this->getFunctionMock('Zotlabs\Lib', 'openssl_pkey_export') ->expects($this->any()) ->willReturnCallback(function (string $result, string &$response): bool { $this->assertEquals('openssl_pkey_result', $result); $response = self::PRVKEY; return true; }); // // Finally we also need to stub the `openssl_pkey_get_details` function // used to retrevive the public key from the `openssl_pkey_new` result. // $openssl_pkey_get_details = $this->getFunctionMock('Zotlabs\Lib', 'openssl_pkey_get_details') ->expects($this->any()) ->with($this->identicalTo('openssl_pkey_result')) ->willReturn([ 'key' => self::PUBKEY ]); } public function test_empty_args_fails_with_an_error_message() { insert_hook('proc_run', [$this, 'proc_run_hook']); $result = create_identity([]); $this->assertEquals( ['success' => false, 'message' => 'No account identifier'], $result); $this->assertFalse($this->queueworker_started); } public function test_create_new_channel_with_valid_account_id(): void { insert_hook('proc_run', [$this, 'proc_run_hook']); $result = create_identity([ 'account_id' => $this->fixtures['account'][0]['account_id'], 'nickname' => 'testuser', 'name' => 'Olga Testuser', ]); $this->assertTrue($result['success']); $this->assertTrue($this->queueworker_started); } public function test_create_new_channel_with_nnexistant_account_id(): void { insert_hook('proc_run', [$this, 'proc_run_hook']); $result = create_identity([ 'account_id' => 666, 'nickname' => 'testuser', 'name' => 'Olga Testuser', ]); /* * We would expect this fo fail, but... * * The create_identity function will happily create a new channel with an * non-existent account_id. The New_channel module will perform a check * to ensure that only valid (and logged in) accounts can create a new channel. * * This is a bit weak, but for now we let it pass... */ $this->assertTrue($result['success']); $this->assertTrue($this->queueworker_started); } public function proc_run_hook(array &$args): void { $args['run_cmd'] = false; $this->queueworker_started = $args['args'] === ['php', 'Zotlabs/Daemon/Master.php', 'Queueworker']; } }