WorkflowExtensionTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Twig\Tests\Extension;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\Twig\Extension\WorkflowExtension;
  13. use Symfony\Component\Workflow\Definition;
  14. use Symfony\Component\Workflow\Marking;
  15. use Symfony\Component\Workflow\Registry;
  16. use Symfony\Component\Workflow\SupportStrategy\ClassInstanceSupportStrategy;
  17. use Symfony\Component\Workflow\Transition;
  18. use Symfony\Component\Workflow\Workflow;
  19. class WorkflowExtensionTest extends TestCase
  20. {
  21. private $extension;
  22. protected function setUp()
  23. {
  24. if (!class_exists(Workflow::class)) {
  25. $this->markTestSkipped('The Workflow component is needed to run tests for this extension.');
  26. }
  27. $places = array('ordered', 'waiting_for_payment', 'processed');
  28. $transitions = array(
  29. new Transition('t1', 'ordered', 'waiting_for_payment'),
  30. new Transition('t2', 'waiting_for_payment', 'processed'),
  31. );
  32. $definition = new Definition($places, $transitions);
  33. $workflow = new Workflow($definition);
  34. $registry = new Registry();
  35. $registry->add($workflow, new ClassInstanceSupportStrategy(\stdClass::class));
  36. $this->extension = new WorkflowExtension($registry);
  37. }
  38. public function testCanTransition()
  39. {
  40. $subject = new \stdClass();
  41. $subject->marking = array();
  42. $this->assertTrue($this->extension->canTransition($subject, 't1'));
  43. $this->assertFalse($this->extension->canTransition($subject, 't2'));
  44. }
  45. public function testGetEnabledTransitions()
  46. {
  47. $subject = new \stdClass();
  48. $subject->marking = array();
  49. $transitions = $this->extension->getEnabledTransitions($subject);
  50. $this->assertCount(1, $transitions);
  51. $this->assertInstanceOf(Transition::class, $transitions[0]);
  52. $this->assertSame('t1', $transitions[0]->getName());
  53. }
  54. public function testHasMarkedPlace()
  55. {
  56. $subject = new \stdClass();
  57. $subject->marking = array();
  58. $subject->marking = array('ordered' => 1, 'waiting_for_payment' => 1);
  59. $this->assertTrue($this->extension->hasMarkedPlace($subject, 'ordered'));
  60. $this->assertTrue($this->extension->hasMarkedPlace($subject, 'waiting_for_payment'));
  61. $this->assertFalse($this->extension->hasMarkedPlace($subject, 'processed'));
  62. }
  63. public function testGetMarkedPlaces()
  64. {
  65. $subject = new \stdClass();
  66. $subject->marking = array();
  67. $subject->marking = array('ordered' => 1, 'waiting_for_payment' => 1);
  68. $this->assertSame(array('ordered', 'waiting_for_payment'), $this->extension->getMarkedPlaces($subject));
  69. $this->assertSame($subject->marking, $this->extension->getMarkedPlaces($subject, false));
  70. }
  71. }