[ Index ] |
PHP Cross Reference of GlotPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 class GP_Thing_Test_Factory { 4 function create( $args ) { 5 6 } 7 8 function save( $args ) { 9 } 10 } 11 12 class GP_Test_Unittest_Factory extends GP_UnitTestCase { 13 14 function create_factory( $field_names = array(), $defaults = array() ) { 15 $thing = (object)compact( 'field_names' ); 16 $factory = new GP_UnitTest_Factory_For_Thing( null, $thing, $defaults ); 17 return $factory; 18 } 19 20 function test_generator_sequence_should_start_with_1() { 21 $sequence = new GP_UnitTest_Generator_Sequence(); 22 $this->assertEquals( 1, $sequence->next() ); 23 } 24 25 function test_generator_sequence_should_generate_consecutive_values() { 26 $sequence = new GP_UnitTest_Generator_Sequence(); 27 $this->assertEquals( 1, $sequence->next() ); 28 $this->assertEquals( 2, $sequence->next() ); 29 $this->assertEquals( 3, $sequence->next() ); 30 $this->assertEquals( 4, $sequence->next() ); 31 } 32 33 function test_generator_sequence_should_include_value_in_template() { 34 $sequence = new GP_UnitTest_Generator_Sequence( 'Baba %s Dyado' ); 35 $this->assertEquals( 'Baba 1 Dyado', $sequence->next() ); 36 } 37 38 function test_generator_sequence_should_start_with_2() { 39 $sequence = new GP_UnitTest_Generator_Sequence( '%s', 2 ); 40 $this->assertEquals( 2, $sequence->next() ); 41 } 42 43 function test_generator_sequence_should_generate_consecutive_values_in_template() { 44 $sequence = new GP_UnitTest_Generator_Sequence( 'Baba %s' ); 45 $this->assertEquals( 'Baba 1', $sequence->next() ); 46 $this->assertEquals( 'Baba 2', $sequence->next() ); 47 $this->assertEquals( 'Baba 3', $sequence->next() ); 48 } 49 50 function test_generator_locale_name_should_start_with_aa() { 51 $locale_name = new GP_UnitTest_Generator_Locale_Name; 52 $this->assertEquals( 'aa', $locale_name->next() ); 53 } 54 55 function test_generator_locale_name_should_generate_consecutive_values() { 56 $locale_name = new GP_UnitTest_Generator_Locale_Name; 57 $this->assertEquals( 'aa', $locale_name->next() ); 58 $this->assertEquals( 'ab', $locale_name->next() ); 59 $this->assertEquals( 'ac', $locale_name->next() ); 60 $this->assertEquals( 'ad', $locale_name->next() ); 61 } 62 63 function test_factory_for_thing_should_construct_with_factory_and_thing_object() { 64 $factory = $this->create_factory(); 65 $this->assertTrue( is_object( $factory->thing ) ); 66 } 67 68 function test_factory_for_thing_generate_args_should_not_touch_args_if_no_generation_definitions() { 69 $factory = $this->create_factory( array('name') ); 70 $args = array( 'name' => 'value' ); 71 $this->assertEquals( $args, $factory->generate_args( $args, array() ) ); 72 } 73 74 function test_factory_for_thing_generate_args_should_not_touch_args_if_different_generation_defintions() { 75 $factory = $this->create_factory( array('name') ); 76 $args = array( 'name' => 'value' ); 77 $this->assertEquals( $args, $factory->generate_args( $args, array( 'other_name' => 5 ) ) ); 78 } 79 80 function test_factory_for_thing_generate_args_should_set_undefined_scalar_values() { 81 $factory = $this->create_factory( array('name') ); 82 $this->assertEquals( array('name' => 'default'), $factory->generate_args( array(), array( 'name' => 'default' ) ) ); 83 } 84 85 function test_factory_for_thing_generate_args_should_use_generator() { 86 $generator_stub = $this->getMockBuilder( 'GP_UnitTest_Generator_Sequence' )->getMock(); 87 $generator_stub->expects( $this->exactly( 2 ) )->method( 'next' )->will( $this->onConsecutiveCalls( 'name 1', 'name 2' ) ); 88 $factory = $this->create_factory( array('name') ); 89 $generation_defintions = array( 'name' => $generator_stub ); 90 $this->assertEquals( array( 'name' => 'name 1'), $factory->generate_args( array(), $generation_defintions ) ); 91 $this->assertEquals( array( 'name' => 'name 2'), $factory->generate_args( array(), $generation_defintions ) ); 92 } 93 94 function test_factory_for_thing_generate_args_should_return_error_on_bad_default_value() { 95 $factory = $this->create_factory( array('name') ); 96 $this->assertWPError( $factory->generate_args( array(), array( 'name' => array( 'non-scalar default value' ) ) ) ); 97 } 98 99 function test_factory_for_thing_generate_args_should_use_default_generator_definition_if_non_given() { 100 $factory = $this->create_factory( array('name'), array('name' => 'default') ); 101 $this->assertEquals( array('name' => 'default'), $factory->generate_args( array() ) ); 102 } 103 104 function test_factory_for_thing_create_should_call_create_once() { 105 $factory = $this->create_factory(); 106 $create_args = array( 'name' => 'value' ); 107 $thing = $this->create_thing_mock_with_name_field_and_with_create_which_should_be_called_once_with( $create_args ); 108 $factory->thing = $thing; 109 $factory->create( $create_args ); 110 } 111 112 private function create_thing_mock_with_name_field_and_with_create_which_should_be_called_once_with( $expected_create_args ) { 113 $thing = $this->getMockBuilder( 'GP_Thing_Test_Factory' )->getMock(); 114 $thing->field_names = array('name'); 115 $thing->expects( $this->once() )->method( 'create' )->with( $this->equalTo( $expected_create_args ) ); 116 return $thing; 117 } 118 119 function test_factory_for_thing_create_should_use_function_generator() { 120 $generation_defintions = array( 121 'full_name' => GP_UnitTest_Factory_For_Thing::callback( function( $o ) { 122 return $o->name . " baba"; 123 } ), 124 ); 125 $factory = $this->create_factory( null, $generation_defintions ); 126 $create_args = array('name' => 'my name is'); 127 $updated_args = array('full_name' => 'my name is baba'); 128 $thing = $this->create_thing_stub_with_name_and_full_name_which_on_create_returns_mock_whose_save_should_be_called_with( $create_args, $updated_args ); 129 $factory->thing = $thing; 130 $factory->create( $create_args ); 131 } 132 133 private function create_thing_stub_with_name_and_full_name_which_on_create_returns_mock_whose_save_should_be_called_with( $create_args, $expected_save_args ) { 134 $thing = $this->getMockBuilder( 'GP_Thing_Test_Factory' )->getMock(); 135 $thing->field_names = array('name', 'full_name'); 136 $created_thing = $this->getMockBuilder( 'GP_Thing_Test_Factory' )->getMock(); 137 foreach( $create_args as $name => $value ) { 138 $created_thing->$name = $value; 139 } 140 $created_thing->expects( $this->once() )->method( 'save' )->with( $this->equalTo( $expected_save_args ) ); 141 $thing->expects( $this->once() )->method( 'create' )->will( $this->returnValue( $created_thing ) ); 142 return $thing; 143 } 144 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sat Nov 23 01:01:06 2024 | Cross-referenced by PHPXref 0.7.1 |