wp = new WPFunctions(); $this->settings = SettingsController::getInstance(); $this->original_wc_settings = $this->settings->get('woocommerce'); $this->newsletters_repository = ContainerWrapper::getInstance()->get(NewslettersRepository::class); $this->transactional_emails = new TransactionalEmails( $this->wp, $this->settings, ContainerWrapper::getInstance()->get(Template::class), ContainerWrapper::getInstance()->get(Renderer::class), Stub::makeEmpty(WooCommerceHelper::class), $this->newsletters_repository ); } function testInitCreatesTransactionalEmailAndSavesItsId() { $this->transactional_emails->init(); $email = $this->newsletters_repository->findOneBy(['type' => Newsletter::TYPE_WC_TRANSACTIONAL_EMAIL]); $id = $this->settings->get(TransactionalEmails::SETTING_EMAIL_ID, null); expect($email)->notEmpty(); expect($id)->notNull(); expect($email->getId())->equals($id); } function testInitDoesntCreateTransactionalEmailIfSettingAlreadySet() { $this->settings->set(TransactionalEmails::SETTING_EMAIL_ID, 1); $this->transactional_emails->init(); $email = $this->newsletters_repository->findOneBy(['type' => Newsletter::TYPE_WC_TRANSACTIONAL_EMAIL]); expect($email)->equals(null); } function testInitUsesImageFromWCSettings() { $wp = Stub::make(new WPFunctions, ['getOption' => function($name) { if ($name == 'woocommerce_email_header_image') { return 'my-awesome-image-url'; } }]); $transactional_emails = new TransactionalEmails( $wp, $this->settings, ContainerWrapper::getInstance()->get(Template::class), ContainerWrapper::getInstance()->get(Renderer::class), Stub::makeEmpty(WooCommerceHelper::class), $this->newsletters_repository ); $transactional_emails->init(); $email = $this->newsletters_repository->findOneBy([ 'type' => NewsletterEntity::TYPE_WC_TRANSACTIONAL_EMAIL, ]); expect($email)->notEmpty(); expect(json_encode($email->getBody()))->contains('my-awesome-image-url'); } function testUseTemplateForWCEmails() { $added_actions = []; $removed_actions = []; $newsletter = new NewsletterEntity; $newsletter->setType(Newsletter::TYPE_WC_TRANSACTIONAL_EMAIL); $newsletter->setSubject('WooCommerce Transactional Email'); $newsletter->setBody([ 'content' => L::col([ L::row([L::col([['type' => 'text', 'text' => 'Some text before heading']])]), ['type' => 'woocommerceHeading'], L::row([L::col([['type' => 'text', 'text' => 'Some text between heading and content']])]), ['type' => 'woocommerceContent'], L::row([L::col([['type' => 'text', 'text' => 'Some text after content']])]), ]), ]); $this->newsletters_repository->persist($newsletter); $this->newsletters_repository->flush(); $this->settings->set(TransactionalEmails::SETTING_EMAIL_ID, $newsletter->getId()); $wp = Stub::make(new WPFunctions, [ 'getOption' => function($name) { return ''; }, 'addAction' => function ($name, $action) use(&$added_actions) { $added_actions[$name] = $action; }, 'removeAction' => function ($name, $action) use(&$removed_actions) { $removed_actions[$name] = $action; }, ]); $renderer = Stub::make(Renderer::class, [ 'render' => function($email) use(&$newsletter) { expect($email->id)->equals($newsletter->getId()); }, 'getHTMLBeforeContent' => function($heading_text) { return 'HTML before content with ' . $heading_text; }, 'getHTMLAfterContent' => function() { return 'HTML after content'; }, 'prefixCss' => function($css) { return 'prefixed ' . $css; }, ]); $transactional_emails = new TransactionalEmails( $wp, $this->settings, ContainerWrapper::getInstance()->get(Template::class), $renderer, Stub::makeEmpty(WooCommerceHelper::class), ContainerWrapper::getInstance()->get(NewslettersRepository::class) ); $transactional_emails->useTemplateForWoocommerceEmails(); expect($added_actions)->count(1); expect($added_actions['woocommerce_init'])->isCallable(); $added_actions['woocommerce_init'](); expect($removed_actions)->count(2); expect($added_actions)->count(4); expect($added_actions['woocommerce_email_header'])->isCallable(); ob_start(); $added_actions['woocommerce_email_header']('heading text'); expect(ob_get_clean())->equals('HTML before content with heading text'); expect($added_actions['woocommerce_email_footer'])->isCallable(); ob_start(); $added_actions['woocommerce_email_footer'](); expect(ob_get_clean())->equals('HTML after content'); expect($added_actions['woocommerce_email_styles'])->isCallable(); expect($added_actions['woocommerce_email_styles']('some css'))->equals('prefixed some css'); } function _after() { $this->entity_manager ->createQueryBuilder() ->delete() ->from(NewsletterEntity::class, 'n') ->getQuery() ->execute(); $this->settings->set('woocommerce', $this->original_wc_settings); } }