How to use ArgumentsFormatter class

Best AspectMock code snippet using ArgumentsFormatter

Verifier.php

Source:Verifier.php Github

copy

Full Screen

1<?php2namespace AspectMock\Proxy;3use \PHPUnit_Framework_ExpectationFailedException as fail;4use AspectMock\Util\ArgumentsFormatter;5/**6 * Interface `Verifiable` defines methods to verify method calls.7 * Implementation may differ for class methods and instance methods.8 * 9 */10abstract class Verifier {11 /**12 * Name of a class.13 */14 public $className;15 protected $invokedFail = "Expected %s to be invoked but it never occur.";16 protected $notInvokedMultipleTimesFail = "Expected %s to be invoked %s times but it never occur.";17 protected $invokedMultipleTimesFail = "Expected %s to be invoked but called %s times but called %s.";18 protected $neverInvoked = "Expected %s not to be invoked but it was.";19 abstract public function getCallsForMethod($method);20 protected function callSyntax($method)21 {22 return method_exists($this->className,$method)23 ? '::'24 : '->';25 }26 protected function onlyExpectedArguments($expectedParams, $passedArgs)27 {28 return empty($expectedParams) ?29 $passedArgs :30 array_slice($passedArgs, 0, count($expectedParams));31 }32 /**33 * Verifies a method was invoked at least once.34 * In second argument you can specify with which params method expected to be invoked;35 *36 * ``` php37 * <?php38 * $user->verifyInvoked('save');39 * $user->verifyInvoked('setName',['davert']);40 *41 * ?>42 * ```43 *44 * @param $name45 * @param null $params46 * @throws \PHPUnit_Framework_ExpectationFailedException47 * @param array $params48 * @throws fail49 */50 public function verifyInvoked($name, $params = null)51 {52 $calls = $this->getCallsForMethod($name);53 $separator = $this->callSyntax($name);54 if (empty($calls)) throw new fail(sprintf($this->invokedFail, $this->className.$separator.$name));55 if (is_array($params)) {56 foreach ($calls as $args) {57 if ($this->onlyExpectedArguments($params, $args) === $params) return;58 }59 $params = ArgumentsFormatter::toString($params);60 throw new fail(sprintf($this->invokedFail, $this->className.$separator.$name."($params)"));61 } else if(is_callable($params)) {62 $params($calls);63 }64 }65 /**66 * Verifies that method was invoked only once.67 *68 * @param $name69 * @param array $params70 */71 public function verifyInvokedOnce($name, $params = null)72 {73 $this->verifyInvokedMultipleTimes($name, 1, $params);74 }75 /**76 * Verifies that method was called exactly $times times.77 *78 * ``` php79 * <?php80 * $user->verifyInvokedMultipleTimes('save',2);81 * $user->verifyInvokedMultipleTimes('dispatchEvent',3,['before_validate']);82 * $user->verifyInvokedMultipleTimes('dispatchEvent',4,['after_save']);83 * ?>84 * ```85 *86 * @param $name87 * @param $times88 * @param array $params89 * @throws \PHPUnit_Framework_ExpectationFailedException90 */91 public function verifyInvokedMultipleTimes($name, $times, $params = null)92 {93 if ($times == 0) return $this->verifyNeverInvoked($name, $params);94 $calls = $this->getCallsForMethod($name);95 $separator = $this->callSyntax($name);96 if (empty($calls)) throw new fail(sprintf($this->notInvokedMultipleTimesFail, $this->className.$separator.$name, $times));97 if (is_array($params)) {98 $equals = 0;99 foreach ($calls as $args) {100 if ($this->onlyExpectedArguments($params, $args) == $params) $equals++;101 }102 if ($equals == $times) return;103 $params = ArgumentsFormatter::toString($params);104 throw new fail(sprintf($this->invokedMultipleTimesFail, $this->className.$separator.$name."($params)", $times, $equals));105 } else if(is_callable($params)) {106 $params($calls);107 }108 $num_calls = count($calls);109 if ($num_calls != $times) throw new fail(sprintf($this->invokedMultipleTimesFail, $this->className.$separator.$name, $times, $num_calls));110 }111 /**112 * Verifies that method was not called.113 * In second argument with which arguments is not expected to be called.114 *115 * ``` php116 * <?php117 * $user->setName('davert');118 * $user->verifyNeverInvoked('setName'); // fail119 * $user->verifyNeverInvoked('setName',['davert']); // fail120 * $user->verifyNeverInvoked('setName',['bob']); // success121 * $user->verifyNeverInvoked('setName',[]); // success122 * ?>123 * ```124 *125 * @param $name126 * @param null $params127 * @throws \PHPUnit_Framework_ExpectationFailedException128 */129 public function verifyNeverInvoked($name, $params = null)130 {131 $calls = $this->getCallsForMethod($name);132 $separator = $this->callSyntax($name);133 if (is_array($params)) {134 if (empty($calls)) return;135 $params = ArgumentsFormatter::toString($params);136 foreach ($calls as $args) {137 if ($this->onlyExpectedArguments($params, $args) == $params) throw new fail(sprintf($this->neverInvoked, $this->className));138 }139 return;140 }141 if (count($calls)) throw new fail(sprintf($this->neverInvoked, $this->className.$separator.$name)); 142 }143}...

Full Screen

Full Screen

ArgumentsFormatterTest.php

Source:ArgumentsFormatterTest.php Github

copy

Full Screen

1<?php2namespace Args\Test\Unit;3use Args\Helpers\ArgumentsFormatter;4use Args\Test\TestCase;5class ArgumentsFormatterTest extends TestCase6{7 public function testGetLastOptionIndex()8 {9 self::assertEquals(2, ArgumentsFormatter::getLastOptionIndex(['-a', '-b', '-c']));10 }11 public function testReduceArgs()12 {13 $args = ['-c', 'c_value', '-b', 'b_value', 'operand_1', 'operand_2'];14 list($options, $operands) = array_values(ArgumentsFormatter::reduceArgs($args));15 self::assertEquals('c_value', $options['-c'][0]);16 self::assertEquals('b_value', $options['-b'][0]);17 self::assertEquals('operand_1', $operands[0]);18 self::assertEquals('operand_2', $operands[1]);19 $args = ['-a', '-b', '-b', '--', '-b', 'b_value'];20 list($options, $operands) = array_values(ArgumentsFormatter::reduceArgs($args));21 self::assertEquals(true, $options['-a'][0]);22 self::assertEquals(2, $options['-b'][0]);23 self::assertEquals('-b', $operands[0]);24 self::assertEquals('b_value', $operands[1]);25 $args = ['-a', 'value', '-b', '--'];26 list($options, $operands) = array_values(ArgumentsFormatter::reduceArgs($args));27 self::assertEquals('value', $options['-a'][0]);28 self::assertEquals(true, $options['-b'][0]);29 self::assertEmpty($operands);30 }31 public function testMapOptions()32 {33 $args = ['-c', 'c_value', '-b', 'b_value', 'operand_1', 'operand_2'];34 list($options,) = array_values(ArgumentsFormatter::reduceArgs($args));35 $result = ArgumentsFormatter::mapOptions($options);36 self::assertEquals('-c', $result[0]->getKey());37 self::assertEquals('c_value', $result[0]->getValues()[0]);38 self::assertEquals(0, $result[0]->getIndex());39 self::assertEquals('-b', $result[1]->getKey());40 self::assertEquals('b_value', $result[1]->getValues()[0]);41 self::assertEquals(1, $result[1]->getIndex());42 }43 public function testMapOperands()44 {45 $args = ['-c', 'c_value', '-b', 'b_value', 'operand_1', 'operand_n', 'operand_n'];46 list($options, $operands) = array_values(ArgumentsFormatter::reduceArgs($args));47 $result = ArgumentsFormatter::mapOperands($operands, count($options));48 self::assertEquals('operand_1', $result[0]->getValues()[0]);49 self::assertEquals('operand_n', $result[0]->getValues()[1]);50 self::assertEquals('operand_n', $result[0]->getValues()[2]);51 self::assertEquals(2, $result[0]->getIndex());52 self::assertEquals(0, $result[0]->getOperandIndex());53 }54}...

Full Screen

Full Screen

Loader.php

Source:Loader.php Github

copy

Full Screen

1<?php2namespace Args;3use Args\Helpers\ArgumentsFormatter;4use Args\Loader\InputElement;5use Args\UtilityArgument\Argument;6use Args\UtilityArgument\Block;7use Args\UtilityArgument\Operand;8use Args\UtilityArgument\Option;9class Loader10{11 protected UtilityArgumentString $map;12 private array $options;13 private array $operands;14 public function __construct(UtilityArgumentString $map)15 {16 $this->map = $map;17 global $argv;18 $args = ArgumentsFormatter::expandArgs($argv);19 $args = ArgumentsFormatter::normalizeArgs($args, $this->map);20 list($options, $operands) = array_values(ArgumentsFormatter::reduceArgs($args));21 $this->options = ArgumentsFormatter::mapOptions($options);22 $this->operands = ArgumentsFormatter::mapOperands($operands, count($options));23 }24 public function getInputElementForBlock(Block $block): ?InputElement25 {26 if (is_a($block, Argument::class)) {27 throw new \RuntimeException('Block cannot be an argument');28 }29 if (is_a($block, Option::class)) {30 foreach ($this->options as $option) {31 if ($option->getKey() === $block->getChar(true)) {32 return $option;33 }34 }35 } elseif (is_a($block, Operand::class)) {36 return $this->operands[0] ?? null;...

Full Screen

Full Screen

ArgumentsFormatter

Using AI Code Generation

copy

Full Screen

1use AspectMock\Test as test;2use Codeception\Util\Debug;3use Codeception\Util\Stub;4{5 protected $tester;6 public function testFormat()7 {8 $formatter = new \AspectMock\Proxy\ArgumentsFormatter();9 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar']));10 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], true));11 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], false));12 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], true));13 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], false));14 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], true));15 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], false));16 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], true));17 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], false));18 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], true));19 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], false));20 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], true));21 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], false));22 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], true));23 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], false));24 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], true));25 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], false));26 $this->assertEquals('foo, bar', $formatter->format(['foo', 'bar'], true));

Full Screen

Full Screen

ArgumentsFormatter

Using AI Code Generation

copy

Full Screen

1use AspectMock\Test as test;2{3 public function doSomething()4 {5 echo 'do something';6 }7}8use AspectMock\Test as test;9{10 public function doSomething()11 {12 echo 'do something';13 }14}15use AspectMock\Test as test;16{17 public function doSomething()18 {19 echo 'do something';20 }21}22$mock = test::double('MyClass', ['staticMethod' => 'mocked']);23MyClass::staticMethod();24$mock->verifyInvoked('staticMethod');25$mock = test::double('MyClass', ['method' => 'mocked']);26$myClass = new MyClass();27$myClass->method();28$mock->verifyInvoked('method');29$mock = test::double('MyClass', ['__construct' => 'mocked']);30$myClass = new MyClass();31$mock->verifyInvoked('__construct');32$mock = test::double('MyClass', ['__construct' => 'mocked']);33$myClass = new MyClass('arg1', 'arg2');34$mock->verifyInvoked('__construct', ['arg1', 'arg2']);35use AspectMock\Test as test;

Full Screen

Full Screen

ArgumentsFormatter

Using AI Code Generation

copy

Full Screen

1use AspectMock\Test as test;2{3 public function testAdd()4 {5 $mock = test::double('Calculator', ['add' => 8, 'subtract' => 2]);6 $this->assertEquals(8, $mock->add(2, 2));7 $this->assertEquals(2, $mock->subtract(4, 2));8 $mock->verifyInvoked('add', [2, 2]);9 $mock->verifyInvoked('subtract', [4, 2]);10 $mock->verifyNeverInvoked('add', [1, 2]);11 $mock->verifyNeverInvoked('subtract', [1, 2]);12 }13}

Full Screen

Full Screen

ArgumentsFormatter

Using AI Code Generation

copy

Full Screen

1{2 public function doSomething($arg, $arg2 = 'default')3 {4 return $arg . $arg2;5 }6}7{8 public function testDoSomething()9 {10 $mock = $this->getMockBuilder('Sample')11 ->setMethods(['doSomething'])12 ->getMock();13 $mock->expects($this->once())14 ->method('doSomething')15 ->with(16 $this->equalTo('bar'),17 $this->equalTo('baz')18 ->will($this->returnValue('foo'));19 $this->assertEquals('foo', $mock->doSomething('bar', 'baz'));20 }21}

Full Screen

Full Screen

ArgumentsFormatter

Using AI Code Generation

copy

Full Screen

1use AspectMock\Test as test;2{3 protected function _before()4 {5 $this->mock = test::double('AspectMock\Proxy\ArgumentsFormatter', ['format' => 'formatted string']);6 }7 public function test2()8 {9 $this->mock->format('arg1', 'arg2');10 }11}12use AspectMock\Test as test;13{14 protected function _before()15 {16 $this->mock = test::double('AspectMock\Proxy\ArgumentsFormatter', ['format' => 'formatted string']);17 }18 public function test3()19 {20 $this->mock->format('arg1', 'arg2');21 }22}23use AspectMock\Test as test;24{25 protected function _before()26 {27 $this->mock = test::double('AspectMock\Proxy\ArgumentsFormatter', ['format' => 'formatted string']);28 }29 public function test4()30 {31 $this->mock->format('arg1', 'arg2');32 }33}

Full Screen

Full Screen

ArgumentsFormatter

Using AI Code Generation

copy

Full Screen

1use AspectMock\Test as test;2{3 public function method2()4 {5 return $this->method1(1, 2, 3);6 }7 public function method1($a, $b, $c)8 {9 return $a + $b + $c;10 }11}12{13 public function testMethod2()14 {15 $class2 = new Class2();16 $stub = test::double($class2, [17 'method1' => function () {18 return 10;19 }20 ]);21 $this->assertEquals($class2->method2(), 10);22 }23}24use AspectMock\Test as test;25{26 public function method1()27 {28 return $this->method2(1, 2, 3);29 }30 public function method2($a, $b, $c)31 {32 return $a + $b + $c;33 }34}35{36 public function testMethod1()37 {38 $class1 = new Class1();39 $stub = test::double($class1, [40 'method2' => function () {41 return 10;42 }43 ]);44 $this->assertEquals($class1->method1(), 10);45 }46}47use AspectMock\Test as test;48{49 public function method2()50 {51 return $this->method1(1, 2, 3);52 }53 public function method1($a, $b, $c)54 {55 return $a + $b + $c;56 }57}58{59 public function testMethod2()60 {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run AspectMock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in ArgumentsFormatter

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful