diff --git a/docs/examples/node/test_example.php b/docs/examples/node/test_example.php index d6efaef..8dbf18f 100644 --- a/docs/examples/node/test_example.php +++ b/docs/examples/node/test_example.php @@ -24,7 +24,7 @@ ->onAnswer(true) ->onCreateNode('mainMenu') ->runWithInput('888') - ->expectSaySound('pp/50', 2) + ->assertSaySound('pp/50', 2) ->assertMaxInputAttemptsReached() ; $pagiApp->init(); diff --git a/src/mg/PAGI/Node/MockedNode.php b/src/mg/PAGI/Node/MockedNode.php index 991415a..08030c2 100644 --- a/src/mg/PAGI/Node/MockedNode.php +++ b/src/mg/PAGI/Node/MockedNode.php @@ -71,7 +71,7 @@ class MockedNode extends Node * * @return MockedNode */ - public function expectSaySound($filename, $totalTimes) + public function assertSaySound($filename, $totalTimes) { $this->expectedSaySound[$filename] = $totalTimes; $this->doneSaySound[$filename] = 0; @@ -101,18 +101,6 @@ public function assertComplete() return $this; } - /** - * Assert that this node is in state of max input attempts reached - * after run(). - * - * @return MockedNode - */ - public function assertTimeout() - { - $this->expectedState = Node::STATE_TIMEOUT; - return $this; - } - /** * Assert that this node is in state of max input attempts reached * after run(). @@ -134,7 +122,7 @@ public function assertMaxInputAttemptsReached() */ public function runWithInput($digits) { - preg_match_all('/([0-9*# ])/', $digits, $matches); + preg_match_all('/([0-9*# X])/', $digits, $matches); $this->mockedInput = $matches[1]; return $this; } @@ -147,13 +135,14 @@ public function run() { $result = parent::run(); foreach ($this->expectedSaySound as $filename => $times) { - if (!isset($this->doneSaySound[$filename])) { - throw new MockedException("$filename was never played"); - } - if ($times != $this->doneSaySound[$filename]) { + $playedTimes = isset($this->doneSaySound[$filename]) + ? $this->doneSaySound[$filename] + : 0 + ; + if ($times != $playedTimes) { throw new MockedException( "$filename expected to be played $times times, was " - . "played {$this->doneSaySound[$filename]} times" + . "played $playedTimes times" ); } } diff --git a/test/mock/Test_Node.php b/test/mock/Test_Node.php new file mode 100644 index 0000000..6a947bc --- /dev/null +++ b/test/mock/Test_Node.php @@ -0,0 +1,246 @@ + + * @license http://marcelog.github.com/ Apache License 2.0 + * @version SVN: $Id$ + * @link http://marcelog.github.com/ + * + * Copyright 2011 Marcelo Gornstein + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +use PAGI\Client\Impl\MockedClientImpl; +use PAGI\Node\MockedNode; +use PAGI\Node\Node; + +/** + * This class will test the node mock + * + * PHP Version 5 + * + * @category Pagi + * @package Test + * @subpackage Mock + * @author Marcelo Gornstein + * @license http://marcelog.github.com/ Apache License 2.0 + * @link http://marcelog.github.com/ + */ +class Test_MockedNode extends PHPUnit_Framework_TestCase +{ + private $properties = array(); + private $client; + + public function setUp() + { + $this->properties = array( + 'log4php.properties' => RESOURCES_DIR . DIRECTORY_SEPARATOR . 'log4php.properties', + ); + $this->client = new MockedClientImpl($this->properties); + } + + /** + * @test + */ + public function can_skip_input_on_uninterruptable_message() + { + $this->client + ->onCreateNode(__METHOD__) + ->runWithInput('1') + ; + $node = $this->client + ->createNode(__METHOD__) + ->unInterruptablePrompts() + ->expectExactly(1) + ->saySound('prompt') + ->run() + ; + } + + /** + * @test + */ + public function can_consume_whitespace_as_no_input() + { + $this->client + ->onCreateNode(__METHOD__) + ->runWithInput(' ') + ; + $node = $this->client + ->createNode(__METHOD__) + ->expectExactly(3) + ->saySound('prompt') + ->playOnNoInput('no-input') + ->run() + ; + } + + /** + * @test + * @expectedException PAGI\Exception\MockedException + */ + public function can_assert_wrong_state() + { + $this->client + ->onCreateNode(__METHOD__) + ->assertCancelled() + ->runWithInput('*') + ; + $node = $this->client + ->createNode(__METHOD__) + ->expectExactly(1) + ->run() + ; + } + + /** + * @test + * @expectedException PAGI\Exception\MockedException + */ + public function can_assert_sound_not_played() + { + $this->client + ->onCreateNode(__METHOD__) + ->assertSaySound('max-attempts', 1) + ->assertSaySound('no-input', 1) + ->assertSaySound('prompt', 99) + ->assertMaxInputAttemptsReached() + ->runWithInput('1') + ; + $node = $this->client + ->createNode(__METHOD__) + ->expectExactly(1) + ->validateInputWith( + 'fails', + function (Node $node) { + return false; + }, + 'invalid' + ) + ->maxAttemptsForInput(3) + ->saySound('prompt') + ->playOnNoInput('no-input') + ->playOnMaxValidInputAttempts('max-attempts') + ->run() + ; + } + /** + * @test + */ + public function can_assert_play_sound() + { + $this->client + ->onCreateNode(__METHOD__) + ->assertSaySound('max-attempts', 1) + ->assertSaySound('no-input', 1) + ->assertSaySound('prompt', 3) + ->assertMaxInputAttemptsReached() + ->runWithInput('1') + ; + $node = $this->client + ->createNode(__METHOD__) + ->expectExactly(1) + ->validateInputWith( + 'fails', + function (Node $node) { + return false; + }, + 'invalid' + ) + ->maxAttemptsForInput(3) + ->saySound('prompt') + ->playOnNoInput('no-input') + ->playOnMaxValidInputAttempts('max-attempts') + ->run() + ; + } + /** + * @test + */ + public function can_assert_max_attempts_reached() + { + $this->client + ->onCreateNode(__METHOD__) + ->assertMaxInputAttemptsReached() + ->runWithInput('111') + ; + $node = $this->client + ->createNode(__METHOD__) + ->expectExactly(1) + ->validateInputWith('fails', function (Node $node) { return false; }) + ->maxAttemptsForInput(3) + ->run() + ; + } + + /** + * @test + */ + public function can_assert_complete_node() + { + $this->client + ->onCreateNode(__METHOD__) + ->assertComplete() + ->runWithInput('12345678901#') + ; + $node = $this->client + ->createNode(__METHOD__) + ->expectAtLeast(10) + ->endInputWith(Node::DTMF_HASH) + ->expectAtMost(12) + ->run() + ; + } + + /** + * @test + */ + public function can_assert_cancelled_node() + { + $this->client + ->onCreateNode(__METHOD__) + ->assertCancelled() + ->runWithInput('*') + ; + $node = $this->client + ->createNode(__METHOD__) + ->cancelWith(Node::DTMF_STAR) + ->expectExactly(1) + ->run() + ; + } + + /** + * @test + */ + public function can_skip_input_on_non_interrupt_digit_input() + { + $this->client + ->onCreateNode(__METHOD__) + ->runWithInput('X X') + ; + $node = $this->client + ->createNode(__METHOD__) + ->expectExactly(1) + ->saySound('prompt') + ->run() + ; + } +} \ No newline at end of file