Skip to content

Commit

Permalink
Updated To Facade: set command completed
Browse files Browse the repository at this point in the history
  • Loading branch information
David Schoenbauer committed Dec 21, 2016
1 parent acee3b9 commit 620b604
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 2 deletions.
8 changes: 6 additions & 2 deletions tests/DotNotation/Framework/Command/CommandTestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@
namespace DSchoenbauerTest\DotNotation\Framework\Command;

use DSchoenbauer\DotNotation\ArrayDotNotation;
use DSchoenbauer\DotNotation\Framework\Command\AbstractCommand;
use PHPUnit_Framework_TestCase;

/**
* Description of CommandTestHelper
*
* @author David Schoenbauer <dschoenbauer@gmail.com>
*/
class CommandTestHelper extends \PHPUnit_Framework_TestCase{
class CommandTestHelper extends PHPUnit_Framework_TestCase{

protected function getArrayDotNotation($data = [], $notationType = '.',$wildCardCharacter = "*"){
return ArrayDotNotation::with($data)->setNotationType($notationType)->setWildCardCharacter($wildCardCharacter);
/*
$arrayDotNotation = $this->getMockBuilder(ArrayDotNotation::class)
->setConstructorArgs([$data])
->getMock();
Expand All @@ -45,5 +47,7 @@ protected function getArrayDotNotation($data = [], $notationType = '.',$wildCard
$arrayDotNotation->expects($this->any())->method('getData')->willReturn($data);
return $arrayDotNotation;
*
*/
}
}
138 changes: 138 additions & 0 deletions tests/DotNotation/Framework/Command/SetCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

/*
* The MIT License
*
* Copyright 2016 David Schoenbauer <dschoenbauer@gmail.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace DSchoenbauer\DotNotation\Framework\Command;

use DSchoenbauerTest\DotNotation\Framework\Command\CommandTestHelper;

/**
* Description of SetCommandTest
*
* @author David Schoenbauer <dschoenbauer@gmail.com>
*/
class SetCommandTest extends CommandTestHelper {

private $_object;

protected function setUp() {
$data = [
'levelA' => [
'levelB' => 'someValueB',
'levelB2' => [
'levelC2' => [
'levelD2' => 'someValueD2'
]
]
],
'levelB' => 'levelB',
'level1' => [
'level2' => 'someValue2'
]
];
$this->_object = new SetCommand($this->getArrayDotNotation($data));
}

public function testSetInitialLevelNewValue() {
$this->assertEquals('noValue', $this->_object->getArrayDotNotation()->get('levelD', 'noValue'));
$this->assertEquals('newValue', $this->_object->set('levelD', 'newValue')->getArrayDotNotation()->get('levelD'));
}

public function testSetDeepLevelNewValue() {
$this->assertEquals('noValue', $this->_object->getArrayDotNotation()->get('LevelA.LevelB.LevelC.levelD', 'noValue'));
$this->assertEquals('newValue', $this->_object->set('LevelA.LevelB.LevelC.levelD', 'newValue')->getArrayDotNotation()->get('LevelA.LevelB.LevelC.levelD'));
$this->assertEquals('someValue2', $this->_object->getArrayDotNotation()->get('level1.level2'), "existing value compromised");
}

/**
* @expectedException UnexpectedValueException
* @expectedExceptionMessage Array dot notation path key 'c' is not an array
*/
public function testSetNotExistArrayButString() {
$data = ['a' => ['b' => ['c' => 'cValue']]];
$newData = ['e' => 'dValue'];
$this->_object->getArrayDotNotation()->setData($data);
$this->_object->set('a.b.c.d', $newData);
}

public function testSetInitialLevelExistingValue() {
$this->assertEquals('levelB', $this->_object->getArrayDotNotation()->get('levelB'));
$this->assertEquals('newValue', $this->_object->set('levelB', 'newValue')->getArrayDotNotation()->get('levelB'));
$this->assertEquals('someValue2', $this->_object->getArrayDotNotation()->get('level1.level2'), "existing value compromised");
}

public function testSetDeepLevelExistingValue() {
$this->assertEquals('someValueB', $this->_object->getArrayDotNotation()->get('levelA.levelB'));
$this->assertEquals('newValue', $this->_object->set('levelA.levelB', 'newValue')->getArrayDotNotation()->get('levelA.levelB'));
$this->assertEquals('someValue2', $this->_object->getArrayDotNotation()->get('level1.level2'), "existing value compromised");
}

public function testSetDataTypeConversion() {
$this->assertEquals(['levelB' => 'someValueB', 'levelB2' => ['levelC2' => ['levelD2' => 'someValueD2']]], $this->_object->getArrayDotNotation()->get('levelA'));
$this->assertEquals('newValue', $this->_object->set('levelA', 'newValue')->getArrayDotNotation()->get('levelA'));
$this->assertEquals('someValue2', $this->_object->getArrayDotNotation()->get('level1.level2'), "existing value compromised");
}

public function testSetWildCardForShallowPath() {
$data = [
["id" => 1, "name" => "one"],
["id" => 2, "name" => "two"],
["id" => 3, "name" => "three"],
["id" => 4, "name" => "four"],
["id" => 5, "name" => "five"],
];
$results = [
["id" => 1, "email" => null, "name" => "one"],
["id" => 2, "email" => null, "name" => "two"],
["id" => 3, "email" => null, "name" => "three"],
["id" => 4, "email" => null, "name" => "four"],
["id" => 5, "email" => null, "name" => "five"],
];
$this->_object->getArrayDotNotation()->setData($data);
$this->assertEquals($results, $this->_object->set('*.email', null)->getArrayDotNotation()->getData());
}

public function testSetWildCardForADeepPath() {
$data = ["level1" => ["level2" => ["level3" => [
["id" => 1, "name" => "one"],
["id" => 2, "name" => "two"],
["id" => 3, "name" => "three"],
["id" => 4, "name" => "four"],
["id" => 5, "name" => "five"],
]]]
];
$results = ["level1" => ["level2" => ["level3" => [
["id" => 1, "email" => null, "name" => "one"],
["id" => 2, "email" => null, "name" => "two"],
["id" => 3, "email" => null, "name" => "three"],
["id" => 4, "email" => null, "name" => "four"],
["id" => 5, "email" => null, "name" => "five"],
]]]
];
$this->_object->getArrayDotNotation()->setData($data);
$this->assertEquals($results, $this->_object->set('level1.level2.level3.*.email', null)->getArrayDotNotation()->getData());
}

}

0 comments on commit 620b604

Please sign in to comment.