/** * 测试connect_ex接口 * 测试init_api过程 */ public function testConnectEx() { $subject = new MetaAgentAdapter(); $pipe_name = 'pipe'; $token = 'token'; $role = BStompRoleType::PUBLISHER; // 测试1 connect还未被初始化的对象 $this->assertFalse($subject->connect_ex($pipe_name, $token, $role)); // 测试2 init api $this->assertTrue(TestUtilities::set_private_var($subject, '_inited', true)); // 测试2.1 package init meta 包失败 $this->assertTrue(TestUtilities::set_private_var($subject, '_conf', $this->conf)); $this->assertFalse($subject->connect_ex($pipe_name, $token, $role)); // 补全conf $this->conf->meta['meta_host'] = '0.0.0.0:0'; $this->conf->meta['root_path'] = '/root'; $this->assertTrue(TestUtilities::set_private_var($subject, '_conf', $this->conf)); // mock connection 行为 // 定义connection行为 $this->stub_conn->expects($this->any())->method('is_connected')->will($this->returnValue(true)); $this->stub_conn->expects($this->any())->method('create_connection')->will($this->returnValue(true)); $this->stub_conn->expects($this->any())->method('send')->will($this->returnValue(true)); $this->stub_conn->expects($this->any())->method('close'); $ack_pkgs = $this->_gen_init_api_ack(); $this->stub_conn->expects($this->any())->method('receive')->will($this->onConsecutiveCalls(null, $ack_pkgs['bad'], $ack_pkgs['failed'], $ack_pkgs['passed'])); $this->assertTrue(TestUtilities::set_private_var($subject, '_connection', $this->stub_conn)); // 测试2.2 request null $this->assertFalse($subject->connect_ex($pipe_name, $token, $role)); // 测试2.3 ack wrong pakcage $this->assertFalse($subject->connect_ex($pipe_name, $token, $role)); // 测试2.4 认证失败 $this->assertFalse($subject->connect_ex($pipe_name, $token, $role)); // 测试2.5 成功 $this->assertEquals(10, $subject->connect_ex($pipe_name, $token, $role)); $this->assertTrue(TestUtilities::set_private_var($subject, '_inited', false)); }