Skip to content

Instantly share code, notes, and snippets.

@yuchitung
Last active August 22, 2019 10:15
Show Gist options
  • Save yuchitung/6e0a7fe1847046b965efd343be239d9c to your computer and use it in GitHub Desktop.
Save yuchitung/6e0a7fe1847046b965efd343be239d9c to your computer and use it in GitHub Desktop.
class ShipmentManager extends StockManager implements Receivable
{
use EmployeeTrait;
public function receive(ProductDTO $product)
{
$qty = $product->getQuantity();
$stock = $this->stockRepository
->skipPresenter(true)
->firstOrCreate([
'product_id' => $product->getId(),
'warehouse_id' => config('setting.warehouse.taipei'),
]);
$stock->quantity = $stock->quantity + (int)$qty;
$stock->save();
$this->stockMvtRepository->create(
[
'stock_id' => $stock->id,
'quantity' => $qty,
'sign' => 1,
'employee_id' => $this->getEmployee()->id,
]
);
return $this;
}
}
public function test_receive()
{
/** arrange */
$product = new ProductDTO();
$employee = new \stdClass();
//assign ...
$mock = Mockery::mock(ShipmentManager::class)->makePartial();
$mock->shouldReceive('getEmployee')->andReturn($employee);
$shipmentManager = $this->app->make(ShipmentManager::class);//應該是這裡沒有處理好
/** act */
$shipmentManager->receive($product);
/** assert */
//從 model 取出 data 看數量有沒有+1
}
@mouson
Copy link

mouson commented Aug 22, 2019

use Mockery as m;
use PHPUnit\Framework\TestCase;

class SampleTest extends TestCase
{
    protected function setUp()
    {
        parent::setUp();
    }

    protected function tearDown()
    {
        m::close();
    }

    /**
     * @test
     */
    public function testTrait()
    {
        /** @var SampleTraitDemo|m\Mock $target */
        $target = m::mock(SampleTraitDemo::class)->makePartial();
        $target->shouldReceive('thisIsATrait')
               ->andReturn('i am mock');

        $expected = 'use Trait - i am mock';

        $actual = $target->useTrait();

        $this->assertSame($expected, $actual);
    }
}

trait ATrait
{
    public function thisIsATrait()
    {
        return 'a real trait';
    }
}

class SampleTraitDemo
{
    use ATrait;

    public function useTrait()
    {
        return ('use Trait - ' . $this->thisIsATrait());
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment